func runAccessCmd(ctx *cli.Context) { if !ctx.Args().Present() || ctx.Args().First() == "help" { cli.ShowCommandHelpAndExit(ctx, "access", 1) // last argument is exit code } config := mustGetMcConfig() acl := bucketACL(ctx.Args().First()) if !acl.isValidBucketACL() { console.Fatalf("Valid types are [private, public, readonly]. %s\n", errInvalidACL{acl: acl.String()}) } for _, arg := range ctx.Args().Tail() { targetURL, err := getExpandedURL(arg, config.Aliases) if err != nil { switch e := iodine.ToError(err).(type) { case errUnsupportedScheme: console.Fatalf("Unknown type of URL %s. %s\n", e.url, err) default: console.Fatalf("Unable to parse argument %s. %s\n", arg, err) } } msg, err := doUpdateAccessCmd(targetURL, acl) if err != nil { console.Fatalln(msg) } console.Infoln(msg) } }
// runMakeBucketCmd is the handler for mc mb command 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) if err != nil { switch e := iodine.ToError(err).(type) { case errUnsupportedScheme: console.Fatalf("Unknown type of URL %s. %s\n", e.url, err) default: console.Fatalf("Unable to parse argument %s. %s\n", arg, err) } } msg, err := doMakeBucketCmd(targetURL) if err != nil { console.Fatalln(msg) } console.Infoln(msg) } }
// initMC - initialize 'mc'. func initMC() { // Check if mc config exists. if !isMcConfigExists() { err := saveMcConfig(newMcConfig()) fatalIf(err.Trace(), "Unable to save new mc config.") console.Infoln("Configuration written to ‘" + mustGetMcConfigPath() + "’. Please update your access credentials.") } // Check if mc session folder exists. if !isSessionDirExists() { fatalIf(createSessionDir().Trace(), "Unable to create session config folder.") } // Check if mc share folder exists. if !isShareDirExists() { initShareConfig() } // Check if certs dir exists if !isCertsDirExists() { fatalIf(createCertsDir().Trace(), "Unable to create `CAs` folder.") } // Check if CAs dir exists if !isCAsDirExists() { fatalIf(createCAsDir().Trace(), "Unable to create `CAs` folder.") } // Load all authority certificates present in CAs dir loadRootCAs() }
// If the host key does not have http(s), fix it. func fixConfigV6ForHosts() { if !isMcConfigExists() { return } brokenMcCfgV6, e := quick.Load(mustGetMcConfigPath(), newConfigV6()) fatalIf(probe.NewError(e), "Unable to load config.") if brokenMcCfgV6.Version() != "6" { return } newCfgV6 := newConfigV6() isMutated := false // Copy aliases. for k, v := range brokenMcCfgV6.Data().(*configV6).Aliases { newCfgV6.Aliases[k] = v } url := &clientURL{} // Copy hosts. for host, hostCfgV6 := range brokenMcCfgV6.Data().(*configV6).Hosts { // Already fixed - Copy and move on. if strings.HasPrefix(host, "https") || strings.HasPrefix(host, "http") { newCfgV6.Hosts[host] = hostCfgV6 continue } // If host entry does not contain "http(s)", introduce a new entry and delete the old one. if host == "s3.amazonaws.com" || host == "storage.googleapis.com" || host == "localhost:9000" || host == "127.0.0.1:9000" || host == "play.minio.io:9000" || host == "dl.minio.io:9000" { console.Infoln("Found broken host entries, replacing " + host + " with https://" + host + ".") url.Host = host url.Scheme = "https" url.SchemeSeparator = "://" newCfgV6.Hosts[url.String()] = hostCfgV6 isMutated = true continue } } if isMutated { // Save the new config back to the disk. mcCfgV6, e := quick.New(newCfgV6) fatalIf(probe.NewError(e), "Unable to initialize quick config for config version ‘v6’.") e = mcCfgV6.Save(mustGetMcConfigPath()) fatalIf(probe.NewError(e), "Unable to save config version ‘v6’.") } }
// runUpdateCmd - func runUpdateCmd(ctx *cli.Context) { if ctx.Args().First() == "help" { cli.ShowCommandHelpAndExit(ctx, "update", 1) // last argument is exit code } msg, err := doUpdateCheck() if err != nil { console.Fatalln(msg) } // no msg do not print one if msg != "" { console.Infoln(msg) } }
// runUpdateCmd - 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) } // no msg do not print one if msg != "" { console.Infoln(msg) } }
// runConfigCmd is the handle for "mc config" sub-command func runConfigCmd(ctx *cli.Context) { // show help if nothing is set if !ctx.Args().Present() || ctx.Args().First() == "help" { cli.ShowCommandHelpAndExit(ctx, "config", 1) // last argument is exit code } arg := ctx.Args().First() tailArgs := ctx.Args().Tail() if len(tailArgs) > 2 { console.Fatalf("Incorrect number of arguments, please use \"mc config help\". %s", errInvalidArgument{}) } msg, err := doConfig(arg, tailArgs) if err != nil { console.Fatalln(msg) } console.Infoln(msg) }
func verifyMCRuntime() { if !isMcConfigExists() { err := createMcConfigDir() fatalIf(err.Trace(), "Unable to create ‘mc’ config folder.") config, err := newConfig() fatalIf(err.Trace(), "Unable to initialize newConfig.") err = writeConfig(config) fatalIf(err.Trace(), "Unable to write newConfig.") console.Infoln("Configuration written to [" + mustGetMcConfigPath() + "]. Please update your access credentials.") } if !isSessionDirExists() { fatalIf(createSessionDir().Trace(), "Unable to create session dir.") } checkGolangRuntimeVersion() }
// runDiffCmd - is a handler for mc diff command 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() secondURL := ctx.Args()[1] var err error firstURL, err = getExpandedURL(firstURL, config.Aliases) if err != nil { switch e := iodine.ToError(err).(type) { case errUnsupportedScheme: console.Fatalf("Unknown type of URL %s. %s\n", e.url, err) default: console.Fatalf("Unable to parse argument %s. %s\n", firstURL, err) } } secondURL, err = getExpandedURL(secondURL, config.Aliases) if err != nil { switch e := iodine.ToError(err).(type) { case errUnsupportedScheme: console.Fatalf("Unknown type of URL %s. %s\n", e.url, err) default: console.Fatalf("Unable to parse argument %s. %s\n", secondURL, err) } } if isURLRecursive(secondURL) { console.Fatalf("Second URL cannot be recursive. %s\n", errInvalidArgument{}) } newFirstURL := stripRecursiveURL(firstURL) for diff := range doDiffCmd(newFirstURL, secondURL, isURLRecursive(firstURL)) { if diff.err != nil { console.Fatalln(diff.message) } console.Infoln(diff.message) } }
// verifyMCRuntime - verify 'mc' compiled runtime version. func verifyMCRuntime() { checkGolangRuntimeVersion() // Check if mc config exists. if !isMcConfigExists() { err := saveMcConfig(newMcConfig()) fatalIf(err.Trace(), "Unable to save new mc config.") console.Infoln("Configuration written to ‘" + mustGetMcConfigPath() + "’. Please update your access credentials.") } // Check if mc session folder exists. if !isSessionDirExists() { fatalIf(createSessionDir().Trace(), "Unable to create session config folder.") } // Check if mc share folder exists. if !isShareDirExists() { initShareConfig() } }
func firstTimeRun() { if !isMcConfigExists() { if err := createMcConfigDir(); err != nil { 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 ‘mc’ session folder. %s\n", err) } } checkGolangVersion() }
// fixConfigV6 - fix all the unnecessary glob URLs present in existing config version 6. func fixConfigV6() { if !isMcConfigExists() { return } config, e := quick.New(newConfigV6()) fatalIf(probe.NewError(e), "Unable to initialize config.") e = config.Load(mustGetMcConfigPath()) fatalIf(probe.NewError(e).Trace(mustGetMcConfigPath()), "Unable to load config.") if config.Data().(*configV6).Version != "6" { return } newConfig := new(configV6) isMutated := false newConfig.Aliases = make(map[string]string) newConfig.Hosts = make(map[string]hostConfigV6) newConfig.Version = "6" newConfig.Aliases = config.Data().(*configV6).Aliases for host, hostCfg := range config.Data().(*configV6).Hosts { if strings.Contains(host, "*") { fatalIf(errInvalidArgument(), fmt.Sprintf("Glob style ‘*’ pattern matching is no longer supported. Please fix ‘%s’ entry manually.", host)) } if strings.Contains(host, "*s3*") || strings.Contains(host, "*.s3*") { console.Infoln("Found glob url, replacing " + host + " with s3.amazonaws.com") newConfig.Hosts["s3.amazonaws.com"] = hostCfg isMutated = true continue } if strings.Contains(host, "s3*") { console.Infoln("Found glob url, replacing " + host + " with s3.amazonaws.com") newConfig.Hosts["s3.amazonaws.com"] = hostCfg isMutated = true continue } if strings.Contains(host, "*amazonaws.com") || strings.Contains(host, "*.amazonaws.com") { console.Infoln("Found glob url, replacing " + host + " with s3.amazonaws.com") newConfig.Hosts["s3.amazonaws.com"] = hostCfg isMutated = true continue } if strings.Contains(host, "*storage.googleapis.com") { console.Infoln("Found glob url, replacing " + host + " with storage.googleapis.com") newConfig.Hosts["storage.googleapis.com"] = hostCfg isMutated = true continue } if strings.Contains(host, "localhost:*") { console.Infoln("Found glob url, replacing " + host + " with localhost:9000") newConfig.Hosts["localhost:9000"] = hostCfg isMutated = true continue } if strings.Contains(host, "127.0.0.1:*") { console.Infoln("Found glob url, replacing " + host + " with 127.0.0.1:9000") newConfig.Hosts["127.0.0.1:9000"] = hostCfg isMutated = true continue } // Other entries are hopefully OK. Copy them blindly. newConfig.Hosts[host] = hostCfg } if isMutated { newConf, e := quick.New(newConfig) fatalIf(probe.NewError(e), "Unable to initialize newly fixed config.") e = newConf.Save(mustGetMcConfigPath()) fatalIf(probe.NewError(e).Trace(mustGetMcConfigPath()), "Unable to save newly fixed config path.") console.Infof("Successfully fixed %s broken config for version ‘6’.\n", mustGetMcConfigPath()) } }
// String printer for SessionV2 func (s sessionV2) Info() { console.Infoln("Session safely terminated. To resume session ‘mc session resume " + s.SessionID + "’") }