Merge pull request #671 from MichaelMure/close-on-error

commands: proper backend close on RunE error
This commit is contained in:
Michael Muré 2021-05-10 11:14:26 +02:00 committed by GitHub
commit 13d9632fb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
35 changed files with 174 additions and 200 deletions

View File

@ -19,13 +19,12 @@ func newAddCommand() *cobra.Command {
options := addOptions{}
cmd := &cobra.Command{
Use: "add",
Short: "Create a new bug.",
PreRunE: loadBackendEnsureUser(env),
PostRunE: closeBackend(env),
RunE: func(cmd *cobra.Command, args []string) error {
Use: "add",
Short: "Create a new bug.",
PreRunE: loadBackendEnsureUser(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runAdd(env, options)
},
}),
}
flags := cmd.Flags()

View File

@ -10,13 +10,12 @@ func newBridgeCommand() *cobra.Command {
env := newEnv()
cmd := &cobra.Command{
Use: "bridge",
Short: "Configure and use bridges to other bug trackers.",
PreRunE: loadBackend(env),
PostRunE: closeBackend(env),
RunE: func(cmd *cobra.Command, args []string) error {
Use: "bridge",
Short: "Configure and use bridges to other bug trackers.",
PreRunE: loadBackend(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runBridge(env)
},
}),
Args: cobra.NoArgs,
}

View File

@ -16,13 +16,12 @@ func newBridgeAuthCommand() *cobra.Command {
env := newEnv()
cmd := &cobra.Command{
Use: "auth",
Short: "List all known bridge authentication credentials.",
PreRunE: loadBackend(env),
PostRunE: closeBackend(env),
RunE: func(cmd *cobra.Command, args []string) error {
Use: "auth",
Short: "List all known bridge authentication credentials.",
PreRunE: loadBackend(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runBridgeAuth(env)
},
}),
Args: cobra.NoArgs,
}

View File

@ -27,13 +27,12 @@ func newBridgeAuthAddTokenCommand() *cobra.Command {
options := bridgeAuthAddTokenOptions{}
cmd := &cobra.Command{
Use: "add-token [TOKEN]",
Short: "Store a new token",
PreRunE: loadBackendEnsureUser(env),
PostRunE: closeBackend(env),
RunE: func(cmd *cobra.Command, args []string) error {
Use: "add-token [TOKEN]",
Short: "Store a new token",
PreRunE: loadBackendEnsureUser(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runBridgeAuthAddToken(env, options, args)
},
}),
Args: cobra.MaximumNArgs(1),
}

View File

@ -15,13 +15,12 @@ func newBridgeAuthShow() *cobra.Command {
env := newEnv()
cmd := &cobra.Command{
Use: "show",
Short: "Display an authentication credential.",
PreRunE: loadBackend(env),
PostRunE: closeBackend(env),
RunE: func(cmd *cobra.Command, args []string) error {
Use: "show",
Short: "Display an authentication credential.",
PreRunE: loadBackend(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runBridgeAuthShow(env, args)
},
}),
Args: cobra.ExactArgs(1),
}

View File

@ -85,11 +85,10 @@ git bug bridge configure \
--target=github \
--url=https://github.com/michaelmure/git-bug \
--token=$(TOKEN)`,
PreRunE: loadBackend(env),
PostRunE: closeBackend(env),
RunE: func(cmd *cobra.Command, args []string) error {
PreRunE: loadBackend(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runBridgeConfigure(env, options)
},
}),
}
flags := cmd.Flags()

View File

@ -26,13 +26,12 @@ func newBridgePullCommand() *cobra.Command {
options := bridgePullOptions{}
cmd := &cobra.Command{
Use: "pull [NAME]",
Short: "Pull updates.",
PreRunE: loadBackend(env),
PostRunE: closeBackend(env),
RunE: func(cmd *cobra.Command, args []string) error {
Use: "pull [NAME]",
Short: "Pull updates.",
PreRunE: loadBackend(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runBridgePull(env, options, args)
},
}),
Args: cobra.MaximumNArgs(1),
}

View File

@ -17,13 +17,12 @@ func newBridgePushCommand() *cobra.Command {
env := newEnv()
cmd := &cobra.Command{
Use: "push [NAME]",
Short: "Push updates.",
PreRunE: loadBackendEnsureUser(env),
PostRunE: closeBackend(env),
RunE: func(cmd *cobra.Command, args []string) error {
Use: "push [NAME]",
Short: "Push updates.",
PreRunE: loadBackendEnsureUser(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runBridgePush(env, args)
},
}),
Args: cobra.MaximumNArgs(1),
}

View File

@ -10,13 +10,12 @@ func newBridgeRm() *cobra.Command {
env := newEnv()
cmd := &cobra.Command{
Use: "rm NAME",
Short: "Delete a configured bridge.",
PreRunE: loadBackend(env),
PostRunE: closeBackend(env),
RunE: func(cmd *cobra.Command, args []string) error {
Use: "rm NAME",
Short: "Delete a configured bridge.",
PreRunE: loadBackend(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runBridgeRm(env, args)
},
}),
Args: cobra.ExactArgs(1),
}

View File

@ -12,13 +12,12 @@ func newCommentCommand() *cobra.Command {
env := newEnv()
cmd := &cobra.Command{
Use: "comment [ID]",
Short: "Display or add comments to a bug.",
PreRunE: loadBackend(env),
PostRunE: closeBackend(env),
RunE: func(cmd *cobra.Command, args []string) error {
Use: "comment [ID]",
Short: "Display or add comments to a bug.",
PreRunE: loadBackend(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runComment(env, args)
},
}),
}
cmd.AddCommand(newCommentAddCommand())

View File

@ -19,13 +19,12 @@ func newCommentAddCommand() *cobra.Command {
options := commentAddOptions{}
cmd := &cobra.Command{
Use: "add [ID]",
Short: "Add a new comment to a bug.",
PreRunE: loadBackendEnsureUser(env),
PostRunE: closeBackend(env),
RunE: func(cmd *cobra.Command, args []string) error {
Use: "add [ID]",
Short: "Add a new comment to a bug.",
PreRunE: loadBackendEnsureUser(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runCommentAdd(env, options, args)
},
}),
}
flags := cmd.Flags()

View File

@ -17,14 +17,13 @@ func newCommentEditCommand() *cobra.Command {
options := commentEditOptions{}
cmd := &cobra.Command{
Use: "edit [COMMENT_ID]",
Short: "Edit an existing comment on a bug.",
Args: cobra.ExactArgs(1),
PreRunE: loadBackendEnsureUser(env),
PostRunE: closeBackend(env),
RunE: func(cmd *cobra.Command, args []string) error {
Use: "edit [COMMENT_ID]",
Short: "Edit an existing comment on a bug.",
Args: cobra.ExactArgs(1),
PreRunE: loadBackendEnsureUser(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runCommentEdit(env, options, args)
},
}),
}
flags := cmd.Flags()

View File

@ -17,11 +17,10 @@ git bug comment
git bug status
git bug deselect
`,
PreRunE: loadBackend(env),
PostRunE: closeBackend(env),
RunE: func(cmd *cobra.Command, args []string) error {
PreRunE: loadBackend(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runDeselect(env)
},
}),
}
return cmd

View File

@ -136,15 +136,23 @@ func loadBackendEnsureUser(env *Env) func(*cobra.Command, []string) error {
}
}
// closeBackend is a post-run function that will close the backend properly
// closeBackend is a wrapper for a RunE function that will close the backend properly
// if it has been opened.
func closeBackend(env *Env) func(*cobra.Command, []string) error {
// This wrapper style is necessary because a Cobra PostE function does not run if RunE return an error.
func closeBackend(env *Env, runE func(cmd *cobra.Command, args []string) error) func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
errRun := runE(cmd, args)
if env.backend == nil {
return nil
}
err := env.backend.Close()
env.backend = nil
// prioritize the RunE error
if errRun != nil {
return errRun
}
return err
}
}

View File

@ -10,13 +10,12 @@ func newLabelCommand() *cobra.Command {
env := newEnv()
cmd := &cobra.Command{
Use: "label [ID]",
Short: "Display, add or remove labels to/from a bug.",
PreRunE: loadBackend(env),
PostRunE: closeBackend(env),
RunE: func(cmd *cobra.Command, args []string) error {
Use: "label [ID]",
Short: "Display, add or remove labels to/from a bug.",
PreRunE: loadBackend(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runLabel(env, args)
},
}),
}
cmd.AddCommand(newLabelAddCommand())

View File

@ -11,13 +11,12 @@ func newLabelAddCommand() *cobra.Command {
env := newEnv()
cmd := &cobra.Command{
Use: "add [ID] LABEL...",
Short: "Add a label to a bug.",
PreRunE: loadBackendEnsureUser(env),
PostRunE: closeBackend(env),
RunE: func(cmd *cobra.Command, args []string) error {
Use: "add [ID] LABEL...",
Short: "Add a label to a bug.",
PreRunE: loadBackendEnsureUser(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runLabelAdd(env, args)
},
}),
}
return cmd

View File

@ -11,13 +11,12 @@ func newLabelRmCommand() *cobra.Command {
env := newEnv()
cmd := &cobra.Command{
Use: "rm [ID] LABEL...",
Short: "Remove a label from a bug.",
PreRunE: loadBackend(env),
PostRunE: closeBackend(env),
RunE: func(cmd *cobra.Command, args []string) error {
Use: "rm [ID] LABEL...",
Short: "Remove a label from a bug.",
PreRunE: loadBackend(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runLabelRm(env, args)
},
}),
}
return cmd

View File

@ -8,13 +8,12 @@ func newLsIdCommand() *cobra.Command {
env := newEnv()
cmd := &cobra.Command{
Use: "ls-id [PREFIX]",
Short: "List bug identifiers.",
PreRunE: loadBackend(env),
PostRunE: closeBackend(env),
RunE: func(cmd *cobra.Command, args []string) error {
Use: "ls-id [PREFIX]",
Short: "List bug identifiers.",
PreRunE: loadBackend(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runLsId(env, args)
},
}),
}
return cmd

View File

@ -13,11 +13,10 @@ func newLsLabelCommand() *cobra.Command {
Long: `List valid labels.
Note: in the future, a proper label policy could be implemented where valid labels are defined in a configuration file. Until that, the default behavior is to return the list of labels already used.`,
PreRunE: loadBackend(env),
PostRunE: closeBackend(env),
RunE: func(cmd *cobra.Command, args []string) error {
PreRunE: loadBackend(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runLsLabel(env)
},
}),
}
return cmd

View File

@ -52,11 +52,10 @@ git bug ls "foo bar" baz
Use queries, flags, and full text search:
git bug ls status:open --by creation "foo bar" baz
`,
PreRunE: loadBackend(env),
PostRunE: closeBackend(env),
RunE: func(cmd *cobra.Command, args []string) error {
PreRunE: loadBackend(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runLs(env, options, args)
},
}),
}
flags := cmd.Flags()

View File

@ -12,13 +12,12 @@ func newPullCommand() *cobra.Command {
env := newEnv()
cmd := &cobra.Command{
Use: "pull [REMOTE]",
Short: "Pull bugs update from a git remote.",
PreRunE: loadBackend(env),
PostRunE: closeBackend(env),
RunE: func(cmd *cobra.Command, args []string) error {
Use: "pull [REMOTE]",
Short: "Pull bugs update from a git remote.",
PreRunE: loadBackend(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runPull(env, args)
},
}),
}
return cmd

View File

@ -10,13 +10,12 @@ func newPushCommand() *cobra.Command {
env := newEnv()
cmd := &cobra.Command{
Use: "push [REMOTE]",
Short: "Push bugs update to a git remote.",
PreRunE: loadBackend(env),
PostRunE: closeBackend(env),
RunE: func(cmd *cobra.Command, args []string) error {
Use: "push [REMOTE]",
Short: "Push bugs update to a git remote.",
PreRunE: loadBackend(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runPush(env, args)
},
}),
}
return cmd

View File

@ -10,14 +10,13 @@ func newRmCommand() *cobra.Command {
env := newEnv()
cmd := &cobra.Command{
Use: "rm ID",
Short: "Remove an existing bug.",
Long: "Remove an existing bug in the local repository. Note removing bugs that were imported from bridges will not remove the bug on the remote, and will only remove the local copy of the bug.",
PreRunE: loadBackendEnsureUser(env),
PostRunE: closeBackend(env),
RunE: func(cmd *cobra.Command, args []string) error {
Use: "rm ID",
Short: "Remove an existing bug.",
Long: "Remove an existing bug in the local repository. Note removing bugs that were imported from bridges will not remove the bug on the remote, and will only remove the local copy of the bug.",
PreRunE: loadBackendEnsureUser(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runRm(env, args)
},
}),
}
flags := cmd.Flags()

View File

@ -27,11 +27,10 @@ instead of
The complementary command is "git bug deselect" performing the opposite operation.
`,
PreRunE: loadBackend(env),
PostRunE: closeBackend(env),
RunE: func(cmd *cobra.Command, args []string) error {
PreRunE: loadBackend(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runSelect(env, args)
},
}),
}
return cmd

View File

@ -23,13 +23,12 @@ func newShowCommand() *cobra.Command {
options := showOptions{}
cmd := &cobra.Command{
Use: "show [ID]",
Short: "Display the details of a bug.",
PreRunE: loadBackend(env),
PostRunE: closeBackend(env),
RunE: func(cmd *cobra.Command, args []string) error {
Use: "show [ID]",
Short: "Display the details of a bug.",
PreRunE: loadBackend(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runShow(env, options, args)
},
}),
}
flags := cmd.Flags()

View File

@ -9,13 +9,12 @@ func newStatusCommand() *cobra.Command {
env := newEnv()
cmd := &cobra.Command{
Use: "status [ID]",
Short: "Display or change a bug status.",
PreRunE: loadBackend(env),
PostRunE: closeBackend(env),
RunE: func(cmd *cobra.Command, args []string) error {
Use: "status [ID]",
Short: "Display or change a bug status.",
PreRunE: loadBackend(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runStatus(env, args)
},
}),
}
cmd.AddCommand(newStatusCloseCommand())

View File

@ -9,13 +9,12 @@ func newStatusCloseCommand() *cobra.Command {
env := newEnv()
cmd := &cobra.Command{
Use: "close [ID]",
Short: "Mark a bug as closed.",
PreRunE: loadBackendEnsureUser(env),
PostRunE: closeBackend(env),
RunE: func(cmd *cobra.Command, args []string) error {
Use: "close [ID]",
Short: "Mark a bug as closed.",
PreRunE: loadBackendEnsureUser(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runStatusClose(env, args)
},
}),
}
return cmd

View File

@ -9,13 +9,12 @@ func newStatusOpenCommand() *cobra.Command {
env := newEnv()
cmd := &cobra.Command{
Use: "open [ID]",
Short: "Mark a bug as open.",
PreRunE: loadBackendEnsureUser(env),
PostRunE: closeBackend(env),
RunE: func(cmd *cobra.Command, args []string) error {
Use: "open [ID]",
Short: "Mark a bug as open.",
PreRunE: loadBackendEnsureUser(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runStatusOpen(env, args)
},
}),
}
return cmd

View File

@ -10,14 +10,13 @@ func newTermUICommand() *cobra.Command {
env := newEnv()
cmd := &cobra.Command{
Use: "termui",
Aliases: []string{"tui"},
Short: "Launch the terminal UI.",
PreRunE: loadBackendEnsureUser(env),
PostRunE: closeBackend(env),
RunE: func(cmd *cobra.Command, args []string) error {
Use: "termui",
Aliases: []string{"tui"},
Short: "Launch the terminal UI.",
PreRunE: loadBackendEnsureUser(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runTermUI(env)
},
}),
}
return cmd

View File

@ -9,13 +9,12 @@ func newTitleCommand() *cobra.Command {
env := newEnv()
cmd := &cobra.Command{
Use: "title [ID]",
Short: "Display or change a title of a bug.",
PreRunE: loadBackend(env),
PostRunE: closeBackend(env),
RunE: func(cmd *cobra.Command, args []string) error {
Use: "title [ID]",
Short: "Display or change a title of a bug.",
PreRunE: loadBackend(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runTitle(env, args)
},
}),
}
cmd.AddCommand(newTitleEditCommand())

View File

@ -18,13 +18,12 @@ func newTitleEditCommand() *cobra.Command {
options := titleEditOptions{}
cmd := &cobra.Command{
Use: "edit [ID]",
Short: "Edit a title of a bug.",
PreRunE: loadBackendEnsureUser(env),
PostRunE: closeBackend(env),
RunE: func(cmd *cobra.Command, args []string) error {
Use: "edit [ID]",
Short: "Edit a title of a bug.",
PreRunE: loadBackendEnsureUser(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runTitleEdit(env, options, args)
},
}),
}
flags := cmd.Flags()

View File

@ -18,13 +18,12 @@ func newUserCommand() *cobra.Command {
options := userOptions{}
cmd := &cobra.Command{
Use: "user [USER-ID]",
Short: "Display or change the user identity.",
PreRunE: loadBackendEnsureUser(env),
PostRunE: closeBackend(env),
RunE: func(cmd *cobra.Command, args []string) error {
Use: "user [USER-ID]",
Short: "Display or change the user identity.",
PreRunE: loadBackendEnsureUser(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runUser(env, options, args)
},
}),
}
cmd.AddCommand(newUserAdoptCommand())

View File

@ -8,14 +8,13 @@ func newUserAdoptCommand() *cobra.Command {
env := newEnv()
cmd := &cobra.Command{
Use: "adopt USER-ID",
Short: "Adopt an existing identity as your own.",
Args: cobra.ExactArgs(1),
PreRunE: loadBackend(env),
PostRunE: closeBackend(env),
RunE: func(cmd *cobra.Command, args []string) error {
Use: "adopt USER-ID",
Short: "Adopt an existing identity as your own.",
Args: cobra.ExactArgs(1),
PreRunE: loadBackend(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runUserAdopt(env, args)
},
}),
}
return cmd

View File

@ -18,13 +18,12 @@ func newUserCreateCommand() *cobra.Command {
options := createUserOptions{}
cmd := &cobra.Command{
Use: "create",
Short: "Create a new identity.",
PreRunE: loadBackend(env),
PostRunE: closeBackend(env),
RunE: func(cmd *cobra.Command, args []string) error {
Use: "create",
Short: "Create a new identity.",
PreRunE: loadBackend(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runUserCreate(env, options)
},
}),
}
flags := cmd.Flags()

View File

@ -19,13 +19,12 @@ func newUserLsCommand() *cobra.Command {
options := userLsOptions{}
cmd := &cobra.Command{
Use: "ls",
Short: "List identities.",
PreRunE: loadBackend(env),
PostRunE: closeBackend(env),
RunE: func(cmd *cobra.Command, args []string) error {
Use: "ls",
Short: "List identities.",
PreRunE: loadBackend(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runUserLs(env, options)
},
}),
}
flags := cmd.Flags()