Skip to content

Commit 367e2c1

Browse files
authored
Allow overriding of CLI install path and config dir (#1521)
1 parent 891cd1b commit 367e2c1

File tree

6 files changed

+33
-6
lines changed

6 files changed

+33
-6
lines changed

cli/cmd/root.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/cortexlabs/cortex/cli/types/flags"
2727
"github.com/cortexlabs/cortex/pkg/lib/errors"
2828
"github.com/cortexlabs/cortex/pkg/lib/exit"
29+
"github.com/cortexlabs/cortex/pkg/lib/files"
2930
libjson "github.com/cortexlabs/cortex/pkg/lib/json"
3031
s "github.com/cortexlabs/cortex/pkg/lib/strings"
3132
"github.com/cortexlabs/cortex/pkg/lib/telemetry"
@@ -72,7 +73,13 @@ func init() {
7273
}
7374
_homeDir = s.EnsureSuffix(homeDir, "/")
7475

75-
_localDir = filepath.Join(homeDir, ".cortex")
76+
_localDir = os.Getenv("CORTEX_CLI_CONFIG_DIR")
77+
if _localDir != "" {
78+
_localDir = files.UserRelToAbsPath(_localDir)
79+
} else {
80+
_localDir = filepath.Join(homeDir, ".cortex")
81+
}
82+
7683
err = os.MkdirAll(_localDir, os.ModePerm)
7784
if err != nil {
7885
err := errors.Wrap(err, "unable to write to home directory", _localDir)

cli/local/config.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/cortexlabs/cortex/pkg/lib/errors"
2424
"github.com/cortexlabs/cortex/pkg/lib/exit"
25+
"github.com/cortexlabs/cortex/pkg/lib/files"
2526
s "github.com/cortexlabs/cortex/pkg/lib/strings"
2627
"github.com/mitchellh/go-homedir"
2728
)
@@ -45,7 +46,13 @@ func init() {
4546
exit.Error(err)
4647
}
4748

48-
_localDir = filepath.Join(homeDir, ".cortex")
49+
_localDir = os.Getenv("CORTEX_CLI_CONFIG_DIR")
50+
if _localDir != "" {
51+
_localDir = files.UserRelToAbsPath(_localDir)
52+
} else {
53+
_localDir = filepath.Join(homeDir, ".cortex")
54+
}
55+
4956
err = os.MkdirAll(_localDir, os.ModePerm)
5057
if err != nil {
5158
err := errors.Wrap(err, "unable to write to home directory", _localDir)

dev/cli_md_template.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ $ bash -c "$(curl -sS https://raw.githubusercontent.com/cortexlabs/cortex/vINSER
2020
$ bash -c "$(curl -sS https://raw.githubusercontent.com/cortexlabs/cortex/v0.18.1/get-cli.sh)"
2121
```
2222

23+
By default, the Cortex CLI is installed at `/usr/local/bin/cortex`. To install the executable elsewhere, export the `CORTEX_INSTALL_PATH` environment variable to your desired location before running the command above.
24+
25+
By default, the Cortex CLI creates a directory at `~/.cortex/` and uses it to store environment configuration. To use a different directory, export the `CORTEX_CLI_CONFIG_DIR` environment variable before running a `cortex` command.
26+
2327
### Windows
2428

2529
To install the Cortex CLI on a Windows machine, follow [this guide](../guides/windows-cli.md).

docs/miscellaneous/cli.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ $ bash -c "$(curl -sS https://raw.githubusercontent.com/cortexlabs/cortex/vINSER
2020
$ bash -c "$(curl -sS https://raw.githubusercontent.com/cortexlabs/cortex/v0.18.1/get-cli.sh)"
2121
```
2222

23+
By default, the Cortex CLI is installed at `/usr/local/bin/cortex`. To install the executable elsewhere, export the `CORTEX_INSTALL_PATH` environment variable to your desired location before running the command above.
24+
25+
By default, the Cortex CLI creates a directory at `~/.cortex/` and uses it to store environment configuration. To use a different directory, export the `CORTEX_CLI_CONFIG_DIR` environment variable before running a `cortex` command.
26+
2327
### Windows
2428

2529
To install the Cortex CLI on a Windows machine, follow [this guide](../guides/windows-cli.md).

get-cli.sh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
set -e
1818

1919
CORTEX_VERSION_BRANCH_STABLE=master
20+
CORTEX_INSTALL_PATH="${CORTEX_INSTALL_PATH:-/usr/local/bin/cortex}"
21+
22+
# replace ~ with the home directory path
23+
CORTEX_INSTALL_PATH="${CORTEX_INSTALL_PATH/#\~/$HOME}"
2024

2125
case "$OSTYPE" in
2226
darwin*) parsed_os="darwin" ;;
@@ -25,7 +29,7 @@ case "$OSTYPE" in
2529
esac
2630

2731
function main() {
28-
echo -e "\ndownloading cli (/usr/local/bin/cortex) ...\n"
32+
echo -e "\ndownloading cli (${CORTEX_INSTALL_PATH}) ...\n"
2933

3034
cortex_sh_tmp_dir="$HOME/.cortex-sh-tmp"
3135
rm -rf $cortex_sh_tmp_dir && mkdir -p $cortex_sh_tmp_dir
@@ -42,10 +46,10 @@ function main() {
4246
chmod +x $cortex_sh_tmp_dir/cortex
4347

4448
if [ $(id -u) = 0 ]; then
45-
mv -f $cortex_sh_tmp_dir/cortex /usr/local/bin/cortex
49+
mv -f $cortex_sh_tmp_dir/cortex $CORTEX_INSTALL_PATH
4650
else
4751
ask_sudo
48-
sudo mv -f $cortex_sh_tmp_dir/cortex /usr/local/bin/cortex
52+
sudo mv -f $cortex_sh_tmp_dir/cortex $CORTEX_INSTALL_PATH
4953
fi
5054

5155
rm -rf $cortex_sh_tmp_dir

pkg/lib/files/files.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,8 @@ func RelToAbsPath(relativePath string, baseDir string) string {
243243
if !IsAbsOrTildePrefixed(relativePath) {
244244
relativePath = filepath.Join(baseDir, relativePath)
245245
}
246-
return filepath.Clean(relativePath)
246+
cleanPath, _ := Clean(relativePath)
247+
return cleanPath
247248
}
248249

249250
func UserRelToAbsPath(relativePath string) string {

0 commit comments

Comments
 (0)