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

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -18,10 +18,9 @@ git bug status
git bug deselect git bug deselect
`, `,
PreRunE: loadBackend(env), PreRunE: loadBackend(env),
PostRunE: closeBackend(env), RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, args []string) error {
return runDeselect(env) return runDeselect(env)
}, }),
} }
return cmd 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. // 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 { return func(cmd *cobra.Command, args []string) error {
errRun := runE(cmd, args)
if env.backend == nil { if env.backend == nil {
return nil return nil
} }
err := env.backend.Close() err := env.backend.Close()
env.backend = nil env.backend = nil
// prioritize the RunE error
if errRun != nil {
return errRun
}
return err return err
} }
} }

View File

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

View File

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

View File

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

View File

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

View File

@ -14,10 +14,9 @@ func newLsLabelCommand() *cobra.Command {
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.`, 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), PreRunE: loadBackend(env),
PostRunE: closeBackend(env), RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, args []string) error {
return runLsLabel(env) return runLsLabel(env)
}, }),
} }
return cmd return cmd

View File

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

View File

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

View File

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

View File

@ -14,10 +14,9 @@ func newRmCommand() *cobra.Command {
Short: "Remove an existing bug.", 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.", 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), PreRunE: loadBackendEnsureUser(env),
PostRunE: closeBackend(env), RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, args []string) error {
return runRm(env, args) return runRm(env, args)
}, }),
} }
flags := cmd.Flags() flags := cmd.Flags()

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -12,10 +12,9 @@ func newUserAdoptCommand() *cobra.Command {
Short: "Adopt an existing identity as your own.", Short: "Adopt an existing identity as your own.",
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
PreRunE: loadBackend(env), PreRunE: loadBackend(env),
PostRunE: closeBackend(env), RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, args []string) error {
return runUserAdopt(env, args) return runUserAdopt(env, args)
}, }),
} }
return cmd return cmd

View File

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

View File

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