From 6a8224b4ab7f9c5213bf32c2af9795d4a6d34af5 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sun, 8 Nov 2015 12:56:27 -0800 Subject: [PATCH] cleanup: Cleanup across code base. - improve on code comments, needs more work this is a first cut. - improve on naming conventions and simplification as and when necessary. --- access-perms.go | 15 ++++---- aliasconfig.go | 7 ++-- build-constants.go | 10 ++++-- cat-main.go | 24 +++++++------ common-methods.go | 1 + ls-main.go | 9 +++-- ls.go | 38 +++++++++++---------- rm-main.go | 2 +- share-download-main.go | 23 +++++++------ share-list.go | 19 ++++++----- share-main.go | 22 ++++++------ share-upload-main.go | 30 ++++++++-------- share-v1.go | 4 +-- share-v2.go | 4 +-- share-v3.go | 4 +-- share.go | 77 +++++++++++++++++++++++------------------- update-main.go | 56 ++++++++++++++++-------------- 17 files changed, 189 insertions(+), 156 deletions(-) diff --git a/access-perms.go b/access-perms.go index b1a919da..0025cc0d 100644 --- a/access-perms.go +++ b/access-perms.go @@ -16,7 +16,7 @@ package main -// isValidAccessPERM - is provided acl string supported +// isValidAccessPERM - is provided access perm string supported. func (b accessPerms) isValidAccessPERM() bool { switch true { case b.isPrivate(): @@ -32,10 +32,10 @@ func (b accessPerms) isValidAccessPERM() bool { } } -// accessPerms - access level access control +// accessPerms - access level. type accessPerms string -// different types of ACL's currently supported for accesss +// different types of Access perm's currently supported by accesss command. const ( accessPrivate = accessPerms("private") accessReadOnly = accessPerms("readonly") @@ -59,6 +59,7 @@ func (b accessPerms) String() string { return "private" } +// convert acl's to access perms func aclToPerms(acl string) accessPerms { switch acl { case "private": @@ -74,22 +75,22 @@ func aclToPerms(acl string) accessPerms { } } -// IsPrivate - is acl Private +// isPrivate - is acl Private. func (b accessPerms) isPrivate() bool { return b == accessPrivate } -// IsPublicRead - is acl PublicRead +// isPublicRead - is acl PublicRead. func (b accessPerms) isReadOnly() bool { return b == accessReadOnly } -// IsPublicReadWrite - is acl PublicReadWrite +// isPublicReadWrite - is acl PublicReadWrite. func (b accessPerms) isPublic() bool { return b == accessPublic } -// IsAuthorized - is acl AuthorizedRead +// isAuthorized - is acl AuthorizedRead. func (b accessPerms) isAuthorized() bool { return b == accessAuthorized } diff --git a/aliasconfig.go b/aliasconfig.go index a6242b4f..55a4bc3b 100644 --- a/aliasconfig.go +++ b/aliasconfig.go @@ -23,12 +23,9 @@ import ( "unicode/utf8" ) -// validAliasName regex validation. -var validAliasName = regexp.MustCompile("^[a-zA-Z][a-zA-Z0-9-]+$") - -// Check if it is an aliased URL. +// isValidAliasName - Check if aliasName is a valid alias. func isValidAliasName(aliasName string) bool { - return validAliasName.MatchString(aliasName) + return regexp.MustCompile("^[a-zA-Z][a-zA-Z0-9-]+$").MatchString(aliasName) } // getAliasURL expands aliased (name/path) to full URL, used by url-parser. diff --git a/build-constants.go b/build-constants.go index ae1df1fe..49b83683 100644 --- a/build-constants.go +++ b/build-constants.go @@ -17,8 +17,12 @@ package main var ( - mcVersion = "UNOFFICIAL.GOGET" - mcReleaseTag = "UNOFFICIAL.GOGET" - mcCommitID = "UNOFFICIAL.GOGET" + // mcVersion - version time.RFC3339. + mcVersion = "UNOFFICIAL.GOGET" + // mcReleaseTag - release tag in TAG.%Y-%m-%dT%H-%M-%SZ. + mcReleaseTag = "UNOFFICIAL.GOGET" + // mcCommitID - latest commit id. + mcCommitID = "UNOFFICIAL.GOGET" + // mcShortCommitID - first 12 characters from mcCommitID. mcShortCommitID = mcCommitID[:] ) diff --git a/cat-main.go b/cat-main.go index f283e6a5..17e16de2 100644 --- a/cat-main.go +++ b/cat-main.go @@ -78,27 +78,25 @@ func checkCatSyntax(ctx *cli.Context) { // catURL displays contents of a URL to stdout. func catURL(sourceURL string) *probe.Error { - config := mustGetMcConfig() - URL := getAliasURL(sourceURL, config.Aliases) - var reader io.ReadCloser - switch URL { + switch sourceURL { case "-": reader = ioutil.NopCloser(bufio.NewReader(os.Stdin)) default: - sourceClnt, err := url2Client(URL) + sourceClnt, err := url2Client(sourceURL) if err != nil { - return err.Trace(URL) + return err.Trace(sourceURL) } - // Ignore size, since os.Stat() would not return proper size all the time for local filesystem for example /proc files. + // Ignore size, since os.Stat() would not return proper size all the + // time for local filesystem for example /proc files. reader, _, err = sourceClnt.Get(0, 0) if err != nil { - return err.Trace(URL) + return err.Trace(sourceURL) } } defer reader.Close() - return catOut(reader).Trace(URL) + return catOut(reader).Trace(sourceURL) } // catOut reads from reader stream and writes to stdout. @@ -149,8 +147,12 @@ func mainCat(ctx *cli.Context) { } } } + // Convert arguments to URLs: expand alias, fix format... - for _, arg := range args { - fatalIf(catURL(arg).Trace(arg), "Unable to read from ‘"+arg+"’.") + URLs, err := args2URLs(args) + fatalIf(err.Trace(args...), "Unable to parse arguments.") + + for _, url := range URLs { + fatalIf(catURL(url).Trace(url), "Unable to read from ‘"+url+"’.") } } diff --git a/common-methods.go b/common-methods.go index b0730691..58f767c1 100644 --- a/common-methods.go +++ b/common-methods.go @@ -184,6 +184,7 @@ func getNewClient(urlStr string, auth hostConfig) (client.Client, *probe.Error) return nil, errInitClient(urlStr).Trace() } +// url2Client - convenience wrapper for getNewClient func url2Client(url string) (client.Client, *probe.Error) { urlconfig, err := getHostConfig(url) if err != nil { diff --git a/ls-main.go b/ls-main.go index d2006812..3960c26c 100644 --- a/ls-main.go +++ b/ls-main.go @@ -71,6 +71,7 @@ EXAMPLES: `, } +// checkListSyntax - validate all the passed arguments func checkListSyntax(ctx *cli.Context) { args := ctx.Args() if !ctx.Args().Present() { @@ -90,6 +91,7 @@ func checkListSyntax(ctx *cli.Context) { } } +// setListPalette - set ls output theme func setListPalette(style string) { console.SetCustomPalette(map[string]*color.Color{ "File": color.New(color.FgWhite), @@ -106,7 +108,7 @@ func setListPalette(style string) { }) return } - /// Add more styles here + /// Add more styles here if needed if style == "nocolor" { // All coloring options exhausted, setting nocolor safely console.SetNoColor() @@ -115,11 +117,14 @@ func setListPalette(style string) { // mainList - is a handler for mc ls command func mainList(ctx *cli.Context) { + // set theme for 'ls' setListPalette(ctx.GlobalString("colors")) + + // check 'ls' cli arguments checkListSyntax(ctx) args := ctx.Args() - // Operating system tool behavior + // mimic operating system tool behavior if globalMimicFlag && !ctx.Args().Present() { args = []string{"."} } diff --git a/ls.go b/ls.go index 1c58c60a..2a9f2c95 100644 --- a/ls.go +++ b/ls.go @@ -36,29 +36,29 @@ const ( printDate = "2006-01-02 15:04:05 MST" ) -// ContentMessage container for content message structure. -type ContentMessage struct { +// contentMessage container for content message structure. +type contentMessage struct { Filetype string `json:"type"` Time time.Time `json:"lastModified"` Size int64 `json:"size"` - Name string `json:"name"` + Key string `json:"key"` } // String colorized string message -func (c ContentMessage) String() string { +func (c contentMessage) String() string { message := console.Colorize("Time", fmt.Sprintf("[%s] ", c.Time.Format(printDate))) message = message + console.Colorize("Size", fmt.Sprintf("%6s ", humanize.IBytes(uint64(c.Size)))) message = func() string { if c.Filetype == "folder" { - return message + console.Colorize("Dir", fmt.Sprintf("%s", c.Name)) + return message + console.Colorize("Dir", fmt.Sprintf("%s", c.Key)) } - return message + console.Colorize("File", fmt.Sprintf("%s", c.Name)) + return message + console.Colorize("File", fmt.Sprintf("%s", c.Key)) }() return message } // JSON jsonified content message -func (c ContentMessage) JSON() string { +func (c contentMessage) JSON() string { jsonMessageBytes, e := json.Marshal(c) fatalIf(probe.NewError(e), "Unable to marshal into JSON.") @@ -66,8 +66,8 @@ func (c ContentMessage) JSON() string { } // parseContent parse client Content container into printer struct. -func parseContent(c *client.Content) ContentMessage { - content := ContentMessage{} +func parseContent(c *client.Content) contentMessage { + content := contentMessage{} content.Time = c.Time.Local() // guess file type @@ -80,7 +80,7 @@ func parseContent(c *client.Content) ContentMessage { content.Size = c.Size // Convert OS Type to match console file printing style. - content.Name = func() string { + content.Key = func() string { switch { case runtime.GOOS == "windows": c.URL.Path = strings.Replace(c.URL.Path, "/", "\\", -1) @@ -103,7 +103,7 @@ func parseContent(c *client.Content) ContentMessage { // trimContent to fancify the output for directories // -// TODO: Find a way to simplify this facification. +// TODO: Find a way to simplify this fancification. func trimContent(parentContent, childContent *client.Content, recursive bool) *client.Content { if recursive { // If recursive remove the unnecessary '/', in the beginning @@ -147,7 +147,8 @@ func doList(clnt client.Client, recursive, multipleArgs bool) *probe.Error { if err != nil { return err.Trace(clnt.GetURL().String()) } - for contentCh := range clnt.List(recursive, false) { + isIncomplete := false // do not list incomplete uploads + for contentCh := range clnt.List(recursive, isIncomplete) { if contentCh.Err != nil { switch contentCh.Err.ToGoError().(type) { // handle this specifically for filesystem @@ -175,8 +176,9 @@ func doList(clnt client.Client, recursive, multipleArgs bool) *probe.Error { err = contentCh.Err.Trace() break } - contentCh.Content = trimContent(parentContent, contentCh.Content, recursive) - printMsg(parseContent(contentCh.Content)) + trimmedContent := trimContent(parentContent, contentCh.Content, recursive) + parsedContent := parseContent(trimmedContent) + printMsg(parsedContent) } if err != nil { return err.Trace() @@ -192,7 +194,8 @@ func doListIncomplete(clnt client.Client, recursive, multipleArgs bool) *probe.E if err != nil { return err.Trace(clnt.GetURL().String()) } - for contentCh := range clnt.List(recursive, true) { + isIncomplete := true // list only incomplete uploads + for contentCh := range clnt.List(recursive, isIncomplete) { if contentCh.Err != nil { switch contentCh.Err.ToGoError().(type) { // handle this specifically for filesystem @@ -216,8 +219,9 @@ func doListIncomplete(clnt client.Client, recursive, multipleArgs bool) *probe.E err = contentCh.Err.Trace() break } - contentCh.Content = trimContent(parentContent, contentCh.Content, recursive) - printMsg(parseContent(contentCh.Content)) + trimmedContent := trimContent(parentContent, contentCh.Content, recursive) + parsedContent := parseContent(trimmedContent) + printMsg(parsedContent) } if err != nil { return err.Trace() diff --git a/rm-main.go b/rm-main.go index d5ebfc0f..b5a2722c 100644 --- a/rm-main.go +++ b/rm-main.go @@ -86,7 +86,7 @@ EXAMPLES: // Structured message depending on the type of console. type rmMessage struct { - URL string `json:"name"` + URL string `json:"url"` } // Colorized message for console printing. diff --git a/share-download-main.go b/share-download-main.go index 0da3859f..7e1844a0 100644 --- a/share-download-main.go +++ b/share-download-main.go @@ -64,10 +64,14 @@ func checkShareDownloadSyntax(ctx *cli.Context) { } } +// main for share download. func mainShareDownload(ctx *cli.Context) { - shareDataDirSetup() - checkShareDownloadSyntax(ctx) + // setup share data folder and file. + shareDataSetup() + // set share command theme. setSharePalette(ctx.GlobalString("colors")) + // check input arguments. + checkShareDownloadSyntax(ctx) args := ctx.Args() config := mustGetMcConfig() @@ -119,24 +123,21 @@ func doShareDownloadURL(targetURL string, recursive bool, expires time.Duration) if err != nil { return err.Trace() } - shareMessage := ShareMessage{ - Expiry: expires, - DownloadURL: sharedURL, - Key: newClnt.GetURL().String(), - } - shareMessageV3 := ShareMessageV3{ + var shareMsg interface{} + shareMsg = shareMessage{ Expiry: expires, DownloadURL: sharedURL, Key: newClnt.GetURL().String(), } + shareMsgV3 := shareMessageV3(shareMsg.(shareMessage)) sURLs.URLs = append(sURLs.URLs, struct { Date time.Time - Message ShareMessageV3 + Message shareMessageV3 }{ Date: shareDate, - Message: shareMessageV3, + Message: shareMsgV3, }) - printMsg(shareMessage) + printMsg(shareMsg.(shareMessage)) } saveSharedURLsV3(sURLs) return nil diff --git a/share-list.go b/share-list.go index aaa7ba39..9ee16bab 100644 --- a/share-list.go +++ b/share-list.go @@ -42,14 +42,7 @@ EXAMPLES: `, } -func mainShareList(ctx *cli.Context) { - shareDataDirSetup() - setSharePalette(ctx.GlobalString("colors")) - err := doShareList() - fatalIf(err.Trace(), "Unable to list shared URLs.") -} - -// doShareList list shared url's +// doShareList list shared url's. func doShareList() *probe.Error { sURLs, err := loadSharedURLsV3() if err != nil { @@ -111,3 +104,13 @@ func doShareList() *probe.Error { } return nil } + +// main for share list. +func mainShareList(ctx *cli.Context) { + // setup share data folder and file. + shareDataSetup() + // set share command theme. + setSharePalette(ctx.GlobalString("colors")) + err := doShareList() + fatalIf(err.Trace(), "Unable to list shared URLs.") +} diff --git a/share-main.go b/share-main.go index e8e343c8..eed93a68 100644 --- a/share-main.go +++ b/share-main.go @@ -52,29 +52,29 @@ COMMANDS: `, } -// ShareMessageV1 ... -type ShareMessageV1 struct { +// structured share command messages version '1' +type shareMessageV1 struct { Expiry time.Duration `json:"expiry"` URL string `json:"url"` Key string `json:"keyName"` } -// ShareMessageV2 ... -type ShareMessageV2 ShareMessageV1 +// structured share command messages version '2' +type shareMessageV2 shareMessageV1 -// ShareMessageV3 ... -type ShareMessageV3 struct { +// structured share command messages version '3' +type shareMessageV3 struct { Expiry time.Duration `json:"expiry"` DownloadURL string `json:"downloadUrl,omitempty"` UploadInfo map[string]string `json:"uploadInfo,omitempty"` Key string `json:"keyName"` } -// ShareMessage ... -type ShareMessage ShareMessageV3 +// shareMessage this points to latest share command message structure. +type shareMessage shareMessageV3 // String - regular colorized message -func (s ShareMessage) String() string { +func (s shareMessage) String() string { if len(s.DownloadURL) > 0 { return console.Colorize("Share", fmt.Sprintf("%s", s.DownloadURL)) } @@ -96,7 +96,7 @@ func (s ShareMessage) String() string { } // JSON json message for share command -func (s ShareMessage) JSON() string { +func (s shareMessage) JSON() string { var shareMessageBytes []byte var err error if len(s.DownloadURL) > 0 { @@ -144,7 +144,7 @@ func (s ShareMessage) JSON() string { return string(shareMessageBytes) } -// mainShare - is a handler for mc share command +// mainShare - main handler for mc share command func mainShare(ctx *cli.Context) { if !ctx.Args().Present() || ctx.Args().First() == "help" { cli.ShowAppHelp(ctx) diff --git a/share-upload-main.go b/share-upload-main.go index 47c6f4d1..439c4114 100644 --- a/share-upload-main.go +++ b/share-upload-main.go @@ -51,6 +51,7 @@ EXAMPLES: `, } +// checkShareUploadSyntaxt - check share upload command arguments func checkShareUploadSyntax(ctx *cli.Context) { args := ctx.Args() if !args.Present() || args.First() == "help" { @@ -68,13 +69,17 @@ func checkShareUploadSyntax(ctx *cli.Context) { } } +// main for share upload command. func mainShareUpload(ctx *cli.Context) { + // setup share data folder and file. + shareDataSetup() + // set share command theme. + setSharePalette(ctx.GlobalString("colors")) + // check input arguments. + checkShareUploadSyntax(ctx) + var expires time.Duration var err error - shareDataDirSetup() - checkShareUploadSyntax(ctx) - setSharePalette(ctx.GlobalString("colors")) - args := ctx.Args() config := mustGetMcConfig() if strings.TrimSpace(args.Get(1)) == "" { @@ -92,7 +97,7 @@ func mainShareUpload(ctx *cli.Context) { fatalIf(e.Trace(targetURL), "Unable to generate URL for upload.") } -// doShareURL share files from target +// doShareUploadURL uploads files to the target. func doShareUploadURL(targetURL string, recursive bool, expires time.Duration, contentType string) *probe.Error { shareDate := time.Now().UTC() sURLs, err := loadSharedURLsV3() @@ -113,24 +118,21 @@ func doShareUploadURL(targetURL string, recursive bool, expires time.Duration, c Key = Key + recursiveSeparator m["key"] = m["key"] + "" } - shareMessage := ShareMessage{ - Expiry: expires, - UploadInfo: m, - Key: Key, - } - shareMessageV3 := ShareMessageV3{ + var shareMsg interface{} + shareMsg = shareMessage{ Expiry: expires, UploadInfo: m, Key: Key, } + shareMsgV3 := shareMessageV3(shareMsg.(shareMessage)) sURLs.URLs = append(sURLs.URLs, struct { Date time.Time - Message ShareMessageV3 + Message shareMessageV3 }{ Date: shareDate, - Message: shareMessageV3, + Message: shareMsgV3, }) - printMsg(shareMessage) + printMsg(shareMsg.(shareMessage)) saveSharedURLsV3(sURLs) return nil } diff --git a/share-v1.go b/share-v1.go index 2206e056..d8c1b101 100644 --- a/share-v1.go +++ b/share-v1.go @@ -28,7 +28,7 @@ type sharedURLsV1 struct { Version string URLs map[string]struct { Date time.Time - Message ShareMessageV1 + Message shareMessageV1 } } @@ -58,7 +58,7 @@ func newSharedURLsV1() *sharedURLsV1 { Version: "1.0.0", URLs: make(map[string]struct { Date time.Time - Message ShareMessageV1 + Message shareMessageV1 }), } return s diff --git a/share-v2.go b/share-v2.go index ce6a2ecb..59fe3646 100644 --- a/share-v2.go +++ b/share-v2.go @@ -28,7 +28,7 @@ type sharedURLsV2 struct { Version string URLs []struct { Date time.Time - Message ShareMessageV2 + Message shareMessageV2 } } @@ -68,7 +68,7 @@ func saveSharedURLsV2(s *sharedURLsV2) *probe.Error { func newSharedURLsV2() *sharedURLsV2 { var urls []struct { Date time.Time - Message ShareMessageV2 + Message shareMessageV2 } s := &sharedURLsV2{ Version: "1.1.0", diff --git a/share-v3.go b/share-v3.go index 39d93974..a086e653 100644 --- a/share-v3.go +++ b/share-v3.go @@ -28,7 +28,7 @@ type sharedURLsV3 struct { Version string URLs []struct { Date time.Time - Message ShareMessageV3 + Message shareMessageV3 } } @@ -68,7 +68,7 @@ func saveSharedURLsV3(s *sharedURLsV3) *probe.Error { func newSharedURLsV3() *sharedURLsV3 { var urls []struct { Date time.Time - Message ShareMessageV3 + Message shareMessageV3 } s := &sharedURLsV3{ Version: "3", diff --git a/share.go b/share.go index e2b873e2..cc95f21f 100644 --- a/share.go +++ b/share.go @@ -30,18 +30,47 @@ import ( "github.com/minio/minio-xl/pkg/quick" ) -func shareDataDirSetup() { +// create shared data folder and file if they doesn't exist. +func shareDataSetup() { if !isSharedURLsDataDirExists() { shareDir, err := getSharedURLsDataDir() fatalIf(err.Trace(), "Unable to get shared URL data folder.") fatalIf(createSharedURLsDataDir().Trace(), "Unable to create shared URL data folder ‘"+shareDir+"’.") + console.Infof("Successfully created ‘%s’ \n", shareDir) } if !isSharedURLsDataFileExists() { shareFile, err := getSharedURLsDataFile() fatalIf(err.Trace(), "Unable to get shared URL data file") fatalIf(createSharedURLsDataFile().Trace(), "Unable to create shared URL data file ‘"+shareFile+"’.") + console.Infof("Successfully created ‘%s’ \n", shareFile) + } +} + +// set share command theme +func setSharePalette(style string) { + console.SetCustomPalette(map[string]*color.Color{ + "Share": color.New(color.FgGreen, color.Bold), + "Expires": color.New(color.FgRed, color.Bold), + "URL": color.New(color.FgCyan, color.Bold), + "File": color.New(color.FgRed, color.Bold), + }) + + if style == "light" { + console.SetCustomPalette(map[string]*color.Color{ + "Share": color.New(color.FgWhite, color.Bold), + "Expires": color.New(color.FgWhite, color.Bold), + "URL": color.New(color.FgWhite, color.Bold), + "File": color.New(color.FgWhite, color.Bold), + }) + return + } + + /// Add more styles here + if style == "nocolor" { + // All coloring options exhausted, setting nocolor safely + console.SetNoColor() } } @@ -55,7 +84,6 @@ func migrateSharedURLsV1ToV2() { if !isSharedURLsDataFileExists() { return } - // try to load latest version if possible sURLsV2, err := loadSharedURLsV2() if err != nil { @@ -73,10 +101,10 @@ func migrateSharedURLsV1ToV2() { value.Message.Key = key entry := struct { Date time.Time - Message ShareMessageV2 + Message shareMessageV2 }{ Date: value.Date, - Message: ShareMessageV2{ + Message: shareMessageV2{ Expiry: value.Message.Expiry, URL: value.Message.URL, Key: value.Message.Key, @@ -107,7 +135,6 @@ func migrateSharedURLsV2ToV3() { if v3 { return } - // try to load V2 if possible sURLsV2, err := loadSharedURLsV2() fatalIf(err.Trace(), "Unable to load shared url version ‘1.1.0’.") @@ -118,10 +145,10 @@ func migrateSharedURLsV2ToV3() { for _, value := range sURLsV2.URLs { entry := struct { Date time.Time - Message ShareMessageV3 + Message shareMessageV3 }{ Date: value.Date, - Message: ShareMessageV3{ + Message: shareMessageV3{ Expiry: value.Message.Expiry, DownloadURL: value.Message.URL, Key: value.Message.Key, @@ -130,7 +157,7 @@ func migrateSharedURLsV2ToV3() { sURLsV3.URLs = append(sURLsV3.URLs, entry) } err = saveSharedURLsV3(sURLsV3) - fatalIf(err.Trace(), "Unable to save new shared url version ‘1.2.0’.") + fatalIf(err.Trace(), "Unable to save new shared url version ‘3’.") } func getSharedURLsDataDir() (string, *probe.Error) { @@ -196,9 +223,13 @@ func createSharedURLsDataFile() *probe.Error { func isObjectKeyPresent(url string) bool { u := client.NewURL(url) path := u.Path - match, _ := filepath.Match("*.s3*.amazonaws.com", u.Host) - switch { - case match == true: + matchS3, _ := filepath.Match("*.s3*.amazonaws.com", u.Host) + if matchS3 { + hostSplits := strings.SplitN(u.Host, ".", 2) + path = string(u.Separator) + hostSplits[0] + u.Path + } + matchGcs, _ := filepath.Match("*.storage.googleapis.com", u.Host) + if matchGcs { hostSplits := strings.SplitN(u.Host, ".", 2) path = string(u.Separator) + hostSplits[0] + u.Path } @@ -217,27 +248,3 @@ func isObjectKeyPresent(url string) bool { } return false } - -func setSharePalette(style string) { - console.SetCustomPalette(map[string]*color.Color{ - "Share": color.New(color.FgGreen, color.Bold), - "Expires": color.New(color.FgRed, color.Bold), - "URL": color.New(color.FgCyan, color.Bold), - "File": color.New(color.FgRed, color.Bold), - }) - if style == "light" { - console.SetCustomPalette(map[string]*color.Color{ - "Share": color.New(color.FgWhite, color.Bold), - "Expires": color.New(color.FgWhite, color.Bold), - "URL": color.New(color.FgWhite, color.Bold), - "File": color.New(color.FgWhite, color.Bold), - }) - return - } - /// Add more styles here - - if style == "nocolor" { - // All coloring options exhausted, setting nocolor safely - console.SetNoColor() - } -} diff --git a/update-main.go b/update-main.go index bcd1c3e1..97328110 100644 --- a/update-main.go +++ b/update-main.go @@ -50,21 +50,27 @@ EXAMPLES: `, } -// Updates container to hold updates json -type Updates struct { +// update URL endpoints. +const ( + mcUpdateURL = "https://dl.minio.io:9000/updates/updates.json" + mcExperimentalURL = "https://dl.minio.io:9000/updates/experimental.json" +) + +// mcUpdates container to hold updates json. +type mcUpdates struct { BuildDate string Platforms map[string]string } -// UpdateMessage container to hold update messages -type UpdateMessage struct { +// updateMessage container to hold update messages. +type updateMessage struct { Update bool `json:"update"` Download string `json:"downloadURL"` Version string `json:"version"` } -// String colorized update message -func (u UpdateMessage) String() string { +// String colorized update message. +func (u updateMessage) String() string { if u.Update { var msg string if runtime.GOOS == "windows" { @@ -79,19 +85,15 @@ func (u UpdateMessage) String() string { return console.Colorize("UpdateMessage", "You are already running the most recent version of ‘mc’.") } -// JSON jsonified update message -func (u UpdateMessage) JSON() string { +// JSON jsonified update message. +func (u updateMessage) JSON() string { updateMessageJSONBytes, err := json.Marshal(u) fatalIf(probe.NewError(err), "Unable to marshal into JSON.") return string(updateMessageJSONBytes) } -const ( - mcUpdateURL = "https://dl.minio.io:9000/updates/updates.json" - mcExperimentalURL = "https://dl.minio.io:9000/updates/experimental.json" -) - +// check valid input arguments. func checkUpdateSyntax(ctx *cli.Context) { if ctx.Args().First() == "help" || !ctx.Args().Present() { cli.ShowCommandHelpAndExit(ctx, "update", 1) // last argument is exit code @@ -102,6 +104,7 @@ func checkUpdateSyntax(ctx *cli.Context) { } } +// set update command theme. func setUpdatePalette(style string) { console.SetCustomPalette(map[string]*color.Color{ "UpdateMessage": color.New(color.FgGreen, color.Bold), @@ -112,17 +115,18 @@ func setUpdatePalette(style string) { }) return } - /// Add more styles here + /// Add more styles here. if style == "nocolor" { - // All coloring options exhausted, setting nocolor safely + // All coloring options exhausted, setting nocolor safely. console.SetNoColor() } } -// mainUpdate - +// main for update command. func mainUpdate(ctx *cli.Context) { + // check input arguments checkUpdateSyntax(ctx) - + // set theme setUpdatePalette(ctx.GlobalString("colors")) arg := strings.TrimSpace(ctx.Args().First()) @@ -134,6 +138,7 @@ func mainUpdate(ctx *cli.Context) { } } +// verify new updates for experimentals. func getExperimentalUpdate() { clnt, err := url2Client(mcExperimentalURL) fatalIf(err.Trace(mcExperimentalURL), "Unable to initalize experimental URL.") @@ -152,7 +157,7 @@ func getExperimentalUpdate() { fatalIf(errDummy().Trace(), "Experimental updates not supported for custom build. Version field is empty. Please download official releases from https://minio.io/#mc") } - var experimentals Updates + var experimentals mcUpdates decoder := json.NewDecoder(data) e = decoder.Decode(&experimentals) fatalIf(probe.NewError(e), "Unable to decode experimental update notification.") @@ -170,16 +175,17 @@ func getExperimentalUpdate() { string(mcExperimentalURLParse.Separator) + experimentals.Platforms[runtime.GOOS]) - updateMessage := UpdateMessage{ + updateMsg := updateMessage{ Download: downloadURL, Version: mcVersion, } if latest.After(current) { - updateMessage.Update = true + updateMsg.Update = true } - printMsg(updateMessage) + printMsg(updateMsg) } +// verify updates for releases. func getReleaseUpdate() { clnt, err := url2Client(mcUpdateURL) fatalIf(err.Trace(mcUpdateURL), "Unable to initalize update URL.") @@ -198,7 +204,7 @@ func getReleaseUpdate() { fatalIf(errDummy().Trace(), "Updates not supported for custom build. Version field is empty. Please download official releases from https://minio.io/#mc") } - var updates Updates + var updates mcUpdates decoder := json.NewDecoder(data) e = decoder.Decode(&updates) fatalIf(probe.NewError(e), "Unable to decode update notification.") @@ -212,12 +218,12 @@ func getReleaseUpdate() { mcUpdateURLParse := clnt.GetURL() downloadURL := mcUpdateURLParse.Scheme + "://" + mcUpdateURLParse.Host + string(mcUpdateURLParse.Separator) + updates.Platforms[runtime.GOOS] - updateMessage := UpdateMessage{ + updateMsg := updateMessage{ Download: downloadURL, Version: mcVersion, } if latest.After(current) { - updateMessage.Update = true + updateMsg.Update = true } - printMsg(updateMessage) + printMsg(updateMsg) }