Skip to content

Commit

Permalink
mc: Inherit global flags for initialization messages.
Browse files Browse the repository at this point in the history
Fixes #2089
  • Loading branch information
harshavardhana committed Jun 23, 2017
1 parent c32b1ea commit 5627174
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
35 changes: 31 additions & 4 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cmd

import (
"bytes"
"encoding/json"
"fmt"
"os"
"path/filepath"
Expand All @@ -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"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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()

Expand Down
6 changes: 3 additions & 3 deletions cmd/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())})
}
}

0 comments on commit 5627174

Please sign in to comment.