From f0662675bc4ad7dd3ae5e18b953cf42dfb6429ec Mon Sep 17 00:00:00 2001 From: Michal Niewrzal Date: Fri, 14 Dec 2018 22:14:59 +0100 Subject: [PATCH] Add '--dir' param for all CLI parts (replace --base-path) (#833) * Add '--dir' param for all CLI parts (replace --base-path) * FindDirParam method moved * fix compilation error * make param global * remove unused fields * rename param * remove config flag * goimports --- cmd/captplanet/main.go | 12 ++++-- cmd/captplanet/setup.go | 91 ++++++++++++++++++---------------------- cmd/satellite/main.go | 41 ++++++++++-------- cmd/storagenode/main.go | 45 +++++++++++--------- cmd/uplink/cmd/root.go | 8 +++- cmd/uplink/cmd/setup.go | 43 ++++++++++++------- pkg/cfgstruct/bind.go | 13 ++++++ pkg/process/exec_conf.go | 12 ++++-- 8 files changed, 151 insertions(+), 114 deletions(-) diff --git a/cmd/captplanet/main.go b/cmd/captplanet/main.go index a221c2318..19a175eed 100644 --- a/cmd/captplanet/main.go +++ b/cmd/captplanet/main.go @@ -18,6 +18,7 @@ import ( "storj.io/storj/internal/fpath" "storj.io/storj/internal/memory" + "storj.io/storj/pkg/cfgstruct" "storj.io/storj/pkg/process" ) @@ -30,6 +31,7 @@ var ( } defaultConfDir = fpath.ApplicationDir("storj", "capt") + confDir *string ) func main() { @@ -38,10 +40,12 @@ func main() { } func init() { - runCmd.Flags().String("config", - filepath.Join(defaultConfDir, "config.yaml"), "path to configuration") - setupCmd.Flags().String("config", - filepath.Join(defaultConfDir, "setup.yaml"), "path to configuration") + dirParam := cfgstruct.FindConfigDirParam() + if dirParam != "" { + defaultConfDir = dirParam + } + + confDir = rootCmd.PersistentFlags().String("config-dir", defaultConfDir, "main directory for captplanet configuration") } // dumpHandler listens for Ctrl+\ on Unix diff --git a/cmd/captplanet/setup.go b/cmd/captplanet/setup.go index c0c0f21c3..8d6de2657 100644 --- a/cmd/captplanet/setup.go +++ b/cmd/captplanet/setup.go @@ -25,7 +25,6 @@ type Config struct { UplinkIdentity provider.IdentitySetupConfig StorageNodeCA provider.CASetupConfig StorageNodeIdentity provider.IdentitySetupConfig - BasePath string `help:"base path for captain planet storage" default:"$CONFDIR"` ListenHost string `help:"the host for providers to listen on" default:"127.0.0.1"` StartingPort int `help:"all providers will listen on ports consecutively starting with this one" default:"7777"` APIKey string `default:"abc123" help:"the api key to use for the satellite"` @@ -36,38 +35,38 @@ type Config struct { var ( setupCmd = &cobra.Command{ - Use: "setup", - Short: "Set up configurations", - RunE: cmdSetup, + Use: "setup", + Short: "Set up configurations", + RunE: cmdSetup, + Annotations: map[string]string{"type": "setup"}, } setupCfg Config ) func init() { rootCmd.AddCommand(setupCmd) - cfgstruct.Bind(setupCmd.Flags(), &setupCfg, - cfgstruct.ConfDir(defaultConfDir)) + cfgstruct.Bind(setupCmd.Flags(), &setupCfg, cfgstruct.ConfDir(defaultConfDir)) } func cmdSetup(cmd *cobra.Command, args []string) (err error) { - setupCfg.BasePath, err = filepath.Abs(setupCfg.BasePath) + setupDir, err := filepath.Abs(*confDir) if err != nil { return err } - valid, err := fpath.IsValidSetupDir(setupCfg.BasePath) + valid, err := fpath.IsValidSetupDir(setupDir) if !setupCfg.Overwrite && !valid { - fmt.Printf("captplanet configuration already exists (%v). rerun with --overwrite\n", setupCfg.BasePath) + fmt.Printf("captplanet configuration already exists (%v). rerun with --overwrite\n", setupDir) return nil } else if setupCfg.Overwrite && err == nil { fmt.Println("overwriting existing captplanet config") - err = os.RemoveAll(setupCfg.BasePath) + err = os.RemoveAll(setupDir) if err != nil { return err } } - satellitePath := filepath.Join(setupCfg.BasePath, "satellite") + satellitePath := filepath.Join(setupDir, "satellite") err = os.MkdirAll(satellitePath, 0700) if err != nil { return err @@ -83,7 +82,7 @@ func cmdSetup(cmd *cobra.Command, args []string) (err error) { } for i := 0; i < len(runCfg.StorageNodes); i++ { - storagenodePath := filepath.Join(setupCfg.BasePath, fmt.Sprintf("f%d", i)) + storagenodePath := filepath.Join(setupDir, fmt.Sprintf("f%d", i)) err = os.MkdirAll(storagenodePath, 0700) if err != nil { return err @@ -101,7 +100,7 @@ func cmdSetup(cmd *cobra.Command, args []string) (err error) { } } - uplinkPath := filepath.Join(setupCfg.BasePath, "uplink") + uplinkPath := filepath.Join(setupDir, "uplink") err = os.MkdirAll(uplinkPath, 0700) if err != nil { return err @@ -134,42 +133,33 @@ func cmdSetup(cmd *cobra.Command, args []string) (err error) { overlayAddr := joinHostPort(setupCfg.ListenHost, startingPort+1) overrides := map[string]interface{}{ - "satellite.identity.cert-path": setupCfg.SatelliteIdentity.CertPath, - "satellite.identity.key-path": setupCfg.SatelliteIdentity.KeyPath, - "satellite.identity.server.address": joinHostPort( - setupCfg.ListenHost, startingPort+1), + "satellite.identity.cert-path": setupCfg.SatelliteIdentity.CertPath, + "satellite.identity.key-path": setupCfg.SatelliteIdentity.KeyPath, + "satellite.identity.server.address": joinHostPort(setupCfg.ListenHost, startingPort+1), "satellite.identity.server.revocation-dburl": "redis://127.0.0.1:6378?db=2&password=abc123", - "satellite.kademlia.bootstrap-addr": joinHostPort( - setupCfg.ListenHost, startingPort+1), - "satellite.pointer-db.database-url": "bolt://" + filepath.Join( - setupCfg.BasePath, "satellite", "pointerdb.db"), - "satellite.overlay.database-url": "bolt://" + filepath.Join( - setupCfg.BasePath, "satellite", "overlay.db"), - "satellite.kademlia.alpha": 3, - "satellite.repairer.queue-address": "redis://127.0.0.1:6378?db=1&password=abc123", - "satellite.repairer.overlay-addr": overlayAddr, - "satellite.repairer.pointer-db-addr": joinHostPort( - setupCfg.ListenHost, startingPort+1), - "satellite.repairer.api-key": setupCfg.APIKey, - "uplink.identity.cert-path": setupCfg.UplinkIdentity.CertPath, - "uplink.identity.key-path": setupCfg.UplinkIdentity.KeyPath, - "uplink.identity.server.address": joinHostPort( - setupCfg.ListenHost, startingPort), - "uplink.identity.server.revocation-dburl": "redis://127.0.0.1:6378?db=2&password=abc123", - "uplink.client.overlay-addr": joinHostPort( - setupCfg.ListenHost, startingPort+1), - "uplink.client.pointer-db-addr": joinHostPort( - setupCfg.ListenHost, startingPort+1), - "uplink.minio.dir": filepath.Join( - setupCfg.BasePath, "uplink", "minio"), - "uplink.enc.key": setupCfg.EncKey, - "uplink.client.api-key": setupCfg.APIKey, - "uplink.rs.min-threshold": 1 * len(runCfg.StorageNodes) / 5, - "uplink.rs.repair-threshold": 2 * len(runCfg.StorageNodes) / 5, - "uplink.rs.success-threshold": 3 * len(runCfg.StorageNodes) / 5, - "uplink.rs.max-threshold": 4 * len(runCfg.StorageNodes) / 5, - "kademlia.bucket-size": 4, - "kademlia.replacement-cache-size": 1, + "satellite.kademlia.bootstrap-addr": joinHostPort(setupCfg.ListenHost, startingPort+1), + "satellite.pointer-db.database-url": "bolt://" + filepath.Join(setupDir, "satellite", "pointerdb.db"), + "satellite.overlay.database-url": "bolt://" + filepath.Join(setupDir, "satellite", "overlay.db"), + "satellite.kademlia.alpha": 3, + "satellite.repairer.queue-address": "redis://127.0.0.1:6378?db=1&password=abc123", + "satellite.repairer.overlay-addr": overlayAddr, + "satellite.repairer.pointer-db-addr": joinHostPort(setupCfg.ListenHost, startingPort+1), + "satellite.repairer.api-key": setupCfg.APIKey, + "uplink.identity.cert-path": setupCfg.UplinkIdentity.CertPath, + "uplink.identity.key-path": setupCfg.UplinkIdentity.KeyPath, + "uplink.identity.server.address": joinHostPort(setupCfg.ListenHost, startingPort), + "uplink.identity.server.revocation-dburl": "redis://127.0.0.1:6378?db=2&password=abc123", + "uplink.client.overlay-addr": joinHostPort(setupCfg.ListenHost, startingPort+1), + "uplink.client.pointer-db-addr": joinHostPort(setupCfg.ListenHost, startingPort+1), + "uplink.minio.dir": filepath.Join(setupDir, "uplink", "minio"), + "uplink.enc.key": setupCfg.EncKey, + "uplink.client.api-key": setupCfg.APIKey, + "uplink.rs.min-threshold": 1 * len(runCfg.StorageNodes) / 5, + "uplink.rs.repair-threshold": 2 * len(runCfg.StorageNodes) / 5, + "uplink.rs.success-threshold": 3 * len(runCfg.StorageNodes) / 5, + "uplink.rs.max-threshold": 4 * len(runCfg.StorageNodes) / 5, + "kademlia.bucket-size": 4, + "kademlia.replacement-cache-size": 1, // TODO: this will eventually go away "pointer-db.auth.api-key": setupCfg.APIKey, @@ -183,7 +173,7 @@ func cmdSetup(cmd *cobra.Command, args []string) (err error) { } for i := 0; i < len(runCfg.StorageNodes); i++ { - storagenodePath := filepath.Join(setupCfg.BasePath, fmt.Sprintf("f%d", i)) + storagenodePath := filepath.Join(setupDir, fmt.Sprintf("f%d", i)) storagenode := fmt.Sprintf("storage-nodes.%02d.", i) overrides[storagenode+"identity.cert-path"] = filepath.Join( storagenodePath, "identity.cert") @@ -200,8 +190,7 @@ func cmdSetup(cmd *cobra.Command, args []string) (err error) { overrides[storagenode+"kademlia.alpha"] = 3 } - return process.SaveConfig(runCmd.Flags(), - filepath.Join(setupCfg.BasePath, "config.yaml"), overrides) + return process.SaveConfig(runCmd.Flags(), filepath.Join(setupDir, "config.yaml"), overrides) } func joinHostPort(host string, port int) string { diff --git a/cmd/satellite/main.go b/cmd/satellite/main.go index 155dd83d2..df1e8519b 100644 --- a/cmd/satellite/main.go +++ b/cmd/satellite/main.go @@ -46,9 +46,10 @@ var ( RunE: cmdRun, } setupCmd = &cobra.Command{ - Use: "setup", - Short: "Create config files", - RunE: cmdSetup, + Use: "setup", + Short: "Create config files", + RunE: cmdSetup, + Annotations: map[string]string{"type": "setup"}, } diagCmd = &cobra.Command{ Use: "diag", @@ -74,7 +75,6 @@ var ( Discovery discovery.Config } setupCfg struct { - BasePath string `default:"$CONFDIR" help:"base path for setup"` CA provider.CASetupConfig Identity provider.IdentitySetupConfig Overwrite bool `default:"false" help:"whether to overwrite pre-existing configuration files"` @@ -88,10 +88,19 @@ var ( } defaultConfDir string + confDir *string ) func init() { defaultConfDir = fpath.ApplicationDir("storj", "satellite") + + dirParam := cfgstruct.FindConfigDirParam() + if dirParam != "" { + defaultConfDir = dirParam + } + + confDir = rootCmd.PersistentFlags().String("config-dir", defaultConfDir, "main directory for satellite configuration") + rootCmd.AddCommand(runCmd) rootCmd.AddCommand(setupCmd) rootCmd.AddCommand(diagCmd) @@ -133,35 +142,35 @@ func cmdRun(cmd *cobra.Command, args []string) (err error) { } func cmdSetup(cmd *cobra.Command, args []string) (err error) { - setupCfg.BasePath, err = filepath.Abs(setupCfg.BasePath) + setupDir, err := filepath.Abs(*confDir) if err != nil { return err } - valid, err := fpath.IsValidSetupDir(setupCfg.BasePath) + valid, err := fpath.IsValidSetupDir(setupDir) if !setupCfg.Overwrite && !valid { - fmt.Printf("satellite configuration already exists (%v). rerun with --overwrite\n", setupCfg.BasePath) + fmt.Printf("satellite configuration already exists (%v). rerun with --overwrite\n", setupDir) return nil } else if setupCfg.Overwrite && err == nil { fmt.Println("overwriting existing satellite config") - err = os.RemoveAll(setupCfg.BasePath) + err = os.RemoveAll(setupDir) if err != nil { return err } } - err = os.MkdirAll(setupCfg.BasePath, 0700) + err = os.MkdirAll(setupDir, 0700) if err != nil { return err } // TODO: handle setting base path *and* identity file paths via args // NB: if base path is set this overrides identity and CA path options - if setupCfg.BasePath != defaultConfDir { - setupCfg.CA.CertPath = filepath.Join(setupCfg.BasePath, "ca.cert") - setupCfg.CA.KeyPath = filepath.Join(setupCfg.BasePath, "ca.key") - setupCfg.Identity.CertPath = filepath.Join(setupCfg.BasePath, "identity.cert") - setupCfg.Identity.KeyPath = filepath.Join(setupCfg.BasePath, "identity.key") + if setupDir != defaultConfDir { + setupCfg.CA.CertPath = filepath.Join(setupDir, "ca.cert") + setupCfg.CA.KeyPath = filepath.Join(setupDir, "ca.key") + setupCfg.Identity.CertPath = filepath.Join(setupDir, "identity.cert") + setupCfg.Identity.KeyPath = filepath.Join(setupDir, "identity.key") } err = provider.SetupIdentity(process.Ctx(cmd), setupCfg.CA, setupCfg.Identity) if err != nil { @@ -174,7 +183,7 @@ func cmdSetup(cmd *cobra.Command, args []string) (err error) { } return process.SaveConfig(runCmd.Flags(), - filepath.Join(setupCfg.BasePath, "config.yaml"), o) + filepath.Join(setupDir, "config.yaml"), o) } func cmdDiag(cmd *cobra.Command, args []string) (err error) { @@ -286,7 +295,5 @@ func cmdQDiag(cmd *cobra.Command, args []string) (err error) { } func main() { - runCmd.Flags().String("config", - filepath.Join(defaultConfDir, "config.yaml"), "path to configuration") process.Exec(rootCmd) } diff --git a/cmd/storagenode/main.go b/cmd/storagenode/main.go index 4b7e8d589..939769f58 100644 --- a/cmd/storagenode/main.go +++ b/cmd/storagenode/main.go @@ -36,9 +36,10 @@ var ( RunE: cmdRun, } setupCmd = &cobra.Command{ - Use: "setup", - Short: "Create config files", - RunE: cmdSetup, + Use: "setup", + Short: "Create config files", + RunE: cmdSetup, + Annotations: map[string]string{"type": "setup"}, } diagCmd = &cobra.Command{ Use: "diag", @@ -52,20 +53,27 @@ var ( Storage psserver.Config } setupCfg struct { - BasePath string `default:"$CONFDIR" help:"base path for setup"` CA provider.CASetupConfig Identity provider.IdentitySetupConfig } diagCfg struct { - BasePath string `default:"$CONFDIR" help:"base path for setup"` } defaultConfDir string defaultDiagDir string + confDir *string ) func init() { defaultConfDir = fpath.ApplicationDir("storj", "storagenode") + + dirParam := cfgstruct.FindConfigDirParam() + if dirParam != "" { + defaultConfDir = dirParam + } + + confDir = rootCmd.PersistentFlags().String("config-dir", defaultConfDir, "main directory for storagenode configuration") + defaultDiagDir = filepath.Join(defaultConfDir, "storage") rootCmd.AddCommand(runCmd) rootCmd.AddCommand(setupCmd) @@ -80,20 +88,20 @@ func cmdRun(cmd *cobra.Command, args []string) (err error) { } func cmdSetup(cmd *cobra.Command, args []string) (err error) { - setupCfg.BasePath, err = filepath.Abs(setupCfg.BasePath) + setupDir, err := filepath.Abs(*confDir) if err != nil { return err } - err = os.MkdirAll(setupCfg.BasePath, 0700) + err = os.MkdirAll(setupDir, 0700) if err != nil { return err } - setupCfg.CA.CertPath = filepath.Join(setupCfg.BasePath, "ca.cert") - setupCfg.CA.KeyPath = filepath.Join(setupCfg.BasePath, "ca.key") - setupCfg.Identity.CertPath = filepath.Join(setupCfg.BasePath, "identity.cert") - setupCfg.Identity.KeyPath = filepath.Join(setupCfg.BasePath, "identity.key") + setupCfg.CA.CertPath = filepath.Join(setupDir, "ca.cert") + setupCfg.CA.KeyPath = filepath.Join(setupDir, "ca.key") + setupCfg.Identity.CertPath = filepath.Join(setupDir, "identity.cert") + setupCfg.Identity.KeyPath = filepath.Join(setupDir, "identity.key") err = provider.SetupIdentity(process.Ctx(cmd), setupCfg.CA, setupCfg.Identity) if err != nil { @@ -103,28 +111,27 @@ func cmdSetup(cmd *cobra.Command, args []string) (err error) { overrides := map[string]interface{}{ "identity.cert-path": setupCfg.Identity.CertPath, "identity.key-path": setupCfg.Identity.KeyPath, - "storage.path": filepath.Join(setupCfg.BasePath, "storage"), + "storage.path": filepath.Join(setupDir, "storage"), } - return process.SaveConfig(runCmd.Flags(), - filepath.Join(setupCfg.BasePath, "config.yaml"), overrides) + return process.SaveConfig(runCmd.Flags(), filepath.Join(setupDir, "config.yaml"), overrides) } func cmdDiag(cmd *cobra.Command, args []string) (err error) { - diagCfg.BasePath, err = filepath.Abs(diagCfg.BasePath) + diagDir, err := filepath.Abs(*confDir) if err != nil { return err } // check if the directory exists - _, err = os.Stat(diagCfg.BasePath) + _, err = os.Stat(diagDir) if err != nil { - fmt.Println("Storagenode directory doesn't exist", diagCfg.BasePath) + fmt.Println("Storagenode directory doesn't exist", diagDir) return err } // open the sql db - dbpath := filepath.Join(diagCfg.BasePath, "piecestore.db") + dbpath := filepath.Join(diagDir, "piecestore.db") db, err := psdb.Open(context.Background(), "", dbpath) if err != nil { fmt.Println("Storagenode database couldnt open:", dbpath) @@ -202,7 +209,5 @@ func cmdDiag(cmd *cobra.Command, args []string) (err error) { } func main() { - runCmd.Flags().String("config", - filepath.Join(defaultConfDir, "config.yaml"), "path to configuration") process.Exec(rootCmd) } diff --git a/cmd/uplink/cmd/root.go b/cmd/uplink/cmd/root.go index 5561fec65..8c474f1ec 100644 --- a/cmd/uplink/cmd/root.go +++ b/cmd/uplink/cmd/root.go @@ -6,7 +6,6 @@ package cmd import ( "context" "fmt" - "path/filepath" "github.com/spf13/cobra" @@ -40,8 +39,13 @@ func addCmd(cmd *cobra.Command, root *cobra.Command) *cobra.Command { root.AddCommand(cmd) defaultConfDir := fpath.ApplicationDir("storj", "uplink") + + dirParam := cfgstruct.FindConfigDirParam() + if dirParam != "" { + defaultConfDir = dirParam + } + cfgstruct.Bind(cmd.Flags(), &cfg, cfgstruct.ConfDir(defaultConfDir)) - cmd.Flags().String("config", filepath.Join(defaultConfDir, "config.yaml"), "path to configuration") return cmd } diff --git a/cmd/uplink/cmd/setup.go b/cmd/uplink/cmd/setup.go index dd64ffdaf..c29bdb0a0 100644 --- a/cmd/uplink/cmd/setup.go +++ b/cmd/uplink/cmd/setup.go @@ -20,31 +20,43 @@ import ( var ( setupCmd = &cobra.Command{ - Use: "setup", - Short: "Create an uplink config file", - RunE: cmdSetup, + Use: "setup", + Short: "Create an uplink config file", + RunE: cmdSetup, + Annotations: map[string]string{"type": "setup"}, } setupCfg struct { CA provider.CASetupConfig Identity provider.IdentitySetupConfig - BasePath string `default:"$CONFDIR" help:"base path for setup"` Overwrite bool `default:"false" help:"whether to overwrite pre-existing configuration files"` SatelliteAddr string `default:"localhost:7778" help:"the address to use for the satellite"` APIKey string `default:"" help:"the api key to use for the satellite"` EncKey string `default:"" help:"your root encryption key"` GenerateMinioCerts bool `default:"false" help:"generate sample TLS certs for Minio GW"` } + + cliConfDir *string + gwConfDir *string ) func init() { defaultConfDir := fpath.ApplicationDir("storj", "uplink") + + dirParam := cfgstruct.FindConfigDirParam() + if dirParam != "" { + defaultConfDir = dirParam + } + + cliConfDir = CLICmd.PersistentFlags().String("config-dir", defaultConfDir, "main directory for setup configuration") + gwConfDir = GWCmd.PersistentFlags().String("config-dir", defaultConfDir, "main directory for setup configuration") + CLICmd.AddCommand(setupCmd) GWCmd.AddCommand(setupCmd) cfgstruct.Bind(setupCmd.Flags(), &setupCfg, cfgstruct.ConfDir(defaultConfDir)) } func cmdSetup(cmd *cobra.Command, args []string) (err error) { - setupCfg.BasePath, err = filepath.Abs(setupCfg.BasePath) + setupDir, err := filepath.Abs(*cliConfDir) if err != nil { return err } @@ -53,12 +65,12 @@ func cmdSetup(cmd *cobra.Command, args []string) (err error) { return fmt.Errorf("%s - Invalid flag. Pleas see --help", flagname) } - valid, _ := fpath.IsValidSetupDir(setupCfg.BasePath) + valid, _ := fpath.IsValidSetupDir(setupDir) if !setupCfg.Overwrite && !valid { - return fmt.Errorf("uplink configuration already exists (%v). Rerun with --overwrite", setupCfg.BasePath) + return fmt.Errorf("uplink configuration already exists (%v). Rerun with --overwrite", setupDir) } - err = os.MkdirAll(setupCfg.BasePath, 0700) + err = os.MkdirAll(setupDir, 0700) if err != nil { return err } @@ -66,11 +78,11 @@ func cmdSetup(cmd *cobra.Command, args []string) (err error) { defaultConfDir := fpath.ApplicationDir("storj", "uplink") // TODO: handle setting base path *and* identity file paths via args // NB: if base path is set this overrides identity and CA path options - if setupCfg.BasePath != defaultConfDir { - setupCfg.CA.CertPath = filepath.Join(setupCfg.BasePath, "ca.cert") - setupCfg.CA.KeyPath = filepath.Join(setupCfg.BasePath, "ca.key") - setupCfg.Identity.CertPath = filepath.Join(setupCfg.BasePath, "identity.cert") - setupCfg.Identity.KeyPath = filepath.Join(setupCfg.BasePath, "identity.key") + if setupDir != defaultConfDir { + setupCfg.CA.CertPath = filepath.Join(setupDir, "ca.cert") + setupCfg.CA.KeyPath = filepath.Join(setupDir, "ca.key") + setupCfg.Identity.CertPath = filepath.Join(setupDir, "identity.cert") + setupCfg.Identity.KeyPath = filepath.Join(setupDir, "identity.key") } err = provider.SetupIdentity(process.Ctx(cmd), setupCfg.CA, setupCfg.Identity) if err != nil { @@ -78,7 +90,7 @@ func cmdSetup(cmd *cobra.Command, args []string) (err error) { } if setupCfg.GenerateMinioCerts { - minioCerts := filepath.Join(setupCfg.BasePath, "minio", "certs") + minioCerts := filepath.Join(setupDir, "minio", "certs") if err := os.MkdirAll(minioCerts, 0744); err != nil { return err } @@ -111,8 +123,7 @@ func cmdSetup(cmd *cobra.Command, args []string) (err error) { "enc.key": setupCfg.EncKey, } - return process.SaveConfig(runCmd.Flags(), - filepath.Join(setupCfg.BasePath, "config.yaml"), o) + return process.SaveConfig(runCmd.Flags(), filepath.Join(setupDir, "config.yaml"), o) } func generateAWSKey() (key string, err error) { diff --git a/pkg/cfgstruct/bind.go b/pkg/cfgstruct/bind.go index 498d10f1d..a0f08ad4f 100644 --- a/pkg/cfgstruct/bind.go +++ b/pkg/cfgstruct/bind.go @@ -154,3 +154,16 @@ func bindConfig(flags FlagSet, prefix string, val reflect.Value, func expand(vars map[string]string, val string) string { return os.Expand(val, func(key string) string { return vars[key] }) } + +// FindConfigDirParam returns '--config-dir' param from os.Args (if exists) +func FindConfigDirParam() string { + // workaround to have early access to 'dir' param + for i, arg := range os.Args { + if strings.HasPrefix(arg, "--config-dir=") { + return strings.TrimPrefix(arg, "--config-dir=") + } else if arg == "--config-dir" && i < len(os.Args)-1 { + return os.Args[i+1] + } + } + return "" +} diff --git a/pkg/process/exec_conf.go b/pkg/process/exec_conf.go index 23de16b6d..037cc24b1 100644 --- a/pkg/process/exec_conf.go +++ b/pkg/process/exec_conf.go @@ -9,6 +9,7 @@ import ( "fmt" "os" "os/signal" + "path/filepath" "strings" "sync" "syscall" @@ -121,15 +122,19 @@ func cleanup(cmd *cobra.Command) { vip.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_")) vip.AutomaticEnv() - cfgFlag := cmd.Flags().Lookup("config") + logger, err := newLogger() + + cfgFlag := cmd.Flags().Lookup("config-dir") if cfgFlag != nil && cfgFlag.Value.String() != "" { - path := os.ExpandEnv(cfgFlag.Value.String()) - if cfgFlag.Changed || fileExists(path) { + path := filepath.Join(os.ExpandEnv(cfgFlag.Value.String()), "config.yaml") + if cmd.Annotations["type"] != "setup" || fileExists(path) { vip.SetConfigFile(path) err = vip.ReadInConfig() if err != nil { return err } + + logger.Sugar().Debug("Configuration loaded from: ", vip.ConfigFileUsed()) } } @@ -149,7 +154,6 @@ func cleanup(cmd *cobra.Command) { } } - logger, err := newLogger() if err != nil { return err }