From 4eb519fa6ca8077c8595ca8e968f209fd089d3c3 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sun, 26 Jul 2015 00:16:27 -0700 Subject: [PATCH] Autogenerate config, for the first time no more config generate --- README.md | 8 ++++---- access-main.go | 3 --- cast-url.go | 3 --- cat-main.go | 3 --- cmd_test.go | 11 +++++++---- config-main.go | 26 +++----------------------- cp-url.go | 3 --- diff-main.go | 3 --- docs/config.md | 10 +++------- first-time.go | 13 +++++++++++-- ls-main.go | 3 --- main.go | 8 -------- mb-main.go | 3 --- session-v2.go | 4 ---- update-main.go | 3 --- 15 files changed, 28 insertions(+), 76 deletions(-) diff --git a/README.md b/README.md index f8b6df59..c44a06a7 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ cast Copy files and folders from a single source to many destinations diff Compute differences between two files or folders access Set access permissions - config Generate default configuration file [~/.mc/config.json] + config Add new alias to default configuration file [~/.mc/config.json] update Check for new software updates ``` @@ -26,7 +26,7 @@ Download ``mc`` from https://dl.minio.io:9000/updates/2015/Jun/linux-amd64/mc ~~~ $ wget https://dl.minio.io:9000/updates/2015/Jun/linux-amd64/mc $ chmod +x mc -$ ./mc config generate +$ ./mc ~~~ #### OS X @@ -36,7 +36,7 @@ Download ``mc`` from https://dl.minio.io:9000/updates/2015/Jun/darwin-amd64/mc ~~~ $ curl https://dl.minio.io:9000/updates/2015/Jun/darwin-amd64/mc > mc $ chmod +x mc -$ ./mc config generate +$ ./mc ~~~ #### Windows @@ -44,7 +44,7 @@ $ ./mc config generate Download ``mc`` from https://dl.minio.io:9000/updates/2015/Jun/windows-amd64/mc.exe ~~~ -C:\Users\Username\Downloads> mc.exe config generate +C:\Users\Username\Downloads> mc.exe ~~~ #### Source diff --git a/access-main.go b/access-main.go index 84418e2d..e04edd2f 100644 --- a/access-main.go +++ b/access-main.go @@ -64,9 +64,6 @@ func runAccessCmd(ctx *cli.Context) { if !ctx.Args().Present() || ctx.Args().First() == "help" { cli.ShowCommandHelpAndExit(ctx, "access", 1) // last argument is exit code } - if !isMcConfigExists() { - console.Fatalf("Please run \"mc config generate\". %s\n", errNotConfigured{}) - } config := mustGetMcConfig() acl := bucketACL(ctx.Args().First()) if !acl.isValidBucketACL() { diff --git a/cast-url.go b/cast-url.go index 08bfa247..b8a30af8 100644 --- a/cast-url.go +++ b/cast-url.go @@ -54,9 +54,6 @@ func checkCastSyntax(ctx *cli.Context) { if len(ctx.Args()) < 2 || ctx.Args().First() == "help" { cli.ShowCommandHelpAndExit(ctx, "cast", 1) // last argument is exit code. } - if !isMcConfigExists() { - console.Fatalf("Please run \"mc config generate\". %s\n", errNotConfigured{}) - } // extract URLs. URLs, err := args2URLs(ctx.Args()) if err != nil { diff --git a/cat-main.go b/cat-main.go index 29010f1d..5f887751 100644 --- a/cat-main.go +++ b/cat-main.go @@ -64,9 +64,6 @@ func runCatCmd(ctx *cli.Context) { if !ctx.Args().Present() || ctx.Args().First() == "help" { cli.ShowCommandHelpAndExit(ctx, "cat", 1) // last argument is exit code } - if !isMcConfigExists() { - console.Fatalf("Please run \"mc config generate\". %s\n", errNotConfigured{}) - } config := mustGetMcConfig() // Convert arguments to URLs: expand alias, fix format... for _, arg := range ctx.Args() { diff --git a/cmd_test.go b/cmd_test.go index 4f82c9a9..294fc72a 100644 --- a/cmd_test.go +++ b/cmd_test.go @@ -58,13 +58,16 @@ func (s *CmdTestSuite) SetUpSuite(c *C) { } setMcConfigDir(customConfigDir) - _, err = doConfig("generate", nil) + err = createMcConfigDir() c.Assert(err, IsNil) - _, err = doConfig("generate", nil) - c.Assert(err, Not(IsNil)) + config, err := newConfig() + c.Assert(err, IsNil) - _, err = doConfig("cover", nil) + err = writeConfig(config) + c.Assert(err, IsNil) + + _, err = doConfig("invalid", nil) c.Assert(err, Not(IsNil)) _, err = doConfig("alias", []string{"test", "https://test.io"}) diff --git a/config-main.go b/config-main.go index ba678214..265f776a 100644 --- a/config-main.go +++ b/config-main.go @@ -37,20 +37,16 @@ import ( // var configCmd = cli.Command{ Name: "config", - Usage: "Generate default configuration file [~/.mc/config.json]", + Usage: "Add new alias to default configuration file [~/.mc/config.json]", Action: runConfigCmd, CustomHelpTemplate: `NAME: mc {{.Name}} - {{.Usage}} USAGE: - mc {{.Name}}{{if .Flags}} [ARGS...]{{end}} generate mc {{.Name}}{{if .Flags}} [ARGS...]{{end}} alias NAME HOSTURL EXAMPLES: - 1. Generate mc config. - $ mc config generate - - 2. Add alias URLs. + 1. Add alias URLs. $ mc config alias zek https://s3.amazonaws.com/ `, @@ -80,19 +76,6 @@ func runConfigCmd(ctx *cli.Context) { // saveConfig writes configuration data in json format to config file. func saveConfig(arg string, aliases []string) error { switch arg { - case "generate": - if isMcConfigExists() { - return NewIodine(iodine.New(errConfigExists{}, nil)) - } - config, err := newConfig() - if err != nil { - return NewIodine(iodine.New(err, nil)) - } - err = writeConfig(config) - if err != nil { - return NewIodine(iodine.New(err, nil)) - } - return nil case "alias": config, err := addAlias(aliases) if err != nil { @@ -125,15 +108,12 @@ func doConfig(arg string, aliases []string) (string, error) { return "Alias [" + aliases[1] + "] is invalid URL", NewIodine(iodine.New(err, nil)) default: // unexpected error - return "Unable to generate config file [" + configPath + "].", NewIodine(iodine.New(err, nil)) + return "Unable to modify config file [" + configPath + "].", NewIodine(iodine.New(err, nil)) } } if arg == "alias" { return "Alias written to [" + configPath + "].", nil } - if arg == "generate" { - return "Configuration written to [" + configPath + "]. Please update your access credentials.", nil - } return "", NewIodine(iodine.New(errUnexpected{}, nil)) } diff --git a/cp-url.go b/cp-url.go index 58834abd..516846ce 100644 --- a/cp-url.go +++ b/cp-url.go @@ -61,9 +61,6 @@ func checkCopySyntax(ctx *cli.Context) { if len(ctx.Args()) < 2 || ctx.Args().First() == "help" { cli.ShowCommandHelpAndExit(ctx, "cp", 1) // last argument is exit code. } - if !isMcConfigExists() { - console.Fatalf("Please run \"mc config generate\". %s\n", errNotConfigured{}) - } // extract URLs. URLs, err := args2URLs(ctx.Args()) if err != nil { diff --git a/diff-main.go b/diff-main.go index 488bd734..40d661d7 100644 --- a/diff-main.go +++ b/diff-main.go @@ -56,9 +56,6 @@ func runDiffCmd(ctx *cli.Context) { if len(ctx.Args()) != 2 || ctx.Args().First() == "help" { cli.ShowCommandHelpAndExit(ctx, "diff", 1) // last argument is exit code } - if !isMcConfigExists() { - console.Fatalf("Please run \"mc config generate\". %s\n", errNotConfigured{}) - } config := mustGetMcConfig() firstURL := ctx.Args().First() diff --git a/docs/config.md b/docs/config.md index 612b3982..dae6215a 100644 --- a/docs/config.md +++ b/docs/config.md @@ -2,16 +2,12 @@ ```go NAME: - mc config - Generate configuration "${HOME}/.mc/config.json" file. + mc config - Add new alias to default configuration "~/.mc/config.json" file. USAGE: - mc config generate - mc config alias NAME HOSTURL + mc config alias NAME HOSTURL EXAMPLES: - 1. Generate mc config - $ mc config generate - - 2. Add alias URLs + 1. Add alias URLs $ mc config alias zek https://s3.amazonaws.com/ ``` diff --git a/first-time.go b/first-time.go index 00a606ca..6bf23621 100644 --- a/first-time.go +++ b/first-time.go @@ -56,12 +56,21 @@ func checkGolangVersion() { func firstTimeRun() { if !isMcConfigExists() { if err := createMcConfigDir(); err != nil { - console.Fatalf("Unable to create ‘mc’ folder. %s\n", err) + console.Fatalf("Unable to create ‘mc’ config folder. %s\n", err) } + config, err := newConfig() + if err != nil { + console.Fatalln(NewIodine(iodine.New(err, nil))) + } + err = writeConfig(config) + if err != nil { + console.Fatalln(NewIodine(iodine.New(err, nil))) + } + console.Infoln("Configuration written to [" + mustGetMcConfigPath() + "]. Please update your access credentials.") } if !isSessionDirExists() { if err := createSessionDir(); err != nil { - console.Fatalf("Unable to create session folder. %s\n", err) + console.Fatalf("Unable to create ‘mc’ session folder. %s\n", err) } } checkGolangVersion() diff --git a/ls-main.go b/ls-main.go index 067ff2a3..1f2f0f89 100644 --- a/ls-main.go +++ b/ls-main.go @@ -81,9 +81,6 @@ func runListCmd(ctx *cli.Context) { cli.ShowCommandHelpAndExit(ctx, "ls", 1) // last argument is exit code } - if !isMcConfigExists() { - console.Fatalf("Please run \"mc config generate\". %s\n", errNotConfigured{}) - } config := mustGetMcConfig() for _, arg := range args { targetURL, err := getExpandedURL(arg, config.Aliases) diff --git a/main.go b/main.go index ff6c03da..4c115673 100644 --- a/main.go +++ b/main.go @@ -104,13 +104,6 @@ func registerBefore(ctx *cli.Context) error { return nil } -func registerAfter(ctx *cli.Context) error { - if !isMcConfigExists() { - console.Fatalf("Please run \"mc config generate\". %s\n", errNotConfigured{}) - } - return nil -} - func registerApp() *cli.App { // Register all the commands registerCmd(lsCmd) // List contents of a bucket @@ -174,7 +167,6 @@ func main() { app := registerApp() app.Before = registerBefore - app.After = registerAfter app.ExtraInfo = func() map[string]string { if globalDebugFlag { return getSystemData() diff --git a/mb-main.go b/mb-main.go index d8ecc730..0dbb7ece 100644 --- a/mb-main.go +++ b/mb-main.go @@ -60,9 +60,6 @@ func runMakeBucketCmd(ctx *cli.Context) { if !ctx.Args().Present() || ctx.Args().First() == "help" { cli.ShowCommandHelpAndExit(ctx, "mb", 1) // last argument is exit code } - if !isMcConfigExists() { - console.Fatalf("Please run \"mc config generate\". %s\n", errNotConfigured{}) - } config := mustGetMcConfig() for _, arg := range ctx.Args() { targetURL, err := getExpandedURL(arg, config.Aliases) diff --git a/session-v2.go b/session-v2.go index f44cfabf..c668d9e3 100644 --- a/session-v2.go +++ b/session-v2.go @@ -63,10 +63,6 @@ type sessionV2 struct { // newSessionV2 provides a new session func newSessionV2() *sessionV2 { - if !isMcConfigExists() { - console.Fatalf("Please run \"mc config generate\". %s\n", errNotConfigured{}) - } - s := &sessionV2{} s.Header = &sessionV2Header{} s.Header.Version = "1.1.0" diff --git a/update-main.go b/update-main.go index faef9ec4..472d73b1 100644 --- a/update-main.go +++ b/update-main.go @@ -104,9 +104,6 @@ func runUpdateCmd(ctx *cli.Context) { if ctx.Args().First() == "help" { cli.ShowCommandHelpAndExit(ctx, "update", 1) // last argument is exit code } - if !isMcConfigExists() { - console.Fatalf("Please run \"mc config generate\". %s\n", NewIodine(iodine.New(errNotConfigured{}, nil))) - } msg, err := doUpdateCheck() if err != nil { console.Fatalln(msg)