From ed70bbb7e39cca21ef465fef5a6b9cf67e8adc9f Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Tue, 11 Aug 2015 23:17:23 -0700 Subject: [PATCH] Print json output for share urls as well --- print-structs.go | 24 ++++++++++++++++++++++++ share-main.go | 3 +-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/print-structs.go b/print-structs.go index 16f3b35d..50a7d510 100644 --- a/print-structs.go +++ b/print-structs.go @@ -18,9 +18,11 @@ package main import ( + "bytes" "encoding/json" "fmt" "strings" + "time" "github.com/minio/mc/pkg/console" ) @@ -160,3 +162,25 @@ func (s MirrorMessage) String() string { } return console.JSON(string(mirrorMessageBytes) + "\n") } + +// ShareMessage container for share messages +type ShareMessage struct { + Version string `json:"version"` + Expires time.Duration `json:"expires"` + PresignedURL string `json:"presigned-url"` +} + +// String string printer for share message +func (s ShareMessage) String() string { + if !globalJSONFlag { + return fmt.Sprintf("Succesfully generated shared URL with expiry %s, please share: %s\n", s.Expires, s.PresignedURL) + } + s.Version = "1.0.0" + shareMessageBytes, err := json.MarshalIndent(s, "", "\t") + if err != nil { + panic(err) + } + // json encoding escapes ampersand into its unicode character which is not usable directly for share and fails with cloud storage. + shareMessageBytes = bytes.Replace(shareMessageBytes, []byte("\\u0026"), []byte("&"), -1) + return console.JSON("%s\n", string(shareMessageBytes)) +} diff --git a/share-main.go b/share-main.go index 2010bb4d..b79d497a 100644 --- a/share-main.go +++ b/share-main.go @@ -18,7 +18,6 @@ package main import ( "errors" - "fmt" "path/filepath" "strings" "time" @@ -114,7 +113,7 @@ func doShareCmd(targetURL string, recursive bool, expires time.Duration) *probe. if err != nil { return err.Trace() } - console.PrintC(fmt.Sprintf("Succesfully generated shared URL with expiry %s, please copy: %s\n", expires, presignedURL)) + console.PrintC(ShareMessage{Expires: expires, PresignedURL: presignedURL}) } return nil }