Implement custom share duration to print in Days() as well

This commit is contained in:
Harshavardhana
2015-08-27 18:38:01 -07:00
parent 59031593dd
commit 3777c0039c
2 changed files with 57 additions and 7 deletions
+50
View File
@@ -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
}
+7 -7
View File
@@ -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())
}