Skip to content

Commit

Permalink
fix edit with query loop, allow continuation when not submitting prev…
Browse files Browse the repository at this point in the history
…ious issue
  • Loading branch information
coryb committed Sep 18, 2017
1 parent 098eb99 commit 0ba8aa0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
30 changes: 16 additions & 14 deletions jiracli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,17 @@ func (o *CommonOptions) editFile(fileName string) (changes bool, err error) {
return false, err
}

var EditLoopAbort = fmt.Errorf("Edit Loop aborted by request")

func EditLoop(opts *CommonOptions, input interface{}, output interface{}, submit func() error) error {
tmpFile, err := tmpTemplate(opts.Template.Value, input)
if err != nil {
return err
}

confirm := func(msg string) (answer bool) {
confirm := func(dflt bool, msg string) (answer bool) {
survey.AskOne(
&survey.Confirm{Message: msg, Default: true},
&survey.Confirm{Message: msg, Default: dflt},
&answer,
nil,
)
Expand All @@ -279,14 +281,14 @@ func EditLoop(opts *CommonOptions, input interface{}, output interface{}, submit
changes, err := opts.editFile(tmpFile)
if err != nil {
log.Error(err.Error())
if confirm("Editor reported an error, edit again?") {
if confirm(true, "Editor reported an error, edit again?") {
continue
}
panic(Exit{Code: 1})
return EditLoopAbort
}
if !changes {
if !confirm("No changes detected, submit anyway?") {
panic(Exit{Code: 1})
if !confirm(false, "No changes detected, submit anyway?") {
return EditLoopAbort
}
}
}
Expand Down Expand Up @@ -319,35 +321,35 @@ func EditLoop(opts *CommonOptions, input interface{}, output interface{}, submit
var raw interface{}
if err := yaml.Unmarshal(data, &raw); err != nil {
log.Error(err.Error())
if confirm("Invalid YAML syntax, edit again?") {
if confirm(true, "Invalid YAML syntax, edit again?") {
continue
}
panic(Exit{Code: 1})
return EditLoopAbort
}
yamlFixup(&raw)
fixedYAML, err := yaml.Marshal(&raw)
if err != nil {
log.Error(err.Error())
if confirm("Invalid YAML syntax, edit again?") {
if confirm(true, "Invalid YAML syntax, edit again?") {
continue
}
panic(Exit{Code: 1})
return EditLoopAbort
}

if err := yaml.Unmarshal(fixedYAML, output); err != nil {
log.Error(err.Error())
if confirm("Invalid YAML syntax, edit again?") {
if confirm(true, "Invalid YAML syntax, edit again?") {
continue
}
panic(Exit{Code: 1})
return EditLoopAbort
}
// submit template
if err := submit(); err != nil {
log.Error(err.Error())
if confirm("Jira reported an error, edit again?") {
if confirm(true, "Jira reported an error, edit again?") {
continue
}
panic(Exit{Code: 1})
return EditLoopAbort
}
break
}
Expand Down
21 changes: 19 additions & 2 deletions jiracmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/coryb/figtree"
"github.com/coryb/oreo"

"gopkg.in/AlecAivazis/survey.v1"
"gopkg.in/Netflix-Skunkworks/go-jira.v1"
"gopkg.in/Netflix-Skunkworks/go-jira.v1/jiracli"
"gopkg.in/Netflix-Skunkworks/go-jira.v1/jiradata"
Expand Down Expand Up @@ -106,7 +106,7 @@ func CmdEdit(o *oreo.Client, globals *jiracli.GlobalOptions, opts *EditOptions)
if err != nil {
return err
}
for _, issueData := range results.Issues {
for i, issueData := range results.Issues {
editMeta, err := jira.GetIssueEditMeta(o, globals.Endpoint.Value, issueData.Key)
if err != nil {
return err
Expand All @@ -121,6 +121,23 @@ func CmdEdit(o *oreo.Client, globals *jiracli.GlobalOptions, opts *EditOptions)
err = jiracli.EditLoop(&opts.CommonOptions, &input, &issueUpdate, func() error {
return jira.EditIssue(o, globals.Endpoint.Value, issueData.Key, &issueUpdate)
})
if err == jiracli.EditLoopAbort {
if len(results.Issues) > i+1 {
var answer bool
survey.AskOne(
&survey.Confirm{
Message: fmt.Sprintf("Continue to edit next issue %s?", results.Issues[i+1].Key),
Default: true,
},
&answer,
nil,
)
if answer {
continue
}
panic(jiracli.Exit{1})
}
}
if err != nil {
return err
}
Expand Down

0 comments on commit 0ba8aa0

Please sign in to comment.