From 3777c0039c23a101ead3a0772e646aeedca39acd Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Thu, 27 Aug 2015 18:38:01 -0700 Subject: [PATCH] Implement custom share duration to print in Days() as well --- share-duration.go | 50 +++++++++++++++++++++++++++++++++++++++++++++++ share-main.go | 14 ++++++------- 2 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 share-duration.go diff --git a/share-duration.go b/share-duration.go new file mode 100644 index 00000000..bc16d690 --- /dev/null +++ b/share-duration.go @@ -0,0 +1,50 @@ +/* + * Minio Client (C) 2014, 2015 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package main + +import ( + "fmt" + "time" +) + +// shareDuration extended version of time.Duration implementing .Days() for convenience +type shareDuration struct { + duration time.Duration +} + +func (s shareDuration) Days() float64 { + return s.duration.Hours() / 24 +} + +func (s shareDuration) Seconds() float64 { + return s.duration.Seconds() +} + +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 +} diff --git a/share-main.go b/share-main.go index f6938e99..20771e71 100644 --- a/share-main.go +++ b/share-main.go @@ -41,7 +41,7 @@ var shareCmd = cli.Command{ mc {{.Name}} - {{.Usage}} USAGE: - mc {{.Name}} TARGET [DURATION=s|m|h|d] {{if .Description}} + mc {{.Name}} TARGET [DURATION=h|m|s] {{if .Description}} DESCRIPTION: {{.Description}}{{end}}{{if .Flags}} @@ -64,14 +64,14 @@ EXAMPLES: // ShareMessage container for share messages type ShareMessage struct { - Expires time.Duration `json:"expire-seconds"` + 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("Succesfully generated shared URL with expiry %s, please share: %s", s.Expires, s.PresignedURL)) + 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.") @@ -119,7 +119,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), expires) + err = doShareCmd(stripRecursiveURL(targetURL), isURLRecursive(targetURL), shareDuration{expires}) fatalIf(err.Trace(targetURL), "Unable to generate URL for sharing.") } @@ -134,7 +134,7 @@ func getNewTargetURL(targetParser *client.URL, name string) string { } // doShareCmd share files from target -func doShareCmd(targetURL string, recursive bool, expires time.Duration) *probe.Error { +func doShareCmd(targetURL string, recursive bool, expires shareDuration) *probe.Error { clnt, err := target2Client(targetURL) if err != nil { return err.Trace() @@ -153,12 +153,12 @@ func doShareCmd(targetURL string, recursive bool, expires time.Duration) *probe. if err != nil { return err.Trace() } - presignedURL, err := newClnt.Share(expires) + presignedURL, err := newClnt.Share(expires.GetDuration()) if err != nil { return err.Trace() } console.Println(ShareMessage{ - Expires: time.Duration(int64(expires.Seconds())) * time.Second, + Expires: expires, PresignedURL: presignedURL, }.String()) }