From 56271742630b9698832f8b859e53965feacc8450 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Wed, 21 Jun 2017 16:33:40 -0700 Subject: [PATCH] mc: Inherit global flags for initialization messages. Fixes #2089 --- cmd/main.go | 35 +++++++++++++++++++++++++++++++---- cmd/share.go | 6 +++--- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 932f87f0c5..6c2e681060 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -18,6 +18,7 @@ package cmd import ( "bytes" + "encoding/json" "fmt" "os" "path/filepath" @@ -28,6 +29,7 @@ import ( "time" "github.com/cheggaaa/pb" + "github.com/fatih/color" "github.com/minio/cli" "github.com/minio/mc/pkg/console" "github.com/minio/minio/pkg/probe" @@ -174,14 +176,39 @@ func getSystemData() map[string]string { } } +// initMessage represents encapsulation for `mc` +// initialization messages such as `config`, `share` +// etc. +// +// Also implements `message` compatible JSON and +// String methods. +type initMessage struct { + Status string `json:"status"` + Message string `json:"msg"` +} + +func (m initMessage) String() string { + return console.Colorize("Init", m.Message) +} + +func (m initMessage) JSON() string { + m.Status = "success" + jsonMessageBytes, e := json.Marshal(m) + fatalIf(probe.NewError(e), "Unable to marshal into JSON.") + + return string(jsonMessageBytes) +} + // initMC - initialize 'mc'. func initMC() { + console.SetColor("Init", color.New(color.FgGreen, color.Bold)) + // 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.") + printMsg(initMessage{Message: fmt.Sprintf("Configuration written to `" + mustGetMcConfigPath() + "`. Please update your access credentials.")}) } // Check if mc session folder exists. @@ -218,12 +245,12 @@ func registerBefore(ctx *cli.Context) error { // Migrate any old version of config / state files to newer format. migrate() - // Initialize default config files. - initMC() - // Set global flags. setGlobalsFromContext(ctx) + // Initialize default config files. + initMC() + // Check if config can be read. checkConfig() diff --git a/cmd/share.go b/cmd/share.go index c070847cbb..59d01613f5 100644 --- a/cmd/share.go +++ b/cmd/share.go @@ -178,20 +178,20 @@ func initShareConfig() { if !isShareDirExists() { fatalIf(createShareDir().Trace(mustGetShareDir()), "Failed to create share `"+mustGetShareDir()+"` folder.") - console.Infof("Successfully created `%s`.\n", mustGetShareDir()) + printMsg(initMessage{Message: fmt.Sprintf("Successfully created `%s`.", mustGetShareDir())}) } // Uploads share file. if !isShareUploadsExists() { fatalIf(initShareUploadsFile().Trace(getShareUploadsFile()), "Failed to initialize share uploads `"+getShareUploadsFile()+"` file.") - console.Infof("Initialized share uploads `%s` file.\n", getShareUploadsFile()) + printMsg(initMessage{Message: fmt.Sprintf("Initialized share uploads `%s` file.", getShareUploadsFile())}) } // Downloads share file. if !isShareDownloadsExists() { fatalIf(initShareDownloadsFile().Trace(getShareDownloadsFile()), "Failed to initialize share downloads `"+getShareDownloadsFile()+"` file.") - console.Infof("Initialized share downloads `%s` file.\n", getShareDownloadsFile()) + printMsg(initMessage{Message: fmt.Sprintf("Initialized share downloads `%s` file.", getShareDownloadsFile())}) } }