From faf3538ee57070d03a4ba52cd18c712c04b65cd0 Mon Sep 17 00:00:00 2001 From: kendavis2 Date: Fri, 8 Nov 2019 11:30:35 -0500 Subject: [PATCH] warn users when overwriting versions --- cli/cmd/pull.go | 2 +- cli/cmd/push.go | 36 +++++++++++++++++++++++++++++++----- components/catalog/file.go | 35 +++++++++++++++++++++++------------ 3 files changed, 55 insertions(+), 18 deletions(-) diff --git a/cli/cmd/pull.go b/cli/cmd/pull.go index d3be633..c97e05e 100644 --- a/cli/cmd/pull.go +++ b/cli/cmd/pull.go @@ -235,7 +235,7 @@ func Pull(catalogPath string, opt cfg.UserOptions, io models.IO) (int, int, erro //------------------------------------------------- //- Save the time the user last pulled file. //------------------------------------------------- - if err := clog.RecordPull(fileEntry.Key(), time.Now()); err != nil { + if err := clog.RecordPull(fileEntry.Key(), time.Now(), opt.Version); err != nil { logger.L.Print(err) continue } diff --git a/cli/cmd/push.go b/cli/cmd/push.go index d74d031..a790cd4 100644 --- a/cli/cmd/push.go +++ b/cli/cmd/push.go @@ -112,12 +112,30 @@ func Push(opt cfg.UserOptions, io models.IO) error { fmt.Fprintln(io.UserOutput, "]") //-------------------------------------------------------- - //- Ensure file has not been modified by another user. + //- Ensure file has not been modified by another user and + //- is the correct version. //-------------------------------------------------------- if lastModified, err := remoteComp.Store.Changed(&fileEntry, file, opt.Version); err == nil { - if !fileEntry.IsCurrent(lastModified, clog.Context) { - if !prompt.Confirm(fmt.Sprintf("Remotely stored data '%s' was modified on %s. Overwrite?", filePath, lastModified.Format("01/02/06")), prompt.Warn, io) { - fmt.Fprintf(io.UserOutput, "Skipping %s\n", filePath) + current, version := fileEntry.IsCurrent(lastModified, clog.Context) + + if opt.Version != version { + if lastModified.IsZero() { + color.New(color.FgGreen, color.Bold).Fprintf(io.UserOutput, "%s created from %s\n", formatVersion(opt.Version), formatVersion(version)) + } else { + if !prompt.Confirm(fmt.Sprintf("Overwrite %s with %s?", formatVersion(opt.Version), formatVersion(version)), prompt.Warn, io) { + fmt.Fprint(io.UserOutput, "Skipping [") + color.New(color.FgBlue).Fprintf(io.UserOutput, fileEntry.Path) + fmt.Fprintln(io.UserOutput, "]") + continue + } + } + } + + if !current { + if !prompt.Confirm(fmt.Sprintf("Remotely stored [%s] was modified %s. Overwrite?", filePath, lastModified.Format("01/02/06")), prompt.Warn, io) { + fmt.Fprint(io.UserOutput, "Skipping [") + color.New(color.FgBlue).Fprintf(io.UserOutput, fileEntry.Path) + fmt.Fprintln(io.UserOutput, "]") continue } } @@ -183,7 +201,7 @@ func Push(opt cfg.UserOptions, io models.IO) error { //------------------------------------------------- //- Save the time the user last pulled file. //------------------------------------------------- - if err := clog.RecordPull(fileEntry.Key(), time.Now().Add(time.Second*1)); err != nil { + if err := clog.RecordPull(fileEntry.Key(), time.Now().Add(time.Second*1), opt.Version); err != nil { logger.L.Print(err) continue } @@ -228,6 +246,14 @@ func Push(opt cfg.UserOptions, io models.IO) error { return nil } +func formatVersion(version string) string { + if len(version) == 0 { + return "unversioned master" + } + + return version +} + const ( storeToken = "store" deleteToken = "delete" diff --git a/components/catalog/file.go b/components/catalog/file.go index b9708ee..089bdd8 100644 --- a/components/catalog/file.go +++ b/components/catalog/file.go @@ -9,7 +9,7 @@ import ( yaml "gopkg.in/yaml.v2" ) -const name = "pulls.yml" +const name = "state.yml" // RemoveRecords ... func (c Catalog) RemoveRecords(fileName string) error { @@ -33,12 +33,12 @@ func (c Catalog) RemoveRecords(fileName string) error { } // RecordPull ... -func (c Catalog) RecordPull(fileName string, lastPull time.Time) error { +func (c Catalog) RecordPull(fileName string, lastPull time.Time, version string) error { if lastPull.IsZero() { - return errors.New("invalid file pulled time") + return errors.New("invalid time") } - pulls := map[string]time.Time{} + pulls := map[string]State{} b, err := local.Get(name, "") if err == nil { @@ -47,7 +47,12 @@ func (c Catalog) RecordPull(fileName string, lastPull time.Time) error { } } - pulls[c.ContextKey(fileName)] = lastPull.UTC() + s := State{ + Pulled: lastPull.UTC(), + Version: version, + } + + pulls[c.ContextKey(fileName)] = s b, err = yaml.Marshal(pulls) if err != nil { return err @@ -57,27 +62,33 @@ func (c Catalog) RecordPull(fileName string, lastPull time.Time) error { } // IsCurrent ... -func (f File) IsCurrent(lastChange time.Time, context string) bool { +func (f File) IsCurrent(lastChange time.Time, context string) (bool, string) { if lastChange.IsZero() { - return true + return true, "" } b, err := local.Get(name, "") if err != nil { logger.L.Print(err) - return false + return false, "" } - pulls := map[string]time.Time{} + pulls := map[string]State{} if err = yaml.Unmarshal(b, &pulls); err != nil { logger.L.Print(err) - return false + return false, "" } if filePulled, found := pulls[f.ContextKey(context)]; found { - return filePulled.After(lastChange) || filePulled.Equal(lastChange) + return filePulled.Pulled.After(lastChange) || filePulled.Pulled.Equal(lastChange), filePulled.Version } - return false + return false, "" +} + +// State contains information that is relavant to the pulled file. +type State struct { + Pulled time.Time + Version string `yaml:"version,omitempty"` }