From 1a74e98b238ead83e0d8e738492b2b2c8ec8271b Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sun, 13 Sep 2015 12:13:20 -0700 Subject: [PATCH] Share migrate to V2 version moving from type map to array. --- main.go | 2 ++ share-main.go | 51 +++++++++++++++++++------------- share-v1.go | 65 +++++++++++++++++++++++++++++++++++++++++ share-v2.go | 78 +++++++++++++++++++++++++++++++++++++++++++++++++ share.go | 81 ++++++++++++++++++++------------------------------- time.go | 47 +++++++++++++++++++++++------- 6 files changed, 244 insertions(+), 80 deletions(-) create mode 100644 share-v1.go create mode 100644 share-v2.go diff --git a/main.go b/main.go index d34cc174..5009dca5 100644 --- a/main.go +++ b/main.go @@ -60,6 +60,8 @@ func migrate() { fixConfig() // Migrate session files if any. migrateSession() + // Migrate shared urls if any. + migrateSharedURLs() } // Get os/arch/platform specific information. diff --git a/share-main.go b/share-main.go index eb39e843..8eee1f56 100644 --- a/share-main.go +++ b/share-main.go @@ -66,23 +66,26 @@ EXAMPLES: // ShareMessage is container for share command on success and failure messages type ShareMessage struct { - Expiry time.Duration - URL string + Expiry time.Duration `json:"expiry"` + URL string `json:"url"` + Key string `json:"keyName"` } // String - regular colorized message func (s ShareMessage) String() string { - return console.Colorize("Share", fmt.Sprintf("Expiry: %s\n URL: %s", humanizeDuration(s.Expiry), s.URL)) + return console.Colorize("Share", fmt.Sprintf("Expiry: %s\n URL: %s\n Key:%s", timeDurationToHumanizedTime(s.Expiry), s.URL, s.Key)) } // JSON json message for share command func (s ShareMessage) JSON() string { shareMessageBytes, err := json.Marshal(struct { - Expiry time.Duration `json:"expireSeconds"` - SharedURL string `json:"sharedURL"` + Expiry humanizedTime `json:"expiry"` + URL string `json:"url"` + Key string `json:"keyName"` }{ - Expiry: time.Duration(s.Expiry.Seconds()), - SharedURL: s.URL, + Expiry: timeDurationToHumanizedTime(s.Expiry), + URL: s.URL, + Key: s.Key, }) fatalIf(probe.NewError(err), "Failed to marshal into JSON.") @@ -92,6 +95,11 @@ func (s ShareMessage) JSON() string { return string(shareMessageBytes) } +// migrateSharedURLs migrate to newest version sequentially +func migrateSharedURLs() { + migrateSharedURLsV1ToV2() +} + 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 @@ -161,38 +169,40 @@ func mainShare(ctx *cli.Context) { // doShareList list shared url's func doShareList() *probe.Error { - sURLs, err := loadSharedURLsV1() + sURLs, err := loadSharedURLsV2() if err != nil { return err.Trace() } - for url, data := range sURLs.URLs { + for i, data := range sURLs.URLs { if time.Since(data.Date) > data.Message.Expiry { - delete(sURLs.URLs, url) + sURLs.URLs = append(sURLs.URLs[:i], sURLs.URLs[i+1:]...) continue } expiry := data.Message.Expiry - time.Since(data.Date) if !globalJSONFlag { msg := console.Colorize("Share", "Name: ") - msg += console.Colorize("URL", url+"\n") + msg += console.Colorize("URL", data.Message.Key+"\n") msg += console.Colorize("Share", "Expiry: ") - msg += console.Colorize("Expires", humanizeDuration(expiry)) + msg += console.Colorize("Expires", timeDurationToHumanizedTime(expiry)) msg += "\n" console.Println(msg) continue } shareListBytes, err := json.Marshal(struct { - Expiry time.Duration `json:"expiry"` + Expiry humanizedTime `json:"expiry"` URL string `json:"url"` + Key string `json:"keyName"` }{ - Expiry: time.Duration(expiry.Seconds()), - URL: url, + Expiry: timeDurationToHumanizedTime(expiry), + URL: data.Message.URL, + Key: data.Message.Key, }) if err != nil { return probe.NewError(err) } console.Println(string(shareListBytes)) } - if err := saveSharedURLsV1(sURLs); err != nil { + if err := saveSharedURLsV2(sURLs); err != nil { return err.Trace() } return nil @@ -201,7 +211,7 @@ func doShareList() *probe.Error { // doShareURL share files from target func doShareURL(targetURL string, recursive bool, expires time.Duration) *probe.Error { shareDate := time.Now().UTC() - sURLs, err := loadSharedURLsV1() + sURLs, err := loadSharedURLsV2() if err != nil { return err.Trace() } @@ -234,17 +244,18 @@ func doShareURL(targetURL string, recursive bool, expires time.Duration) *probe. shareMessage := ShareMessage{ Expiry: expires, URL: sharedURL, + Key: newClnt.URL().String(), } - sURLs.URLs[newClnt.URL().String()] = struct { + sURLs.URLs = append(sURLs.URLs, struct { Date time.Time Message ShareMessage }{ Date: shareDate, Message: shareMessage, - } + }) Prints("%s\n", shareMessage) } - saveSharedURLsV1(sURLs) + saveSharedURLsV2(sURLs) return nil } diff --git a/share-v1.go b/share-v1.go new file mode 100644 index 00000000..2cc0b270 --- /dev/null +++ b/share-v1.go @@ -0,0 +1,65 @@ +/* + * 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 ( + "os" + "time" + + "github.com/minio/minio/pkg/probe" + "github.com/minio/minio/pkg/quick" +) + +type sharedURLsV1 struct { + Version string + URLs map[string]struct { + Date time.Time + Message ShareMessage + } +} + +func loadSharedURLsV1() (*sharedURLsV1, *probe.Error) { + sharedURLsDataFile, err := getSharedURLsDataFile() + if err != nil { + return nil, err.Trace() + } + if _, err := os.Stat(sharedURLsDataFile); err != nil { + return nil, probe.NewError(err) + } + + qs, err := quick.New(newSharedURLsV1()) + if err != nil { + return nil, err.Trace() + } + err = qs.Load(sharedURLsDataFile) + if err != nil { + return nil, err.Trace(sharedURLsDataFile) + } + s := qs.Data().(*sharedURLsV1) + return s, nil +} + +func newSharedURLsV1() *sharedURLsV1 { + s := &sharedURLsV1{ + Version: "1.0.0", + URLs: make(map[string]struct { + Date time.Time + Message ShareMessage + }), + } + return s +} diff --git a/share-v2.go b/share-v2.go new file mode 100644 index 00000000..c7adaaf3 --- /dev/null +++ b/share-v2.go @@ -0,0 +1,78 @@ +/* + * 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 ( + "os" + "time" + + "github.com/minio/minio/pkg/probe" + "github.com/minio/minio/pkg/quick" +) + +type sharedURLsV2 struct { + Version string + URLs []struct { + Date time.Time + Message ShareMessage + } +} + +func loadSharedURLsV2() (*sharedURLsV2, *probe.Error) { + sharedURLsDataFile, err := getSharedURLsDataFile() + if err != nil { + return nil, err.Trace() + } + if _, err := os.Stat(sharedURLsDataFile); err != nil { + return nil, probe.NewError(err) + } + + qs, err := quick.New(newSharedURLsV2()) + if err != nil { + return nil, err.Trace() + } + err = qs.Load(sharedURLsDataFile) + if err != nil { + return nil, err.Trace(sharedURLsDataFile) + } + s := qs.Data().(*sharedURLsV2) + return s, nil +} + +func saveSharedURLsV2(s *sharedURLsV2) *probe.Error { + qs, err := quick.New(s) + if err != nil { + return err.Trace() + } + sharedURLsDataFile, err := getSharedURLsDataFile() + if err != nil { + return err.Trace() + } + return qs.Save(sharedURLsDataFile).Trace(sharedURLsDataFile) +} + +func newSharedURLsV2() *sharedURLsV2 { + var urls []struct { + Date time.Time + Message ShareMessage + } + s := &sharedURLsV2{ + Version: "1.1.0", + URLs: urls, + } + return s +} diff --git a/share.go b/share.go index 6a777cd6..9ccc9b8d 100644 --- a/share.go +++ b/share.go @@ -17,31 +17,45 @@ package main import ( + "encoding/json" "os" "path/filepath" - "time" "github.com/minio/minio/pkg/probe" - "github.com/minio/minio/pkg/quick" ) -type sharedURLs struct { - Version string - URLs map[string]struct { - Date time.Time - Message ShareMessage - } +func newSharedURLs() *sharedURLsV2 { + return newSharedURLsV2() } -func newSharedURLs() *sharedURLs { - s := &sharedURLs{ - Version: "1.0.0", - URLs: make(map[string]struct { - Date time.Time - Message ShareMessage - }), +func migrateSharedURLsV1ToV2() { + if !isSharedURLsDataFileExists() { + return + } + sURLsV2 := newSharedURLsV2() + + // try to load latest version if possible + sURLsV2, err := loadSharedURLsV2() + if err != nil { + switch err.ToGoError().(type) { + case *json.UnmarshalTypeError: + // try to load V1 if possible + var sURLsV1 *sharedURLsV1 + sURLsV1, err = loadSharedURLsV1() + fatalIf(err.Trace(), "Unable to load shared url version ‘1.0.0’.") + if sURLsV1.Version != "1.0.0" { + fatalIf(errDummy().Trace(), "Invalid version loaded ‘"+sURLsV1.Version+"’.") + } + for key, value := range sURLsV1.URLs { + value.Message.Key = key + sURLsV2.URLs = append(sURLsV2.URLs, value) + } + err = saveSharedURLsV2(sURLsV2) + fatalIf(err.Trace(), "Unable to save new shared url version ‘1.1.0’.") + default: + fatalIf(err.Trace(), "Unable to load shared url version ‘1.1.0’.") + } } - return s } func getSharedURLsDataDir() (string, *probe.Error) { @@ -97,41 +111,8 @@ func isSharedURLsDataFileExists() bool { } func createSharedURLsDataFile() *probe.Error { - if err := saveSharedURLsV1(newSharedURLs()); err != nil { + if err := saveSharedURLsV2(newSharedURLs()); err != nil { return err.Trace() } return nil } - -func loadSharedURLsV1() (*sharedURLs, *probe.Error) { - sharedURLsDataFile, err := getSharedURLsDataFile() - if err != nil { - return nil, err.Trace() - } - if _, err := os.Stat(sharedURLsDataFile); err != nil { - return nil, probe.NewError(err) - } - - qs, err := quick.New(newSharedURLs()) - if err != nil { - return nil, err.Trace() - } - err = qs.Load(sharedURLsDataFile) - if err != nil { - return nil, err.Trace(sharedURLsDataFile) - } - s := qs.Data().(*sharedURLs) - return s, nil -} - -func saveSharedURLsV1(s *sharedURLs) *probe.Error { - qs, err := quick.New(s) - if err != nil { - return err.Trace() - } - sharedURLsDataFile, err := getSharedURLsDataFile() - if err != nil { - return err.Trace() - } - return qs.Save(sharedURLsDataFile).Trace(sharedURLsDataFile) -} diff --git a/time.go b/time.go index 2923de6f..ed5925d2 100644 --- a/time.go +++ b/time.go @@ -22,26 +22,53 @@ import ( "time" ) -// humanizeDuration humanizes time.Duration output to a meaningful value, -// golang's default ``time.Duration`` output is badly formatted and unreadable. -func humanizeDuration(duration time.Duration) string { +type humanizedTime struct { + Days int64 `json:"days,omitempty"` + Hours int64 `json:"hours,omitempty"` + Minutes int64 `json:"minutes,omitempty"` + Seconds int64 `json:"seconds,omitempty"` +} + +// String() humanizes humanizedTime to human readable, +func (r humanizedTime) String() string { + if r.Minutes == 0 { + return fmt.Sprintf("%d seconds", r.Seconds) + } + if r.Hours == 0 { + return fmt.Sprintf("%d minutes %d seconds", r.Minutes, r.Seconds) + } + if r.Days == 0 { + return fmt.Sprintf("%d hours %d minutes %d seconds", r.Hours, r.Minutes, r.Seconds) + } + return fmt.Sprintf("%d days %d hours %d minutes %d seconds", r.Days, r.Hours, r.Minutes, r.Seconds) +} + +func timeDurationToHumanizedTime(duration time.Duration) humanizedTime { + r := humanizedTime{} if duration.Seconds() < 60.0 { - return fmt.Sprintf("%d seconds", int64(duration.Seconds())) + r.Seconds = int64(duration.Seconds()) + return r } if duration.Minutes() < 60.0 { remainingSeconds := math.Mod(duration.Seconds(), 60) - return fmt.Sprintf("%d minutes %d seconds", int64(duration.Minutes()), int64(remainingSeconds)) + r.Seconds = int64(remainingSeconds) + r.Minutes = int64(duration.Minutes()) + return r } if duration.Hours() < 24.0 { remainingMinutes := math.Mod(duration.Minutes(), 60) remainingSeconds := math.Mod(duration.Seconds(), 60) - return fmt.Sprintf("%d hours %d minutes %d seconds", - int64(duration.Hours()), int64(remainingMinutes), int64(remainingSeconds)) + r.Seconds = int64(remainingSeconds) + r.Minutes = int64(remainingMinutes) + r.Hours = int64(duration.Hours()) + return r } remainingHours := math.Mod(duration.Hours(), 24) remainingMinutes := math.Mod(duration.Minutes(), 60) remainingSeconds := math.Mod(duration.Seconds(), 60) - return fmt.Sprintf("%d days %d hours %d minutes %d seconds", - int64(duration.Hours()/24), int64(remainingHours), - int64(remainingMinutes), int64(remainingSeconds)) + r.Hours = int64(remainingHours) + r.Minutes = int64(remainingMinutes) + r.Seconds = int64(remainingSeconds) + r.Days = int64(duration.Hours() / 24) + return r }