args: Honor config-dir & quiet wherever they are (#3356)

setGlobalsFromContext() is added to set global variables after parsing
command line arguments. Thus, global flags will be honored wherever
they are placed in minio command.
This commit is contained in:
Anis Elleuch 2016-11-28 21:15:36 +01:00 committed by Harshavardhana
parent 9ccfb70104
commit 01f625824a
5 changed files with 103 additions and 70 deletions

View File

@ -22,6 +22,8 @@ import (
humanize "github.com/dustin/go-humanize" humanize "github.com/dustin/go-humanize"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/minio/cli"
"github.com/minio/mc/pkg/console"
"github.com/minio/minio/pkg/objcache" "github.com/minio/minio/pkg/objcache"
) )
@ -53,11 +55,12 @@ const (
) )
var ( var (
globalQuiet = false // Quiet flag set via command line. globalQuiet = false // quiet flag set via command line.
globalIsDistXL = false // "Is Distributed?" flag. globalConfigDir = mustGetConfigPath() // config-dir flag set via command line
// Add new global flags here. // Add new global flags here.
globalIsDistXL = false // "Is Distributed?" flag.
// Maximum cache size. // Maximum cache size.
globalMaxCacheSize = uint64(maxCacheSize) globalMaxCacheSize = uint64(maxCacheSize)
// Cache expiry. // Cache expiry.
@ -90,3 +93,19 @@ var (
colorBlue = color.New(color.FgBlue).SprintfFunc() colorBlue = color.New(color.FgBlue).SprintfFunc()
colorGreen = color.New(color.FgGreen).SprintfFunc() colorGreen = color.New(color.FgGreen).SprintfFunc()
) )
// Parse command arguments and set global variables accordingly
func setGlobalsFromContext(c *cli.Context) {
// Set config dir
switch {
case c.IsSet("config-dir"):
globalConfigDir = c.String("config-dir")
case c.GlobalIsSet("config-dir"):
globalConfigDir = c.GlobalString("config-dir")
}
if globalConfigDir == "" {
console.Fatalf("Unable to get config file. Config directory is empty.")
}
// Set global quiet flag.
globalQuiet = c.Bool("quiet") || c.GlobalBool("quiet")
}

View File

@ -17,7 +17,6 @@
package cmd package cmd
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"sort" "sort"
@ -148,19 +147,26 @@ func checkMainSyntax(c *cli.Context) {
} }
} }
// Main main for minio server. // Check for updates and print a notification message
func Main() { func checkUpdate() {
app := registerApp() // Do not print update messages, if quiet flag is set.
app.Before = func(c *cli.Context) error { if !globalQuiet {
configDir := c.GlobalString("config-dir") updateMsg, _, err := getReleaseUpdate(minioUpdateStableURL, 1*time.Second)
if configDir == "" { if err != nil {
fatalIf(errors.New("Config directory is empty"), "Unable to get config file.") // Ignore any errors during getReleaseUpdate(), possibly
// because of network errors.
return
}
if updateMsg.Update {
console.Println(updateMsg)
}
}
} }
// Sets new config directory.
setGlobalConfigPath(configDir)
// Valid input arguments to main. // Generic Minio initialization to create/load config, prepare loggers, etc..
checkMainSyntax(c) func minioInit() {
// Sets new config directory.
setGlobalConfigPath(globalConfigDir)
// Migrate any old version of config / state files to newer format. // Migrate any old version of config / state files to newer format.
migrate() migrate()
@ -197,23 +203,14 @@ func Main() {
// Init the error tracing module. // Init the error tracing module.
initError() initError()
// Set global quiet flag. }
globalQuiet = c.Bool("quiet") || c.GlobalBool("quiet")
// Do not print update messages, if quiet flag is set. // Main main for minio server.
if !globalQuiet { func Main() {
if c.Args().Get(0) != "update" { app := registerApp()
updateMsg, _, err := getReleaseUpdate(minioUpdateStableURL, 1*time.Second) app.Before = func(c *cli.Context) error {
if err != nil { // Valid input arguments to main.
// Ignore any errors during getReleaseUpdate(), possibly checkMainSyntax(c)
// because of network errors.
return nil
}
if updateMsg.Update {
console.Println(updateMsg)
}
}
}
return nil return nil
} }

View File

@ -363,8 +363,13 @@ func serverMain(c *cli.Context) {
cli.ShowCommandHelpAndExit(c, "server", 1) cli.ShowCommandHelpAndExit(c, "server", 1)
} }
// Set global quiet flag. // Set global variables after parsing passed arguments
globalQuiet = c.Bool("quiet") || c.GlobalBool("quiet") setGlobalsFromContext(c)
// Initialization routine, such as config loading, enable logging, ..
minioInit()
checkUpdate()
// Server address. // Server address.
serverAddr := c.String("address") serverAddr := c.String("address")

View File

@ -265,8 +265,14 @@ func getReleaseUpdate(updateURL string, duration time.Duration) (updateMsg updat
// main entry point for update command. // main entry point for update command.
func mainUpdate(ctx *cli.Context) { func mainUpdate(ctx *cli.Context) {
// Set global quiet flag.
if ctx.Bool("quiet") || ctx.GlobalBool("quiet") { // Set global variables after parsing passed arguments
setGlobalsFromContext(ctx)
// Initialization routine, such as config loading, enable logging, ..
minioInit()
if globalQuiet {
return return
} }

View File

@ -43,8 +43,14 @@ func mainVersion(ctx *cli.Context) {
if len(ctx.Args()) != 0 { if len(ctx.Args()) != 0 {
cli.ShowCommandHelpAndExit(ctx, "version", 1) cli.ShowCommandHelpAndExit(ctx, "version", 1)
} }
// Set global quiet flag.
if ctx.Bool("quiet") || ctx.GlobalBool("quiet") { // Set global variables after parsing passed arguments
setGlobalsFromContext(ctx)
// Initialization routine, such as config loading, enable logging, ..
minioInit()
if globalQuiet {
return return
} }