Skip to content

Commit

Permalink
warn users when overwriting versions
Browse files Browse the repository at this point in the history
  • Loading branch information
kendavis2 committed Nov 8, 2019
1 parent 3afeead commit faf3538
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 18 deletions.
2 changes: 1 addition & 1 deletion cli/cmd/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
36 changes: 31 additions & 5 deletions cli/cmd/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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"
Expand Down
35 changes: 23 additions & 12 deletions components/catalog/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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"`
}

0 comments on commit faf3538

Please sign in to comment.