Merge pull request #1056 from harshavardhana/share-message

Converge shareMessage into shareDuration
This commit is contained in:
Harshavardhana
2015-08-27 18:57:05 -07:00
2 changed files with 35 additions and 36 deletions
+32 -8
View File
@@ -17,13 +17,19 @@
package main
import (
"bytes"
"encoding/json"
"fmt"
"time"
"github.com/minio/mc/pkg/console"
"github.com/minio/minio/pkg/probe"
)
// shareDuration extended version of time.Duration implementing .Days() for convenience
type shareDuration struct {
duration time.Duration
duration time.Duration
presignedURL string
}
func (s shareDuration) Days() float64 {
@@ -38,13 +44,31 @@ func (s shareDuration) Hours() float64 {
return s.duration.Hours()
}
func (s shareDuration) String() string {
if s.duration.Hours() > 24 {
return fmt.Sprintf("%dd", int64(s.Days()))
}
return s.duration.String()
}
func (s shareDuration) GetDuration() time.Duration {
return s.duration
}
func (s shareDuration) String() string {
if !globalJSONFlag {
durationString := func() string {
if s.duration.Hours() > 24 {
return fmt.Sprintf("%dd", int64(s.Days()))
}
return s.duration.String()
}
return console.Colorize("Share", fmt.Sprintf("Expiry: %s\n URL: %s", durationString(), s.presignedURL))
}
shareMessageBytes, err := json.Marshal(struct {
Expires time.Duration `json:"expire-seconds"`
PresignedURL string `json:"presigned-url"`
}{
Expires: time.Duration(s.Seconds()),
PresignedURL: s.presignedURL,
})
fatalIf(probe.NewError(err), "Failed to marshal into JSON.")
// 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)
return string(shareMessageBytes)
}
+3 -28
View File
@@ -17,10 +17,7 @@
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"path/filepath"
"strings"
"time"
@@ -62,26 +59,6 @@ EXAMPLES:
`,
}
// ShareMessage container for share messages
type ShareMessage struct {
Expires shareDuration `json:"expire-seconds"`
PresignedURL string `json:"presigned-url"`
}
// String string printer for share message
func (s ShareMessage) String() string {
if !globalJSONFlag {
return console.Colorize("Share", fmt.Sprintf("Expiry: %s\n URL: %s", s.Expires, s.PresignedURL))
}
shareMessageBytes, err := json.Marshal(s)
fatalIf(probe.NewError(err), "Failed to marshal into JSON.")
// 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)
return string(shareMessageBytes)
}
func checkShareSyntax(ctx *cli.Context) {
if !ctx.Args().Present() || ctx.Args().First() == "help" || len(ctx.Args()) > 2 {
cli.ShowCommandHelpAndExit(ctx, "share", 1) // last argument is exit code
@@ -119,7 +96,7 @@ func mainShare(ctx *cli.Context) {
fatalIf(err.Trace(url), "Unable to parse argument ‘"+url+"’.")
// if recursive strip off the "..."
err = doShareCmd(stripRecursiveURL(targetURL), isURLRecursive(targetURL), shareDuration{expires})
err = doShareCmd(stripRecursiveURL(targetURL), isURLRecursive(targetURL), shareDuration{duration: expires})
fatalIf(err.Trace(targetURL), "Unable to generate URL for sharing.")
}
@@ -157,10 +134,8 @@ func doShareCmd(targetURL string, recursive bool, expires shareDuration) *probe.
if err != nil {
return err.Trace()
}
console.Println(ShareMessage{
Expires: expires,
PresignedURL: presignedURL,
}.String())
expires.presignedURL = presignedURL
console.Println(expires)
}
return nil
}