Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/xp.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ Use the following environment variables in your terminal to simplify the communi

|`ENONIC_CLI_HTTP_PROXY`
|URL of proxy server to use

|`ENONIC_CLI_HOME_PATH`
|Path to use for the relative home directory for XP ( Default is `$HOME`, or the user directory of your user). For example, if you set this to `/tmp`, there will be placed a folder called `/tmp/.enonic` and your sandboxes and similar will live there.
|===

NOTE: Credentials passed via command line overrides the environment variables.
Expand Down
2 changes: 1 addition & 1 deletion internal/app/commands/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func GetInEnonicDir(children ...string) string {
}
}
if joinArgs == nil {
joinArgs = []string{util.GetHomeDir(), ".enonic"}
joinArgs = []string{util.GetEnonicHome()}
}
if len(children) > 0 {
joinArgs = append(joinArgs, children...)
Expand Down
4 changes: 2 additions & 2 deletions internal/app/commands/remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ type RemotesData struct {
}

func readRemotesData() RemotesData {
path := filepath.Join(util.GetHomeDir(), ".enonic", ".enonic")
path := filepath.Join(util.GetEnonicHome(), ".enonic")
file := util.OpenOrCreateDataFile(path, true)
defer file.Close()

Expand All @@ -90,7 +90,7 @@ func readRemotesData() RemotesData {
}

func writeRemotesData(data RemotesData) {
path := filepath.Join(util.GetHomeDir(), ".enonic", ".enonic")
path := filepath.Join(util.GetEnonicHome(), ".enonic")
file := util.OpenOrCreateDataFile(path, false)
defer file.Close()

Expand Down
10 changes: 3 additions & 7 deletions internal/app/commands/sandbox/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,7 @@ func updateXPConfig(sandboxName string) {
}

func ensureDirStructure() {
// Using go-homedir instead of user.Current()
// because of https://github.com/golang/go/issues/6376
home := util.GetHomeDir()

enonicPath := filepath.Join(home, ".enonic")
enonicPath := util.GetEnonicHome()
enonicPathInfo, _ := os.Lstat(enonicPath)
enonicPathExists := enonicPathInfo != nil
enonicPathIsSymlink := enonicPathExists && enonicPathInfo.Mode()&os.ModeSymlink == os.ModeSymlink
Expand Down Expand Up @@ -374,8 +370,8 @@ func ensureDirStructure() {
}
}

createFolderIfNotExist(home, ".enonic", "distributions")
createFolderIfNotExist(home, ".enonic", "sandboxes")
createFolderIfNotExist(enonicPath, "distributions")
createFolderIfNotExist(enonicPath, "sandboxes")
}

func mustMove(from, to string) {
Expand Down
13 changes: 13 additions & 0 deletions internal/app/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
"time"
)

const ENONIC_CLI_HOME_ENV_VAR_NAME = "ENONIC_CLI_HOME_PATH"

func PrettyPrintJSON(data interface{}) string {
var out = new(bytes.Buffer)
enc := json.NewEncoder(out)
Expand Down Expand Up @@ -257,6 +259,17 @@ func GetCurrentOsWithArch() string {
return currentOs
}

func GetEnonicHome() string {
// Using go-homedir instead of user.Current()
// because of https://github.com/golang/go/issues/6376
home := GetHomeDir()
// Allow people to override it, using the environment variable ENONIC_CLI_HOME_PATH.
if envHome := os.Getenv(ENONIC_CLI_HOME_ENV_VAR_NAME); envHome != "" {
home = envHome
}
return filepath.Join(home, ".enonic")
}

// Taken from go-homedir
func GetHomeDir() string {
var result string
Expand Down
33 changes: 33 additions & 0 deletions internal/app/util/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package util

import (
"testing"
"os"
"path/filepath"
)

func TestGetHomeDir(t *testing.T) {
expected := os.Getenv("HOME")
home := GetHomeDir()
if home != expected {
t.Errorf("GetHomeDir() failed: %s", home)
}
}

func TestGetEnonicDir(t *testing.T) {
expected := filepath.Join(os.Getenv("HOME"), ".enonic")
home := GetEnonicHome()
if home != expected {
t.Errorf("GetHomeDir() failed: %s", home)
}
}

func TestGetEnonicDirOverridden(t *testing.T) {
expected := "/root/.enonic"
os.Setenv("ENONIC_CLI_HOME_PATH", "/root")
defer os.Unsetenv("ENONIC_CLI_HOME_PATH")
home := GetEnonicHome()
if (home != expected) {
t.Errorf("GetHomeDir() failed: %s", home)
}
}