aboutsummaryrefslogtreecommitdiffstats
path: root/commands/add.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2020-06-28 19:09:32 +0200
committerMichael Muré <batolettre@gmail.com>2020-06-28 19:09:32 +0200
commit536c290dfbe6e0741c56f33659563c528c9f09b1 (patch)
tree79ea46f76ac6f69c2e6cbf3323be7c620c39a510 /commands/add.go
parent26bd1dd11010b4d86cebe2510ad7085a6b316334 (diff)
downloadgit-bug-536c290dfbe6e0741c56f33659563c528c9f09b1.tar.gz
commands: open and close the backend in a single place, simplify commands
Diffstat (limited to 'commands/add.go')
-rw-r--r--commands/add.go21
1 files changed, 7 insertions, 14 deletions
diff --git a/commands/add.go b/commands/add.go
index 8b5facaf..17fbbc93 100644
--- a/commands/add.go
+++ b/commands/add.go
@@ -3,9 +3,7 @@ package commands
import (
"github.com/spf13/cobra"
- "github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/input"
- "github.com/MichaelMure/git-bug/util/interrupt"
)
type addOptions struct {
@@ -19,9 +17,10 @@ func newAddCommand() *cobra.Command {
options := addOptions{}
cmd := &cobra.Command{
- Use: "add",
- Short: "Create a new bug.",
- PreRunE: loadRepoEnsureUser(env),
+ Use: "add",
+ Short: "Create a new bug.",
+ PreRunE: loadBackendEnsureUser(env),
+ PostRunE: closeBackend(env),
RunE: func(cmd *cobra.Command, args []string) error {
return runAdd(env, options)
},
@@ -41,13 +40,7 @@ func newAddCommand() *cobra.Command {
}
func runAdd(env *Env, opts addOptions) error {
- backend, err := cache.NewRepoCache(env.repo)
- if err != nil {
- return err
- }
- defer backend.Close()
- interrupt.RegisterCleaner(backend.Close)
-
+ var err error
if opts.messageFile != "" && opts.message == "" {
opts.title, opts.message, err = input.BugCreateFileInput(opts.messageFile)
if err != nil {
@@ -56,7 +49,7 @@ func runAdd(env *Env, opts addOptions) error {
}
if opts.messageFile == "" && (opts.message == "" || opts.title == "") {
- opts.title, opts.message, err = input.BugCreateEditorInput(backend, opts.title, opts.message)
+ opts.title, opts.message, err = input.BugCreateEditorInput(env.backend, opts.title, opts.message)
if err == input.ErrEmptyTitle {
env.out.Println("Empty title, aborting.")
@@ -67,7 +60,7 @@ func runAdd(env *Env, opts addOptions) error {
}
}
- b, _, err := backend.NewBug(opts.title, opts.message)
+ b, _, err := env.backend.NewBug(opts.title, opts.message)
if err != nil {
return err
}