Skip to content

Commit

Permalink
if no changes are made on edit templates then abort edit
Browse files Browse the repository at this point in the history
  • Loading branch information
Cory Bennett committed Sep 12, 2015
1 parent 0ede380 commit e69b65c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
16 changes: 15 additions & 1 deletion jira/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (c *Cli) getTemplate(name string) string {
} else {
return readFile(file)
}
}
}
return all_templates[name]
} else {
return readFile(file)
Expand All @@ -222,6 +222,9 @@ func (c *Cli) editTemplate(template string, tmpFilePrefix string, templateData m
log.Error("Failed to rename %s to %s: %s", fh.Name(), fmt.Sprintf("%s.yml", fh.Name()), err)
return err
}
defer func() {
os.Remove(tmpFileName)
}()

err = runTemplate(template, templateData, fh)
if err != nil {
Expand All @@ -246,6 +249,12 @@ func (c *Cli) editTemplate(template string, tmpFilePrefix string, templateData m
editing = false
}

tmpFileNameOrig := fmt.Sprintf("%s-orig")
copyFile(tmpFileName,tmpFileNameOrig)
defer func() {
os.Remove(tmpFileNameOrig)
}()

for true {
if editing {
shell, _ := shellquote.Split(editor)
Expand All @@ -260,6 +269,11 @@ func (c *Cli) editTemplate(template string, tmpFilePrefix string, templateData m
}
return err
}
diff := exec.Command("diff", "-q", tmpFileNameOrig, tmpFileName)
if err := diff.Run(); err == nil {
log.Info("No changes found, aborting")
return fmt.Errorf("No changes found, aborting")
}
}

edited := make(map[string]interface{})
Expand Down
28 changes: 23 additions & 5 deletions jira/cli/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ func readFile(file string) string {
return string(bytes)
}

func copyFile(src, dst string) (err error){
var s, d *os.File;
if s, err = os.Open(src); err == nil {
defer s.Close()
if d, err = os.Create(dst); err == nil {
if _, err = io.Copy(d, s); err != nil {
d.Close()
return
}
return d.Close()
}
}
return
}


func fuzzyAge(start string) (string, error) {
if t, err := time.Parse("2006-01-02T15:04:05.000-0700", start); err != nil {
return "", err
Expand Down Expand Up @@ -246,23 +262,25 @@ func yamlFixup(data interface{}) (interface{}, error) {
}
return copy, nil
case map[string]interface{}:
copy := make(map[string]interface{})
for k, v := range d {
if fixed, err := yamlFixup(v); err != nil {
return nil, err
} else if fixed != nil {
d[k] = fixed
copy[k] = fixed
}
}
return d, nil
return copy, nil
case []interface{}:
for i, val := range d {
copy := make([]interface{}, 0, len(d))
for _, val := range d {
if fixed, err := yamlFixup(val); err != nil {
return nil, err
} else if fixed != nil {
d[i] = fixed
copy = append(copy, fixed)
}
}
return data, nil
return copy, nil
case string:
if d == "" {
return nil, nil
Expand Down

0 comments on commit e69b65c

Please sign in to comment.