diff --git a/cmd-options.go b/cmd-options.go index 4fca35628b..b424b16cf5 100644 --- a/cmd-options.go +++ b/cmd-options.go @@ -31,6 +31,12 @@ var commands = []cli.Command{} var flags = []cli.Flag{} var ( + configFlag = cli.StringFlag{ + Name: "config, C", + Value: mustGetMcConfigDir(), + Usage: "Path to configuration directory", + } + quietFlag = cli.BoolFlag{ Name: "quiet, q", Usage: "Suppress chatty console output", diff --git a/cmd_test.go b/cmd_test.go index 83e3b4ff05..5dc669be3c 100644 --- a/cmd_test.go +++ b/cmd_test.go @@ -32,26 +32,31 @@ import ( "github.com/minio/mc/pkg/quick" ) +var customConfigDir string + func Test(t *testing.T) { TestingT(t) } type CmdTestSuite struct{} var _ = Suite(&CmdTestSuite{}) -func mustGetMcConfigDir() string { - dir, _ := getMcConfigDir() - return dir -} - var server *httptest.Server func (s *CmdTestSuite) SetUpSuite(c *C) { // do not set it elsewhere, leads to data races since this is a global flag globalQuietFlag = true - configDir, err := ioutil.TempDir(os.TempDir(), "cmd-") + tmpDir, err := ioutil.TempDir(os.TempDir(), "cmd-") c.Assert(err, IsNil) - customConfigDir = configDir + + // For windows the path is slightly different. + if runtime.GOOS == "windows" { + customConfigDir = filepath.Join(tmpDir, mcConfigWindowsDir) + + } else { + customConfigDir = filepath.Join(tmpDir, mcConfigDir) + } + setMcConfigDir(customConfigDir) _, err = doConfig("generate", nil) c.Assert(err, IsNil) @@ -158,9 +163,9 @@ func (s *CmdTestSuite) TestGetMcConfigDir(c *C) { case "freebsd": fallthrough case "darwin": - c.Assert(dir, Equals, filepath.Join(customConfigDir, mcConfigDir)) + c.Assert(dir, Equals, customConfigDir) case "windows": - c.Assert(dir, Equals, filepath.Join(customConfigDir, mcConfigWindowsDir)) + c.Assert(dir, Equals, customConfigDir) default: c.Fail() } diff --git a/config.go b/config.go index cc876235ea..24e20f8784 100644 --- a/config.go +++ b/config.go @@ -23,6 +23,7 @@ import ( "runtime" "sync" + "github.com/minio/mc/pkg/console" "github.com/minio/mc/pkg/quick" "github.com/minio/minio/pkg/iodine" ) @@ -36,20 +37,20 @@ type configV1 struct { // cached variables should *NEVER* be accessed directly from outside this file. var cache sync.Pool -// customConfigDir used internally only by test functions -var customConfigDir string +// customConfigDir contains the whole path to config dir. Only access via get/set functions. +var mcCustomConfigDir string -// getMcConfigDir - construct minio client config folder +// setMcConfigDir - construct minio client config folder. +func setMcConfigDir(configDir string) { + mcCustomConfigDir = configDir +} + +// getMcConfigDir - construct minio client config folder. func getMcConfigDir() (string, error) { - if customConfigDir != "" { - // For windows the path is slightly different - switch runtime.GOOS { - case "windows": - return filepath.Join(customConfigDir, mcConfigWindowsDir), nil - default: - return filepath.Join(customConfigDir, mcConfigDir), nil - } + if mcCustomConfigDir != "" { + return mcCustomConfigDir, nil } + u, err := user.Current() if err != nil { return "", iodine.New(err, nil) @@ -63,6 +64,15 @@ func getMcConfigDir() (string, error) { } } +// mustGetMcConfigDir - construct minio client config folder or fail +func mustGetMcConfigDir() (configDir string) { + configDir, err := getMcConfigDir() + if err != nil { + console.Fatalf("Unable to determine default configuration folder. %s\n", err) + } + return configDir +} + // createMcConfigDir - create minio client config folder func createMcConfigDir() error { p, err := getMcConfigDir() diff --git a/globals.go b/globals.go index 085b49879e..cb5a3aada7 100644 --- a/globals.go +++ b/globals.go @@ -14,7 +14,7 @@ * limitations under the License. */ -// This package contains all the global variables and constants +// This package contains all the global variables and constants. ONLY TO BE ACCESSED VIA GET/SET FUNCTIONS. package main var ( @@ -26,7 +26,7 @@ var ( mcCurrentConfigVersion = "1.0.0" ) -// mc configuration related constants +// mc configuration related constants. const ( mcConfigDir = ".mc/" mcConfigWindowsDir = "mc\\" diff --git a/main.go b/main.go index c122fd81f0..466b84dce7 100644 --- a/main.go +++ b/main.go @@ -103,11 +103,12 @@ func main() { registerCmd(updateCmd) // update Check for new software updates // register all the flags - registerFlag(quietFlag) // suppress console output - registerFlag(aliasFlag) // OS toolchain mimic - registerFlag(themeFlag) // console theme flag - registerFlag(jsonFlag) // json formatted output - registerFlag(debugFlag) // enable debugging output + registerFlag(configFlag) // path to config folder + registerFlag(quietFlag) // suppress console output + registerFlag(aliasFlag) // OS toolchain mimic + registerFlag(themeFlag) // console theme flag + registerFlag(jsonFlag) // json formatted output + registerFlag(debugFlag) // enable debugging output app := cli.NewApp() app.Usage = "Minio Client for object storage and filesystems" @@ -117,6 +118,10 @@ func main() { app.Flags = flags app.Author = "Minio.io" app.Before = func(ctx *cli.Context) error { + if ctx.GlobalString("config") != "" { + setMcConfigDir(ctx.GlobalString("config")) + } + globalQuietFlag = ctx.GlobalBool("quiet") globalAliasFlag = ctx.GlobalBool("alias") globalDebugFlag = ctx.GlobalBool("debug")