From ef98d5963baa9df1b594e290de9239cc2b64b7da Mon Sep 17 00:00:00 2001 From: Krishna Srinivas Date: Fri, 9 Oct 2015 15:29:04 -0700 Subject: [PATCH] mc share output cleanup --- share-main.go | 36 +++++++++++++++++++++++++++--------- share-upload.go | 11 ++++++++--- share.go | 2 ++ 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/share-main.go b/share-main.go index d03c3145..97fa7584 100644 --- a/share-main.go +++ b/share-main.go @@ -20,6 +20,7 @@ import ( "bytes" "encoding/json" "fmt" + "strings" "time" "github.com/minio/cli" @@ -75,7 +76,7 @@ type ShareMessage ShareMessageV3 // String - regular colorized message func (s ShareMessage) String() string { if len(s.DownloadURL) > 0 { - return console.Colorize("Share", fmt.Sprintf("Expiry: %s\n DownloadURL: %s\n Key: %s", timeDurationToHumanizedTime(s.Expiry), s.DownloadURL, s.Key)) + return console.Colorize("Share", fmt.Sprintf("%s", s.DownloadURL)) } var key string URL := client.NewURL(s.Key) @@ -88,8 +89,10 @@ func (s ShareMessage) String() string { } curlCommand = curlCommand + fmt.Sprintf("-F %s=%s ", k, v) } - curlCommand = curlCommand + fmt.Sprintf("-F key=%s ", key) + "-F file=@ " - return console.Colorize("Share", fmt.Sprintf("Expiry: %s\n curl-command: %s\n Key: %s", timeDurationToHumanizedTime(s.Expiry), curlCommand, s.Key)) + curlCommand = curlCommand + fmt.Sprintf("-F key=%s ", key) + "-F file=@ " + emphasize := console.Colorize("File", "") + curlCommand = strings.Replace(curlCommand, "", emphasize, -1) + return console.Colorize("Share", fmt.Sprintf("%s", curlCommand)) } // JSON json message for share command @@ -107,14 +110,27 @@ func (s ShareMessage) JSON() string { Key: s.Key, }) } else { + var key string + URL := client.NewURL(s.Key) + postURL := URL.Scheme + URL.SchemeSeparator + URL.Host + string(URL.Separator) + s.UploadInfo["bucket"] + " " + curlCommand := "curl " + postURL + for k, v := range s.UploadInfo { + if k == "key" { + key = v + continue + } + curlCommand = curlCommand + fmt.Sprintf("-F %s=%s ", k, v) + } + curlCommand = curlCommand + fmt.Sprintf("-F key=%s ", key) + "-F file=@ " + shareMessageBytes, err = json.Marshal(struct { - Expiry humanizedTime `json:"expiry"` - UploadInfo map[string]string `json:"uploadInfo"` - Key string `json:"keyName"` + Expiry humanizedTime `json:"expiry"` + UploadCommand string `json:"uploadCommand"` + Key string `json:"keyName"` }{ - Expiry: timeDurationToHumanizedTime(s.Expiry), - UploadInfo: s.UploadInfo, - Key: s.Key, + Expiry: timeDurationToHumanizedTime(s.Expiry), + UploadCommand: curlCommand, + Key: s.Key, }) } @@ -123,6 +139,8 @@ func (s ShareMessage) JSON() string { // json encoding escapes ampersand into its unicode character which is not usable directly for share // and fails with cloud storage. convert them back so that they are usable shareMessageBytes = bytes.Replace(shareMessageBytes, []byte("\\u0026"), []byte("&"), -1) + shareMessageBytes = bytes.Replace(shareMessageBytes, []byte("\\u003c"), []byte("<"), -1) + shareMessageBytes = bytes.Replace(shareMessageBytes, []byte("\\u003e"), []byte(">"), -1) return string(shareMessageBytes) } diff --git a/share-upload.go b/share-upload.go index 1fc0afc2..72ef1f4d 100644 --- a/share-upload.go +++ b/share-upload.go @@ -17,6 +17,8 @@ package main import ( + "fmt" + "strings" "time" "github.com/minio/cli" @@ -64,9 +66,14 @@ func mainShareUpload(ctx *cli.Context) { var err error shareDataDirSetup() checkShareUploadSyntax(ctx) + setSharePalette(ctx.GlobalString("colors")) + args := ctx.Args() config := mustGetMcConfig() url := args.Get(0) + if strings.HasSuffix(url, "/") { + fatalIf(errDummy().Trace(), fmt.Sprintf("Upload location can not end with '/'. Did you mean %s%s", url, recursiveSeparator)) + } if len(args.Get(1)) == 0 { expires = time.Duration(604800) * time.Second } else { @@ -78,8 +85,6 @@ func mainShareUpload(ctx *cli.Context) { contentType := args.Get(2) targetURL := getAliasURL(url, config.Aliases) - setSharePalette(ctx.GlobalString("colors")) - e := doShareUploadURL(stripRecursiveURL(targetURL), isURLRecursive(targetURL), expires, contentType) fatalIf(e.Trace(targetURL), "Unable to generate URL for sharing.") } @@ -103,7 +108,7 @@ func doShareUploadURL(targetURL string, recursive bool, expires time.Duration, c Key := targetURL if recursive { Key = Key + recursiveSeparator - m["key"] = "" + m["key"] = m["key"] + "" } shareMessage := ShareMessage{ Expiry: expires, diff --git a/share.go b/share.go index df1a3330..c6ecc704 100644 --- a/share.go +++ b/share.go @@ -222,12 +222,14 @@ func setSharePalette(style string) { "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 }