From d32f90fe9590eef092bb8e20fc734d17a069c334 Mon Sep 17 00:00:00 2001 From: kannappanr <30541348+kannappanr@users.noreply.github.com> Date: Wed, 28 Feb 2018 20:13:33 -0800 Subject: [PATCH] Honor global flags irrespective of the position. (#5486) Flags like `json, config-dir, quiet` are now honored even if they are between minio and gateway in the cli, like, `minio --json gateway s3`. Fixes #5403 --- cmd/common-main.go | 41 +++++++++++++++++++++++++++-------------- cmd/config-dir.go | 10 ++++++---- cmd/gateway-main.go | 6 +++--- cmd/main.go | 10 ++++++++-- cmd/server-main.go | 4 ++-- 5 files changed, 46 insertions(+), 25 deletions(-) diff --git a/cmd/common-main.go b/cmd/common-main.go index 10371e70b..4668fb58f 100644 --- a/cmd/common-main.go +++ b/cmd/common-main.go @@ -1,5 +1,5 @@ /* - * Minio Cloud Storage, (C) 2017 Minio, Inc. + * Minio Cloud Storage, (C) 2017, 2018 Minio, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -51,23 +51,36 @@ func initConfig() { } func handleCommonCmdArgs(ctx *cli.Context) { - // Set configuration directory. - { - // Get configuration directory from command line argument. - configDir := ctx.String("config-dir") - if !ctx.IsSet("config-dir") && ctx.GlobalIsSet("config-dir") { - configDir = ctx.GlobalString("config-dir") + + var configDir string + + if ctx.IsSet("config-dir") { + configDir = ctx.String("config-dir") + } else if ctx.GlobalIsSet("config-dir") { + configDir = ctx.GlobalString("config-dir") + // cli package does not expose parent's "config-dir" option. Below code is workaround. + if configDir == "" || configDir == getConfigDir() { + if ctx.Parent().GlobalIsSet("config-dir") { + configDir = ctx.Parent().GlobalString("config-dir") + } } + } else { + // Neither local nor global config-dir option is provided. In this case, try to use + // default config directory. + configDir = getConfigDir() if configDir == "" { - fatalIf(errors.New("empty directory"), "Configuration directory cannot be empty.") + fatalIf(errors.New("missing option"), "config-dir option must be provided.") } - - // Disallow relative paths, figure out absolute paths. - configDirAbs, err := filepath.Abs(configDir) - fatalIf(err, "Unable to fetch absolute path for config directory %s", configDir) - - setConfigDir(configDirAbs) } + + if configDir == "" { + fatalIf(errors.New("empty directory"), "Configuration directory cannot be empty.") + } + + // Disallow relative paths, figure out absolute paths. + configDirAbs, err := filepath.Abs(configDir) + fatalIf(err, "Unable to fetch absolute path for config directory %s", configDir) + setConfigDir(configDirAbs) } func handleCommonEnvVars() { diff --git a/cmd/config-dir.go b/cmd/config-dir.go index dc95f73ae..6a6265932 100644 --- a/cmd/config-dir.go +++ b/cmd/config-dir.go @@ -1,5 +1,5 @@ /* - * Minio Cloud Storage, (C) 2015, 2016, 2017 Minio, Inc. + * Minio Cloud Storage, (C) 2015, 2016, 2017, 2018 Minio, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -95,14 +95,16 @@ func (config *ConfigDir) GetPrivateKeyFile() string { return filepath.Join(config.getCertsDir(), privateKeyFile) } -func mustGetDefaultConfigDir() string { +func getDefaultConfigDir() string { homeDir, err := homedir.Dir() - fatalIf(err, "Unable to get home directory.") + if err != nil { + return "" + } return filepath.Join(homeDir, defaultMinioConfigDir) } -var configDir = &ConfigDir{dir: mustGetDefaultConfigDir()} +var configDir = &ConfigDir{dir: getDefaultConfigDir()} func setConfigDir(dir string) { configDir.Set(dir) diff --git a/cmd/gateway-main.go b/cmd/gateway-main.go index af400c886..cd6d69222 100644 --- a/cmd/gateway-main.go +++ b/cmd/gateway-main.go @@ -1,5 +1,5 @@ /* - * Minio Cloud Storage, (C) 2017 Minio, Inc. + * Minio Cloud Storage, (C) 2017, 2018 Minio, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -114,13 +114,13 @@ func StartGateway(ctx *cli.Context, gw Gateway) { // Get "json" flag from command line argument and // enable json and quite modes if jason flag is turned on. - jsonFlag := ctx.Bool("json") || ctx.GlobalBool("json") + jsonFlag := ctx.IsSet("json") || ctx.GlobalIsSet("json") if jsonFlag { log.EnableJSON() } // Get quiet flag from command line argument. - quietFlag := ctx.Bool("quiet") || ctx.GlobalBool("quiet") + quietFlag := ctx.IsSet("quiet") || ctx.GlobalIsSet("quiet") if quietFlag { log.EnableQuiet() } diff --git a/cmd/main.go b/cmd/main.go index 5863f4f41..e2a5a9e07 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,5 +1,5 @@ /* - * Minio Cloud Storage, (C) 2015, 2016, 2017 Minio, Inc. + * Minio Cloud Storage, (C) 2015, 2016, 2017, 2018 Minio, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,7 +32,13 @@ var globalFlags = []cli.Flag{ cli.StringFlag{ Name: "config-dir, C", Value: getConfigDir(), - Usage: "Path to configuration directory.", + Usage: func() string { + usage := "Path to configuration directory." + if getConfigDir() == "" { + usage = usage + " This option must be set." + } + return usage + }(), }, cli.BoolFlag{ Name: "quiet", diff --git a/cmd/server-main.go b/cmd/server-main.go index 091f153c1..cb0f4a509 100644 --- a/cmd/server-main.go +++ b/cmd/server-main.go @@ -156,13 +156,13 @@ func serverMain(ctx *cli.Context) { // Get "json" flag from command line argument and // enable json and quite modes if jason flag is turned on. - jsonFlag := ctx.Bool("json") || ctx.GlobalBool("json") + jsonFlag := ctx.IsSet("json") || ctx.GlobalIsSet("json") if jsonFlag { log.EnableJSON() } // Get quiet flag from command line argument. - quietFlag := ctx.Bool("quiet") || ctx.GlobalBool("quiet") + quietFlag := ctx.IsSet("quiet") || ctx.GlobalIsSet("quiet") if quietFlag { log.EnableQuiet() }