Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for custom --config dir #874

Merged
merged 1 commit into from
Jul 13, 2015
Merged
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
6 changes: 6 additions & 0 deletions cmd-options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
23 changes: 14 additions & 9 deletions cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
}
Expand Down
32 changes: 21 additions & 11 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -26,7 +26,7 @@ var (
mcCurrentConfigVersion = "1.0.0"
)

// mc configuration related constants
// mc configuration related constants.
const (
mcConfigDir = ".mc/"
mcConfigWindowsDir = "mc\\"
Expand Down
15 changes: 10 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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")
Expand Down