From b4ea2f3048518455b80262c16ebaa80d3cc7bba6 Mon Sep 17 00:00:00 2001 From: Sebastien Douheret Date: Wed, 18 Jul 2018 15:41:51 +0200 Subject: Add XDS_SERVER_ROOT_CFG_DIR env var support By default xds-server uses $HOME/.xds/server directory to save all config. This changes allows user to change this root directory by defining XDS_SERVER_ROOT_CFG_DIR environment variable. Change-Id: Iebde8dcf2d27b77671c7332bac04295d31659e24 Signed-off-by: Sebastien Douheret --- lib/xdsconfig/config.go | 29 +++++++++++++++++++++++------ lib/xdsconfig/fileconfig.go | 13 +++---------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/lib/xdsconfig/config.go b/lib/xdsconfig/config.go index ee20bd2..ca5b5cb 100644 --- a/lib/xdsconfig/config.go +++ b/lib/xdsconfig/config.go @@ -21,6 +21,8 @@ import ( "fmt" "io" "os" + "os/user" + "path" "path/filepath" common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git/golib" @@ -53,8 +55,8 @@ type Options struct { const ( DefaultAPIVersion = "1" DefaultPort = "8000" - DefaultShareDir = "${HOME}/.xds/server/projects" - DefaultSTHomeDir = "${HOME}/.xds/server/syncthing-config" + DefaultShareDir = "projects" + DefaultSTHomeDir = "syncthing-config" DefaultSdkScriptsDir = "${EXEPATH}/sdks" DefaultSdkDbUpdate = "startup" ) @@ -63,12 +65,12 @@ const ( func Init(cliCtx *cli.Context, log *logrus.Logger) (*Config, error) { var err error - dfltShareDir := DefaultShareDir - dfltSTHomeDir := DefaultSTHomeDir - if resDir, err := common.ResolveEnvVar(DefaultShareDir); err == nil { + dfltShareDir := path.Join(ConfigRootDir(), DefaultShareDir) + dfltSTHomeDir := path.Join(ConfigRootDir(), DefaultSTHomeDir) + if resDir, err := common.ResolveEnvVar(dfltShareDir); err == nil { dfltShareDir = resDir } - if resDir, err := common.ResolveEnvVar(DefaultSTHomeDir); err == nil { + if resDir, err := common.ResolveEnvVar(dfltSTHomeDir); err == nil { dfltSTHomeDir = resDir } @@ -157,3 +159,18 @@ func Init(cliCtx *cli.Context, log *logrus.Logger) (*Config, error) { return &c, nil } + +// ConfigRootDir return the root directory where xds server save all config files +func ConfigRootDir() string { + root := "$HOME" + if usr, err := user.Current(); err == nil { + root = usr.HomeDir + } + + // Default $HOME/.xds/server but may be changed by an env variable + if envVar, envDef := os.LookupEnv("XDS_SERVER_ROOT_CFG_DIR"); envDef { + root = envVar + } + + return path.Join(root, "/.xds/server") +} diff --git a/lib/xdsconfig/fileconfig.go b/lib/xdsconfig/fileconfig.go index 86f39b5..b786a24 100644 --- a/lib/xdsconfig/fileconfig.go +++ b/lib/xdsconfig/fileconfig.go @@ -28,9 +28,6 @@ import ( common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git/golib" ) -// ConfigDir Directory in user HOME directory where xds config will be saved -var ConfigDir = path.Join(".xds", "server") - const ( // GlobalConfigFilename Global config filename GlobalConfigFilename = "server-config.json" @@ -74,8 +71,8 @@ func readGlobalConfig(c *Config, confFile string) error { if confFile != "" { searchIn = append(searchIn, confFile) } - if usr, err := user.Current(); err == nil { - searchIn = append(searchIn, path.Join(usr.HomeDir, ConfigDir, GlobalConfigFilename)) + if _, err := user.Current(); err == nil { + searchIn = append(searchIn, path.Join(ConfigRootDir(), GlobalConfigFilename)) } searchIn = append(searchIn, "/etc/xds/server/server-config.json") @@ -172,11 +169,7 @@ func readGlobalConfig(c *Config, confFile string) error { } func configFilenameGet(cfgFile string) (string, error) { - usr, err := user.Current() - if err != nil { - return "", err - } - return path.Join(usr.HomeDir, ConfigDir, cfgFile), nil + return path.Join(ConfigRootDir(), cfgFile), nil } // FoldersConfigFilenameGet Return the FoldersConfig filename -- cgit 1.2.3-korg