From 334e864eea5ab6887e76d159e38c2f587fa549ba Mon Sep 17 00:00:00 2001 From: "Anand Babu (AB) Periasamy" Date: Sat, 7 Nov 2015 16:31:19 -0800 Subject: [PATCH] Prints cleanup --- access-main.go | 12 ++++++------ config-alias-main.go | 25 +++++++------------------ config-host-main.go | 24 +++++++----------------- cp-main.go | 10 +++++----- diff-main.go | 2 +- ls.go | 4 ++-- mb-main.go | 33 ++++++++++++--------------------- mb_access_test.go | 22 ++++++++++++++-------- mirror-main.go | 10 +++++----- print.go | 14 +++++++------- rm-main.go | 2 +- session-main.go | 20 +++++++------------- share-download-main.go | 2 +- share-upload-main.go | 2 +- update-main.go | 4 ++-- 15 files changed, 78 insertions(+), 108 deletions(-) diff --git a/access-main.go b/access-main.go index 3fa621c9..e79838c0 100644 --- a/access-main.go +++ b/access-main.go @@ -59,8 +59,8 @@ EXAMPLES: `, } -// AccessMessage is container for access command on bucket success and failure messages -type AccessMessage struct { +// accessMessage is container for access command on bucket success and failure messages +type accessMessage struct { Operation string `json:"operation"` Status string `json:"status"` Bucket string `json:"bucket"` @@ -68,7 +68,7 @@ type AccessMessage struct { } // String colorized access message -func (s AccessMessage) String() string { +func (s accessMessage) String() string { if s.Operation == "set" { return console.Colorize("Access", "Set access permission ‘"+string(s.Perms)+"’ updated successfully for ‘"+s.Bucket+"’") } @@ -80,7 +80,7 @@ func (s AccessMessage) String() string { } // JSON jsonified access message -func (s AccessMessage) JSON() string { +func (s accessMessage) JSON() string { accessJSONBytes, err := json.Marshal(s) fatalIf(probe.NewError(err), "Unable to marshal into JSON.") @@ -146,7 +146,7 @@ func mainAccess(ctx *cli.Context) { fatalIf(doSetAccess(targetURL, perms).Trace(targetURL, string(perms)), "Unable to set access permission ‘"+string(perms)+"’ for ‘"+targetURL+"’.") - Prints("%s\n", AccessMessage{ + printMsg(accessMessage{ Operation: "set", Status: "success", Bucket: targetURL, @@ -159,7 +159,7 @@ func mainAccess(ctx *cli.Context) { perms, err := doGetAccess(targetURL) fatalIf(err.Trace(targetURL), "Unable to get access permission for ‘"+targetURL+"’.") - Prints("%s\n", AccessMessage{ + printMsg(accessMessage{ Operation: "get", Status: "success", Bucket: targetURL, diff --git a/config-alias-main.go b/config-alias-main.go index fcd54493..73fe4368 100644 --- a/config-alias-main.go +++ b/config-alias-main.go @@ -56,15 +56,15 @@ EXAMPLES: `, } -// AliasMessage container for content message structure -type AliasMessage struct { +// aliasMessage container for content message structure +type aliasMessage struct { op string Alias string `json:"alias"` URL string `json:"url,omitempty"` } // String colorized alias message -func (a AliasMessage) String() string { +func (a aliasMessage) String() string { if a.op == "list" { message := console.Colorize("Alias", fmt.Sprintf("[%s] <- ", a.Alias)) message += console.Colorize("URL", fmt.Sprintf("%s", a.URL)) @@ -81,7 +81,7 @@ func (a AliasMessage) String() string { } // JSON jsonified alias message -func (a AliasMessage) JSON() string { +func (a aliasMessage) JSON() string { jsonMessageBytes, e := json.Marshal(a) fatalIf(probe.NewError(e), "Unable to marshal into JSON.") @@ -165,11 +165,7 @@ func listAliases() { // convert interface{} back to its original struct newConf := config.Data().(*configV5) for k, v := range newConf.Aliases { - Prints("%s\n", AliasMessage{ - op: "list", - Alias: k, - URL: v, - }) + printMsg(aliasMessage{op: "list", Alias: k, URL: v}) } } @@ -200,10 +196,7 @@ func removeAlias(alias string) { err = writeConfig(newConfig) fatalIf(err.Trace(alias), "Unable to save alias ‘"+alias+"’.") - Prints("%s\n", AliasMessage{ - op: "remove", - Alias: alias, - }) + printMsg(aliasMessage{op: "remove", Alias: alias}) } // addAlias - add new aliases @@ -236,9 +229,5 @@ func addAlias(alias, url string) { err = writeConfig(newConfig) fatalIf(err.Trace(alias, url), "Unable to save alias ‘"+alias+"’.") - Prints("%s\n", AliasMessage{ - op: "add", - Alias: alias, - URL: url, - }) + printMsg(aliasMessage{op: "add", Alias: alias, URL: url}) } diff --git a/config-host-main.go b/config-host-main.go index 79db86ad..8309cb7a 100644 --- a/config-host-main.go +++ b/config-host-main.go @@ -45,8 +45,8 @@ EXAMPLES: `, } -// HostMessage container for content message structure -type HostMessage struct { +// hostMessage container for content message structure +type hostMessage struct { op string Host string `json:"host"` AccessKeyID string `json:"accessKeyId,omitempty"` @@ -55,7 +55,7 @@ type HostMessage struct { } // String colorized host message -func (a HostMessage) String() string { +func (a hostMessage) String() string { if a.op == "list" { message := console.Colorize("Host", fmt.Sprintf("[%s] ", a.Host)) if a.AccessKeyID != "" || a.SecretAccessKey != "" { @@ -76,7 +76,7 @@ func (a HostMessage) String() string { } // JSON jsonified host message -func (a HostMessage) JSON() string { +func (a hostMessage) JSON() string { jsonMessageBytes, e := json.Marshal(a) fatalIf(probe.NewError(e), "Unable to marshal into JSON.") @@ -163,15 +163,8 @@ func listHosts() { // convert interface{} back to its original struct newConf := config.Data().(*configV5) for k, v := range newConf.Hosts { - Prints("%s\n", HostMessage{ - op: "list", - Host: k, - AccessKeyID: v.AccessKeyID, - SecretAccessKey: v.SecretAccessKey, - API: v.API, - }) + printMsg(hostMessage{op: "list", Host: k, AccessKeyID: v.AccessKeyID, SecretAccessKey: v.SecretAccessKey, API: v.API}) } - } func removeHost(hostGlob string) { @@ -200,10 +193,7 @@ func removeHost(hostGlob string) { err = writeConfig(newConfig) fatalIf(err.Trace(hostGlob), "Unable to save host glob ‘"+hostGlob+"’.") - Prints("%s\n", HostMessage{ - op: "remove", - Host: hostGlob, - }) + printMsg(hostMessage{op: "remove", Host: hostGlob}) } // isValidSecretKey - validate secret key @@ -265,7 +255,7 @@ func addHost(hostGlob, accessKeyID, secretAccessKey, api string) { err = writeConfig(newConfig) fatalIf(err.Trace(hostGlob), "Unable to save host glob ‘"+hostGlob+"’.") - Prints("%s\n", HostMessage{ + printMsg(hostMessage{ op: "add", Host: hostGlob, AccessKeyID: accessKeyID, diff --git a/cp-main.go b/cp-main.go index 85d3dbaa..ca9609bc 100644 --- a/cp-main.go +++ b/cp-main.go @@ -66,20 +66,20 @@ EXAMPLES: `, } -// CopyMessage container for file copy messages -type CopyMessage struct { +// copyMessage container for file copy messages +type copyMessage struct { Source string `json:"source"` Target string `json:"target"` Length int64 `json:"length"` } // String colorized copy message -func (c CopyMessage) String() string { +func (c copyMessage) String() string { return console.Colorize("Copy", fmt.Sprintf("‘%s’ -> ‘%s’", c.Source, c.Target)) } // JSON jsonified copy message -func (c CopyMessage) JSON() string { +func (c copyMessage) JSON() string { copyMessageBytes, err := json.Marshal(c) fatalIf(probe.NewError(err), "Failed to marshal copy message.") @@ -115,7 +115,7 @@ func doCopy(cpURLs copyURLs, progressReader interface{}, cpQueue <-chan bool, wg var newReader io.ReadCloser if globalQuietFlag || globalJSONFlag { - Prints("%s\n", CopyMessage{ + printMsg(copyMessage{ Source: cpURLs.SourceContent.Name, Target: cpURLs.TargetContent.Name, Length: cpURLs.SourceContent.Size, diff --git a/diff-main.go b/diff-main.go index d0e9dac7..aa0cbbed 100644 --- a/diff-main.go +++ b/diff-main.go @@ -99,7 +99,7 @@ func mainDiff(ctx *cli.Context) { for diff := range doDiff(newFirstURL, secondURL, isURLRecursive(firstURL)) { fatalIf(diff.Error.Trace(newFirstURL, secondURL), "Failed to diff ‘"+firstURL+"’ and ‘"+secondURL+"’.") - Prints("%s\n", diff) + printMsg(diff) } } diff --git a/ls.go b/ls.go index 62c74345..678888ef 100644 --- a/ls.go +++ b/ls.go @@ -147,7 +147,7 @@ func doList(clnt client.Client, recursive, multipleArgs bool) *probe.Error { if multipleArgs && parentContent.Type.IsDir() { contentCh.Content.Name = filepath.Join(parentContent.Name, strings.TrimPrefix(contentCh.Content.Name, parentContent.Name)) } - Prints("%s\n", parseContent(contentCh.Content)) + printMsg(parseContent(contentCh.Content)) } if err != nil { return err.Trace() @@ -190,7 +190,7 @@ func doListIncomplete(clnt client.Client, recursive, multipleArgs bool) *probe.E if multipleArgs && parentContent.Type.IsDir() { contentCh.Content.Name = filepath.Join(parentContent.Name, strings.TrimPrefix(contentCh.Content.Name, parentContent.Name)) } - Prints("%s\n", parseContent(contentCh.Content)) + printMsg(parseContent(contentCh.Content)) } if err != nil { return err.Trace() diff --git a/mb-main.go b/mb-main.go index 03c06148..0bfdc8df 100644 --- a/mb-main.go +++ b/mb-main.go @@ -49,19 +49,19 @@ EXAMPLES: `, } -// MakeBucketMessage is container for make bucket success and failure messages -type MakeBucketMessage struct { +// makeBucketMessage is container for make bucket success and failure messages +type makeBucketMessage struct { Status string `json:"status"` Bucket string `json:"bucket"` } // String colorized make bucket message -func (s MakeBucketMessage) String() string { +func (s makeBucketMessage) String() string { return console.Colorize("MakeBucket", "Bucket created successfully ‘"+s.Bucket+"’") } // JSON jsonified make bucket message -func (s MakeBucketMessage) JSON() string { +func (s makeBucketMessage) JSON() string { makeBucketJSONBytes, err := json.Marshal(s) fatalIf(probe.NewError(err), "Unable to marshal into JSON.") @@ -105,23 +105,14 @@ func mainMakeBucket(ctx *cli.Context) { for _, arg := range ctx.Args() { targetURL := getAliasURL(arg, config.Aliases) - fatalIf(doMakeBucket(targetURL).Trace(targetURL), "Unable to make bucket ‘"+targetURL+"’.") - Prints("%s\n", MakeBucketMessage{ - Status: "success", - Bucket: targetURL, - }) - } -} + // Instantiate client for URL. + clnt, err := url2Client(targetURL) + fatalIf(err.Trace(targetURL), "Invalid target target ‘"+targetURL+"’.") -// doMakeBucket - -func doMakeBucket(targetURL string) *probe.Error { - clnt, err := url2Client(targetURL) - if err != nil { - return err.Trace(targetURL) + // Make bucket. + fatalIf(clnt.MakeBucket().Trace(), "Unable to make bucket ‘"+targetURL+"’.") + + // Successfully created a bucket. + printMsg(makeBucketMessage{Status: "success", Bucket: targetURL}) } - err = clnt.MakeBucket() - if err != nil { - return err.Trace(targetURL) - } - return nil } diff --git a/mb_access_test.go b/mb_access_test.go index 9884bd71..5d8be71f 100644 --- a/mb_access_test.go +++ b/mb_access_test.go @@ -24,17 +24,23 @@ import ( ) func (s *TestSuite) TestMbAndAccess(c *C) { - perr := doMakeBucket(server.URL + "/bucket") - c.Assert(perr, IsNil) - perr = doSetAccess(server.URL+"/bucket", "public-read-write") - c.Assert(perr, IsNil) + // Instantiate client for URL. + clnt, err := url2Client(server.URL + "/bucket") + c.Assert(err, IsNil) - perr = doSetAccess(server.URL+"/bucket", "invalid") - c.Assert(perr, Not(IsNil)) + // Make bucket. + err = clnt.MakeBucket() + c.Assert(err, IsNil) - perm, perr := doGetAccess(server.URL + "/bucket") - c.Assert(perr, IsNil) + err = doSetAccess(server.URL+"/bucket", "public-read-write") + c.Assert(err, IsNil) + + err = doSetAccess(server.URL+"/bucket", "invalid") + c.Assert(err, Not(IsNil)) + + perm, err := doGetAccess(server.URL + "/bucket") + c.Assert(err, IsNil) c.Assert(perm.isPrivate(), Equals, true) } diff --git a/mirror-main.go b/mirror-main.go index 12b8fce0..c7afe8da 100644 --- a/mirror-main.go +++ b/mirror-main.go @@ -62,19 +62,19 @@ EXAMPLES: `, } -// MirrorMessage container for file mirror messages -type MirrorMessage struct { +// mirrorMessage container for file mirror messages +type mirrorMessage struct { Source string `json:"source"` Targets []string `json:"targets"` } // String colorized mirror message -func (m MirrorMessage) String() string { +func (m mirrorMessage) String() string { return console.Colorize("Mirror", fmt.Sprintf("‘%s’ -> ‘%s’", m.Source, m.Targets)) } // JSON jsonified mirror message -func (m MirrorMessage) JSON() string { +func (m mirrorMessage) JSON() string { mirrorMessageBytes, e := json.Marshal(m) fatalIf(probe.NewError(e), "Unable to marshal into JSON.") @@ -115,7 +115,7 @@ func doMirror(sURLs mirrorURLs, progressReader interface{}, mirrorQueueCh <-chan var newReader io.ReadCloser if globalQuietFlag || globalJSONFlag { - Prints("%s\n", MirrorMessage{ + printMsg(mirrorMessage{ Source: sURLs.SourceContent.Name, Targets: targetURLs, }) diff --git a/print.go b/print.go index beb132f1..240d1eeb 100644 --- a/print.go +++ b/print.go @@ -18,17 +18,17 @@ package main import "github.com/minio/mc/pkg/console" -// Message interface for all structured messages implementing JSON(), String() methods -type Message interface { +// message interface for all structured messages implementing JSON(), String() methods. +type message interface { JSON() string String() string } -// Prints print wrapper for Message interface, implementing both JSON() and String() methods -func Prints(format string, message Message) { +// printMsg prints message string or JSON structure depending on the type of output console. +func printMsg(msg message) { if !globalJSONFlag { - console.Printf(format, message.String()) - return + console.Println(msg.String()) + } else { + console.Println(msg.JSON()) } - console.Printf(format, message.JSON()) } diff --git a/rm-main.go b/rm-main.go index c9bb6fec..fb7b2ca2 100644 --- a/rm-main.go +++ b/rm-main.go @@ -287,7 +287,7 @@ func rmPrinterFuncGenerate() rmPrinterFunc { } return func(msg rmMessage) { if globalJSONFlag || globalQuietFlag { - Prints("%s\n", msg) + printMsg(msg) return } scanBar(msg.Name) diff --git a/session-main.go b/session-main.go index 7f3d5ed6..8cf590c0 100644 --- a/session-main.go +++ b/session-main.go @@ -75,24 +75,24 @@ func listSessions() *probe.Error { // sort sessions based on time sort.Sort(bySessionWhen(bySessions)) for _, session := range bySessions { - Prints("%s\n", session) + printMsg(session) } return nil } -// ClearSessionMessage container for clearing session messages -type ClearSessionMessage struct { +// clearSessionMessage container for clearing session messages +type clearSessionMessage struct { Status string `json:"success"` SessionID string `json:"sessionId"` } // String colorized clear session message -func (c ClearSessionMessage) String() string { +func (c clearSessionMessage) String() string { return console.Colorize("ClearSession", "Session ‘"+c.SessionID+"’ cleared successfully") } // JSON jsonified clear session message -func (c ClearSessionMessage) JSON() string { +func (c clearSessionMessage) JSON() string { clearSessionJSONBytes, err := json.Marshal(c) fatalIf(probe.NewError(err), "Unable to marshal into JSON.") @@ -107,10 +107,7 @@ func clearSession(sid string) { fatalIf(session.Delete().Trace(sid), "Unable to load session ‘"+sid+"’.") - Prints("%s\n", ClearSessionMessage{ - Status: "success", - SessionID: sid, - }) + printMsg(clearSessionMessage{Status: "success", SessionID: sid}) } return } @@ -124,10 +121,7 @@ func clearSession(sid string) { if session != nil { fatalIf(session.Delete().Trace(sid), "Unable to load session ‘"+sid+"’.") - Prints("%s\n", ClearSessionMessage{ - Status: "success", - SessionID: sid, - }) + printMsg(clearSessionMessage{Status: "success", SessionID: sid}) } } diff --git a/share-download-main.go b/share-download-main.go index 0f0490df..1dffd5a0 100644 --- a/share-download-main.go +++ b/share-download-main.go @@ -136,7 +136,7 @@ func doShareDownloadURL(targetURL string, recursive bool, expires time.Duration) Date: shareDate, Message: shareMessageV3, }) - Prints("%s\n", shareMessage) + printMsg(shareMessage) } saveSharedURLsV3(sURLs) return nil diff --git a/share-upload-main.go b/share-upload-main.go index dd9f2eaa..47c6f4d1 100644 --- a/share-upload-main.go +++ b/share-upload-main.go @@ -130,7 +130,7 @@ func doShareUploadURL(targetURL string, recursive bool, expires time.Duration, c Date: shareDate, Message: shareMessageV3, }) - Prints("%s\n", shareMessage) + printMsg(shareMessage) saveSharedURLsV3(sURLs) return nil } diff --git a/update-main.go b/update-main.go index 46a2655c..e1dda2ab 100644 --- a/update-main.go +++ b/update-main.go @@ -177,7 +177,7 @@ func getExperimentalUpdate() { if latest.After(current) { updateMessage.Update = true } - Prints("%s\n", updateMessage) + printMsg(updateMessage) } func getReleaseUpdate() { @@ -219,5 +219,5 @@ func getReleaseUpdate() { if latest.After(current) { updateMessage.Update = true } - Prints("%s\n", updateMessage) + printMsg(updateMessage) }