diff options
Diffstat (limited to 'commands')
34 files changed, 304 insertions, 480 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 } diff --git a/commands/bridge.go b/commands/bridge.go index 8a2cf38b..46b3d9ec 100644 --- a/commands/bridge.go +++ b/commands/bridge.go @@ -4,17 +4,16 @@ import ( "github.com/spf13/cobra" "github.com/MichaelMure/git-bug/bridge" - "github.com/MichaelMure/git-bug/cache" - "github.com/MichaelMure/git-bug/util/interrupt" ) func newBridgeCommand() *cobra.Command { env := newEnv() cmd := &cobra.Command{ - Use: "bridge", - Short: "Configure and use bridges to other bug trackers.", - PreRunE: loadRepo(env), + 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 { return runBridge(env) }, @@ -31,14 +30,7 @@ func newBridgeCommand() *cobra.Command { } func runBridge(env *Env) error { - backend, err := cache.NewRepoCache(env.repo) - if err != nil { - return err - } - defer backend.Close() - interrupt.RegisterCleaner(backend.Close) - - configured, err := bridge.ConfiguredBridges(backend) + configured, err := bridge.ConfiguredBridges(env.backend) if err != nil { return err } diff --git a/commands/bridge_auth.go b/commands/bridge_auth.go index e51b9b9d..db4c5212 100644 --- a/commands/bridge_auth.go +++ b/commands/bridge_auth.go @@ -9,18 +9,17 @@ import ( text "github.com/MichaelMure/go-term-text" "github.com/MichaelMure/git-bug/bridge/core/auth" - "github.com/MichaelMure/git-bug/cache" "github.com/MichaelMure/git-bug/util/colors" - "github.com/MichaelMure/git-bug/util/interrupt" ) func newBridgeAuthCommand() *cobra.Command { env := newEnv() cmd := &cobra.Command{ - Use: "auth", - Short: "List all known bridge authentication credentials.", - PreRunE: loadRepo(env), + Use: "auth", + Short: "List all known bridge authentication credentials.", + PreRunE: loadBackend(env), + PostRunE: closeBackend(env), RunE: func(cmd *cobra.Command, args []string) error { return runBridgeAuth(env) }, @@ -35,14 +34,7 @@ func newBridgeAuthCommand() *cobra.Command { } func runBridgeAuth(env *Env) error { - backend, err := cache.NewRepoCache(env.repo) - if err != nil { - return err - } - defer backend.Close() - interrupt.RegisterCleaner(backend.Close) - - creds, err := auth.List(backend) + creds, err := auth.List(env.backend) if err != nil { return err } diff --git a/commands/bridge_auth_addtoken.go b/commands/bridge_auth_addtoken.go index dde7a6dd..55edd919 100644 --- a/commands/bridge_auth_addtoken.go +++ b/commands/bridge_auth_addtoken.go @@ -14,7 +14,6 @@ import ( "github.com/MichaelMure/git-bug/bridge/core" "github.com/MichaelMure/git-bug/bridge/core/auth" "github.com/MichaelMure/git-bug/cache" - "github.com/MichaelMure/git-bug/util/interrupt" ) type bridgeAuthAddTokenOptions struct { @@ -28,9 +27,10 @@ func newBridgeAuthAddTokenCommand() *cobra.Command { options := bridgeAuthAddTokenOptions{} cmd := &cobra.Command{ - Use: "add-token [<token>]", - Short: "Store a new token", - PreRunE: loadRepoEnsureUser(env), + Use: "add-token [<token>]", + Short: "Store a new token", + PreRunE: loadBackendEnsureUser(env), + PostRunE: closeBackend(env), RunE: func(cmd *cobra.Command, args []string) error { return runBridgeAuthAddToken(env, options, args) }, @@ -64,13 +64,6 @@ func runBridgeAuthAddToken(env *Env, opts bridgeAuthAddTokenOptions, args []stri return fmt.Errorf("flag --login is required") } - backend, err := cache.NewRepoCache(env.repo) - if err != nil { - return err - } - defer backend.Close() - interrupt.RegisterCleaner(backend.Close) - if !core.TargetExist(opts.target) { return fmt.Errorf("unknown target") } @@ -93,11 +86,12 @@ func runBridgeAuthAddToken(env *Env, opts bridgeAuthAddTokenOptions, args []stri } var user *cache.IdentityCache + var err error if opts.user == "" { - user, err = backend.GetUserIdentity() + user, err = env.backend.GetUserIdentity() } else { - user, err = backend.ResolveIdentityPrefix(opts.user) + user, err = env.backend.ResolveIdentityPrefix(opts.user) } if err != nil { return err diff --git a/commands/bridge_auth_show.go b/commands/bridge_auth_show.go index f8b15c9a..408a6cf1 100644 --- a/commands/bridge_auth_show.go +++ b/commands/bridge_auth_show.go @@ -9,17 +9,16 @@ import ( "github.com/spf13/cobra" "github.com/MichaelMure/git-bug/bridge/core/auth" - "github.com/MichaelMure/git-bug/cache" - "github.com/MichaelMure/git-bug/util/interrupt" ) func newBridgeAuthShow() *cobra.Command { env := newEnv() cmd := &cobra.Command{ - Use: "show", - Short: "Display an authentication credential.", - PreRunE: loadRepo(env), + Use: "show", + Short: "Display an authentication credential.", + PreRunE: loadBackend(env), + PostRunE: closeBackend(env), RunE: func(cmd *cobra.Command, args []string) error { return runBridgeAuthShow(env, args) }, @@ -30,13 +29,6 @@ func newBridgeAuthShow() *cobra.Command { } func runBridgeAuthShow(env *Env, args []string) error { - backend, err := cache.NewRepoCache(env.repo) - if err != nil { - return err - } - defer backend.Close() - interrupt.RegisterCleaner(backend.Close) - cred, err := auth.LoadWithPrefix(env.repo, args[0]) if err != nil { return err diff --git a/commands/bridge_configure.go b/commands/bridge_configure.go index 83555b0c..ecdb6502 100644 --- a/commands/bridge_configure.go +++ b/commands/bridge_configure.go @@ -12,9 +12,7 @@ import ( "github.com/MichaelMure/git-bug/bridge" "github.com/MichaelMure/git-bug/bridge/core" "github.com/MichaelMure/git-bug/bridge/core/auth" - "github.com/MichaelMure/git-bug/cache" "github.com/MichaelMure/git-bug/repository" - "github.com/MichaelMure/git-bug/util/interrupt" ) type bridgeConfigureOptions struct { @@ -86,7 +84,8 @@ git bug bridge configure \ --target=github \ --url=https://github.com/michaelmure/git-bug \ --token=$(TOKEN)`, - PreRunE: loadRepo(env), + PreRunE: loadBackend(env), + PostRunE: closeBackend(env), RunE: func(cmd *cobra.Command, args []string) error { return runBridgeConfigure(env, options) }, @@ -111,12 +110,7 @@ git bug bridge configure \ } func runBridgeConfigure(env *Env, opts bridgeConfigureOptions) error { - backend, err := cache.NewRepoCache(env.repo) - if err != nil { - return err - } - defer backend.Close() - interrupt.RegisterCleaner(backend.Close) + var err error if (opts.tokenStdin || opts.token != "" || opts.params.CredPrefix != "") && (opts.name == "" || opts.target == "") { @@ -156,7 +150,7 @@ func runBridgeConfigure(env *Env, opts bridgeConfigureOptions) error { } } - b, err := bridge.NewBridge(backend, opts.target, opts.name) + b, err := bridge.NewBridge(env.backend, opts.target, opts.name) if err != nil { return err } diff --git a/commands/bridge_pull.go b/commands/bridge_pull.go index dc8825fa..bb705582 100644 --- a/commands/bridge_pull.go +++ b/commands/bridge_pull.go @@ -13,7 +13,6 @@ import ( "github.com/MichaelMure/git-bug/bridge" "github.com/MichaelMure/git-bug/bridge/core" - "github.com/MichaelMure/git-bug/cache" "github.com/MichaelMure/git-bug/util/interrupt" ) @@ -27,9 +26,10 @@ func newBridgePullCommand() *cobra.Command { options := bridgePullOptions{} cmd := &cobra.Command{ - Use: "pull [<name>]", - Short: "Pull updates.", - PreRunE: loadRepo(env), + Use: "pull [<name>]", + Short: "Pull updates.", + PreRunE: loadBackend(env), + PostRunE: closeBackend(env), RunE: func(cmd *cobra.Command, args []string) error { return runBridgePull(env, options, args) }, @@ -50,19 +50,13 @@ func runBridgePull(env *Env, opts bridgePullOptions, args []string) error { return fmt.Errorf("only one of --no-resume and --since flags should be used") } - backend, err := cache.NewRepoCache(env.repo) - if err != nil { - return err - } - defer backend.Close() - interrupt.RegisterCleaner(backend.Close) - var b *core.Bridge + var err error if len(args) == 0 { - b, err = bridge.DefaultBridge(backend) + b, err = bridge.DefaultBridge(env.backend) } else { - b, err = bridge.LoadBridge(backend, args[0]) + b, err = bridge.LoadBridge(env.backend, args[0]) } if err != nil { diff --git a/commands/bridge_push.go b/commands/bridge_push.go index 73df87fb..7061a5ca 100644 --- a/commands/bridge_push.go +++ b/commands/bridge_push.go @@ -10,7 +10,6 @@ import ( "github.com/MichaelMure/git-bug/bridge" "github.com/MichaelMure/git-bug/bridge/core" - "github.com/MichaelMure/git-bug/cache" "github.com/MichaelMure/git-bug/util/interrupt" ) @@ -18,9 +17,10 @@ func newBridgePushCommand() *cobra.Command { env := newEnv() cmd := &cobra.Command{ - Use: "push [<name>]", - Short: "Push updates.", - PreRunE: loadRepoEnsureUser(env), + Use: "push [<name>]", + Short: "Push updates.", + PreRunE: loadBackendEnsureUser(env), + PostRunE: closeBackend(env), RunE: func(cmd *cobra.Command, args []string) error { return runBridgePush(env, args) }, @@ -31,19 +31,13 @@ func newBridgePushCommand() *cobra.Command { } func runBridgePush(env *Env, args []string) error { - backend, err := cache.NewRepoCache(env.repo) - if err != nil { - return err - } - defer backend.Close() - interrupt.RegisterCleaner(backend.Close) - var b *core.Bridge + var err error if len(args) == 0 { - b, err = bridge.DefaultBridge(backend) + b, err = bridge.DefaultBridge(env.backend) } else { - b, err = bridge.LoadBridge(backend, args[0]) + b, err = bridge.LoadBridge(env.backend, args[0]) } if err != nil { diff --git a/commands/bridge_rm.go b/commands/bridge_rm.go index 4ff963db..9f93c37a 100644 --- a/commands/bridge_rm.go +++ b/commands/bridge_rm.go @@ -4,17 +4,16 @@ import ( "github.com/spf13/cobra" "github.com/MichaelMure/git-bug/bridge" - "github.com/MichaelMure/git-bug/cache" - "github.com/MichaelMure/git-bug/util/interrupt" ) func newBridgeRm() *cobra.Command { env := newEnv() cmd := &cobra.Command{ - Use: "rm <name>", - Short: "Delete a configured bridge.", - PreRunE: loadRepo(env), + Use: "rm <name>", + Short: "Delete a configured bridge.", + PreRunE: loadBackend(env), + PostRunE: closeBackend(env), RunE: func(cmd *cobra.Command, args []string) error { return runBridgeRm(env, args) }, @@ -25,14 +24,7 @@ func newBridgeRm() *cobra.Command { } func runBridgeRm(env *Env, args []string) error { - backend, err := cache.NewRepoCache(env.repo) - if err != nil { - return err - } - defer backend.Close() - interrupt.RegisterCleaner(backend.Close) - - err = bridge.RemoveBridge(backend, args[0]) + err := bridge.RemoveBridge(env.backend, args[0]) if err != nil { return err } diff --git a/commands/comment.go b/commands/comment.go index 82e7d9f6..e81405a6 100644 --- a/commands/comment.go +++ b/commands/comment.go @@ -4,19 +4,18 @@ import ( text "github.com/MichaelMure/go-term-text" "github.com/spf13/cobra" - "github.com/MichaelMure/git-bug/cache" "github.com/MichaelMure/git-bug/commands/select" "github.com/MichaelMure/git-bug/util/colors" - "github.com/MichaelMure/git-bug/util/interrupt" ) func newCommentCommand() *cobra.Command { env := newEnv() cmd := &cobra.Command{ - Use: "comment [<id>]", - Short: "Display or add comments to a bug.", - PreRunE: loadRepo(env), + 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 { return runComment(env, args) }, @@ -28,14 +27,7 @@ func newCommentCommand() *cobra.Command { } func runComment(env *Env, args []string) error { - backend, err := cache.NewRepoCache(env.repo) - if err != nil { - return err - } - defer backend.Close() - interrupt.RegisterCleaner(backend.Close) - - b, args, err := _select.ResolveBug(backend, args) + b, args, err := _select.ResolveBug(env.backend, args) if err != nil { return err } diff --git a/commands/comment_add.go b/commands/comment_add.go index 1a560b5e..47366d00 100644 --- a/commands/comment_add.go +++ b/commands/comment_add.go @@ -3,10 +3,8 @@ package commands import ( "github.com/spf13/cobra" - "github.com/MichaelMure/git-bug/cache" "github.com/MichaelMure/git-bug/commands/select" "github.com/MichaelMure/git-bug/input" - "github.com/MichaelMure/git-bug/util/interrupt" ) type commentAddOptions struct { @@ -19,9 +17,10 @@ func newCommentAddCommand() *cobra.Command { options := commentAddOptions{} cmd := &cobra.Command{ - Use: "add [<id>]", - Short: "Add a new comment to a bug.", - PreRunE: loadRepoEnsureUser(env), + 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 { return runCommentAdd(env, options, args) }, @@ -40,14 +39,7 @@ func newCommentAddCommand() *cobra.Command { } func runCommentAdd(env *Env, opts commentAddOptions, args []string) error { - backend, err := cache.NewRepoCache(env.repo) - if err != nil { - return err - } - defer backend.Close() - interrupt.RegisterCleaner(backend.Close) - - b, args, err := _select.ResolveBug(backend, args) + b, args, err := _select.ResolveBug(env.backend, args) if err != nil { return err } @@ -60,7 +52,7 @@ func runCommentAdd(env *Env, opts commentAddOptions, args []string) error { } if opts.messageFile == "" && opts.message == "" { - opts.message, err = input.BugCommentEditorInput(backend, "") + opts.message, err = input.BugCommentEditorInput(env.backend, "") if err == input.ErrEmptyMessage { env.err.Println("Empty message, aborting.") return nil diff --git a/commands/deselect.go b/commands/deselect.go index 22a5f55d..23f77e2d 100644 --- a/commands/deselect.go +++ b/commands/deselect.go @@ -3,9 +3,7 @@ package commands import ( "github.com/spf13/cobra" - "github.com/MichaelMure/git-bug/cache" "github.com/MichaelMure/git-bug/commands/select" - "github.com/MichaelMure/git-bug/util/interrupt" ) func newDeselectCommand() *cobra.Command { @@ -19,7 +17,8 @@ git bug comment git bug status git bug deselect `, - PreRunE: loadRepo(env), + PreRunE: loadBackend(env), + PostRunE: closeBackend(env), RunE: func(cmd *cobra.Command, args []string) error { return runDeselect(env) }, @@ -29,14 +28,7 @@ git bug deselect } func runDeselect(env *Env) error { - backend, err := cache.NewRepoCache(env.repo) - if err != nil { - return err - } - defer backend.Close() - interrupt.RegisterCleaner(backend.Close) - - err = _select.Clear(backend) + err := _select.Clear(env.backend) if err != nil { return err } diff --git a/commands/env.go b/commands/env.go index daba8420..c3596c2d 100644 --- a/commands/env.go +++ b/commands/env.go @@ -5,14 +5,21 @@ import ( "io" "os" + "github.com/spf13/cobra" + + "github.com/MichaelMure/git-bug/bug" + "github.com/MichaelMure/git-bug/cache" + "github.com/MichaelMure/git-bug/identity" "github.com/MichaelMure/git-bug/repository" + "github.com/MichaelMure/git-bug/util/interrupt" ) // Env is the environment of a command type Env struct { - repo repository.ClockedRepo - out out - err out + repo repository.ClockedRepo + backend *cache.RepoCache + out out + err out } func newEnv() *Env { @@ -38,3 +45,106 @@ func (o out) Print(a ...interface{}) { func (o out) Println(a ...interface{}) { _, _ = fmt.Fprintln(o, a...) } + +// loadRepo is a pre-run function that load the repository for use in a command +func loadRepo(env *Env) func(*cobra.Command, []string) error { + return func(cmd *cobra.Command, args []string) error { + cwd, err := os.Getwd() + if err != nil { + return fmt.Errorf("unable to get the current working directory: %q", err) + } + + env.repo, err = repository.NewGitRepo(cwd, []repository.ClockLoader{bug.ClockLoader}) + if err == repository.ErrNotARepo { + return fmt.Errorf("%s must be run from within a git repo", rootCommandName) + } + + if err != nil { + return err + } + + return nil + } +} + +// loadRepoEnsureUser is the same as loadRepo, but also ensure that the user has configured +// an identity. Use this pre-run function when an error after using the configured user won't +// do. +func loadRepoEnsureUser(env *Env) func(*cobra.Command, []string) error { + return func(cmd *cobra.Command, args []string) error { + err := loadRepo(env)(cmd, args) + if err != nil { + return err + } + + _, err = identity.GetUserIdentity(env.repo) + if err != nil { + return err + } + + return nil + } +} + +// loadBackend is a pre-run function that load the repository and the backend for use in a command +// When using this function you also need to use closeBackend as a post-run +func loadBackend(env *Env) func(*cobra.Command, []string) error { + return func(cmd *cobra.Command, args []string) error { + err := loadRepo(env)(cmd, args) + if err != nil { + return err + } + + env.backend, err = cache.NewRepoCache(env.repo) + if err != nil { + return err + } + + cleaner := func(env *Env) interrupt.CleanerFunc { + return func() error { + if env.backend != nil { + err := env.backend.Close() + env.backend = nil + return err + } + return nil + } + } + + // Cleanup properly on interrupt + interrupt.RegisterCleaner(cleaner(env)) + return nil + } +} + +// loadBackendEnsureUser is the same as loadBackend, but also ensure that the user has configured +// an identity. Use this pre-run function when an error after using the configured user won't +// do. +func loadBackendEnsureUser(env *Env) func(*cobra.Command, []string) error { + return func(cmd *cobra.Command, args []string) error { + err := loadRepo(env)(cmd, args) + if err != nil { + return err + } + + _, err = identity.GetUserIdentity(env.repo) + if err != nil { + return err + } + + return nil + } +} + +// closeBackend is a post-run function that will close the backend properly +// if it has been opened. +func closeBackend(env *Env) func(*cobra.Command, []string) error { + return func(cmd *cobra.Command, args []string) error { + if env.backend == nil { + return nil + } + err := env.backend.Close() + env.backend = nil + return err + } +} diff --git a/commands/label.go b/commands/label.go index e48be18e..de7bdb3a 100644 --- a/commands/label.go +++ b/commands/label.go @@ -3,18 +3,17 @@ package commands import ( "github.com/spf13/cobra" - "github.com/MichaelMure/git-bug/cache" "github.com/MichaelMure/git-bug/commands/select" - "github.com/MichaelMure/git-bug/util/interrupt" ) func newLabelCommand() *cobra.Command { env := newEnv() cmd := &cobra.Command{ - Use: "label [<id>]", - Short: "Display, add or remove labels to/from a bug.", - PreRunE: loadRepo(env), + 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 { return runLabel(env, args) }, @@ -27,14 +26,7 @@ func newLabelCommand() *cobra.Command { } func runLabel(env *Env, args []string) error { - backend, err := cache.NewRepoCache(env.repo) - if err != nil { - return err - } - defer backend.Close() - interrupt.RegisterCleaner(backend.Close) - - b, args, err := _select.ResolveBug(backend, args) + b, args, err := _select.ResolveBug(env.backend, args) if err != nil { return err } diff --git a/commands/label_add.go b/commands/label_add.go index 83a9f064..05a27948 100644 --- a/commands/label_add.go +++ b/commands/label_add.go @@ -3,18 +3,17 @@ package commands import ( "github.com/spf13/cobra" - "github.com/MichaelMure/git-bug/cache" "github.com/MichaelMure/git-bug/commands/select" - "github.com/MichaelMure/git-bug/util/interrupt" ) func newLabelAddCommand() *cobra.Command { env := newEnv() cmd := &cobra.Command{ - Use: "add [<id>] <label>[...]", - Short: "Add a label to a bug.", - PreRunE: loadRepoEnsureUser(env), + 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 { return runLabelAdd(env, args) }, @@ -24,14 +23,7 @@ func newLabelAddCommand() *cobra.Command { } func runLabelAdd(env *Env, args []string) error { - backend, err := cache.NewRepoCache(env.repo) - if err != nil { - return err - } - defer backend.Close() - interrupt.RegisterCleaner(backend.Close) - - b, args, err := _select.ResolveBug(backend, args) + b, args, err := _select.ResolveBug(env.backend, args) if err != nil { return err } diff --git a/commands/label_rm.go b/commands/label_rm.go index 5fba4150..445a56a4 100644 --- a/commands/label_rm.go +++ b/commands/label_rm.go @@ -3,18 +3,17 @@ package commands import ( "github.com/spf13/cobra" - "github.com/MichaelMure/git-bug/cache" "github.com/MichaelMure/git-bug/commands/select" - "github.com/MichaelMure/git-bug/util/interrupt" ) func newLabelRmCommand() *cobra.Command { env := newEnv() cmd := &cobra.Command{ - Use: "rm [<id>] <label>[...]", - Short: "Remove a label from a bug.", - PreRunE: loadRepo(env), + 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 { return runLabelRm(env, args) }, @@ -24,14 +23,7 @@ func newLabelRmCommand() *cobra.Command { } func runLabelRm(env *Env, args []string) error { - backend, err := cache.NewRepoCache(env.repo) - if err != nil { - return err - } - defer backend.Close() - interrupt.RegisterCleaner(backend.Close) - - b, args, err := _select.ResolveBug(backend, args) + b, args, err := _select.ResolveBug(env.backend, args) if err != nil { return err } diff --git a/commands/ls-id.go b/commands/ls-id.go index 6624793d..bed6c057 100644 --- a/commands/ls-id.go +++ b/commands/ls-id.go @@ -2,18 +2,16 @@ package commands import ( "github.com/spf13/cobra" - - "github.com/MichaelMure/git-bug/cache" - "github.com/MichaelMure/git-bug/util/interrupt" ) func newLsIdCommand() *cobra.Command { env := newEnv() cmd := &cobra.Command{ - Use: "ls-id [<prefix>]", - Short: "List bug identifiers.", - PreRunE: loadRepo(env), + Use: "ls-id [<prefix>]", + Short: "List bug identifiers.", + PreRunE: loadBackend(env), + PostRunE: closeBackend(env), RunE: func(cmd *cobra.Command, args []string) error { return runLsId(env, args) }, @@ -23,19 +21,12 @@ func newLsIdCommand() *cobra.Command { } func runLsId(env *Env, args []string) error { - backend, err := cache.NewRepoCache(env.repo) - if err != nil { - return err - } - defer backend.Close() - interrupt.RegisterCleaner(backend.Close) - var prefix = "" if len(args) != 0 { prefix = args[0] } - for _, id := range backend.AllBugsIds() { + for _, id := range env.backend.AllBugsIds() { if prefix == "" || id.HasPrefix(prefix) { env.out.Println(id) } diff --git a/commands/ls-labels.go b/commands/ls-labels.go index 3473aadd..a7c2fb6f 100644 --- a/commands/ls-labels.go +++ b/commands/ls-labels.go @@ -2,9 +2,6 @@ package commands import ( "github.com/spf13/cobra" - - "github.com/MichaelMure/git-bug/cache" - "github.com/MichaelMure/git-bug/util/interrupt" ) func newLsLabelCommand() *cobra.Command { @@ -16,7 +13,8 @@ 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: loadRepo(env), + PreRunE: loadBackend(env), + PostRunE: closeBackend(env), RunE: func(cmd *cobra.Command, args []string) error { return runLsLabel(env) }, @@ -26,14 +24,7 @@ Note: in the future, a proper label policy could be implemented where valid labe } func runLsLabel(env *Env) error { - backend, err := cache.NewRepoCache(env.repo) - if err != nil { - return err - } - defer backend.Close() - interrupt.RegisterCleaner(backend.Close) - - labels := backend.ValidLabels() + labels := env.backend.ValidLabels() for _, l := range labels { env.out.Println(l) diff --git a/commands/ls.go b/commands/ls.go index f48a5796..ad61a852 100644 --- a/commands/ls.go +++ b/commands/ls.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "strings" + "time" text "github.com/MichaelMure/go-term-text" "github.com/spf13/cobra" @@ -12,7 +13,6 @@ import ( "github.com/MichaelMure/git-bug/cache" "github.com/MichaelMure/git-bug/query" "github.com/MichaelMure/git-bug/util/colors" - "github.com/MichaelMure/git-bug/util/interrupt" ) type lsOptions struct { @@ -41,7 +41,8 @@ git bug ls status:open sort:edit-desc List closed bugs sorted by creation with flags: git bug ls --status closed --by creation `, - PreRunE: loadRepo(env), + PreRunE: loadBackend(env), + PostRunE: closeBackend(env), RunE: func(cmd *cobra.Command, args []string) error { return runLs(env, options, args) }, @@ -75,14 +76,11 @@ git bug ls --status closed --by creation } func runLs(env *Env, opts lsOptions, args []string) error { - backend, err := cache.NewRepoCache(env.repo) - if err != nil { - return err - } - defer backend.Close() - interrupt.RegisterCleaner(backend.Close) + time.Sleep(5 * time.Second) var q *query.Query + var err error + if len(args) >= 1 { q, err = query.Parse(strings.Join(args, " ")) @@ -97,11 +95,11 @@ func runLs(env *Env, opts lsOptions, args []string) error { q = &opts.query } - allIds := backend.QueryBugs(q) + allIds := env.backend.QueryBugs(q) bugExcerpt := make([]*cache.BugExcerpt, len(allIds)) for i, id := range allIds { - b, err := backend.ResolveBugExcerpt(id) + b, err := env.backend.ResolveBugExcerpt(id) if err != nil { return err } @@ -110,13 +108,13 @@ func runLs(env *Env, opts lsOptions, args []string) error { switch opts.outputFormat { case "org-mode": - return lsOrgmodeFormatter(env, backend, bugExcerpt) + return lsOrgmodeFormatter(env, bugExcerpt) case "plain": - return lsPlainFormatter(env, backend, bugExcerpt) + return lsPlainFormatter(env, bugExcerpt) case "json": - return lsJsonFormatter(env, backend, bugExcerpt) + return lsJsonFormatter(env, bugExcerpt) case "default": - return lsDefaultFormatter(env, backend, bugExcerpt) + return lsDefaultFormatter(env, bugExcerpt) default: return fmt.Errorf("unknown format %s", opts.outputFormat) } @@ -139,7 +137,7 @@ type JSONBugExcerpt struct { Metadata map[string]string `json:"metadata"` } -func lsJsonFormatter(env *Env, backend *cache.RepoCache, bugExcerpts []*cache.BugExcerpt) error { +func lsJsonFormatter(env *Env, bugExcerpts []*cache.BugExcerpt) error { jsonBugs := make([]JSONBugExcerpt, len(bugExcerpts)) for i, b := range bugExcerpts { jsonBug := JSONBugExcerpt{ @@ -155,7 +153,7 @@ func lsJsonFormatter(env *Env, backend *cache.RepoCache, bugExcerpts []*cache.Bu } if b.AuthorId != "" { - author, err := backend.ResolveIdentityExcerpt(b.AuthorId) + author, err := env.backend.ResolveIdentityExcerpt(b.AuthorId) if err != nil { return err } @@ -166,7 +164,7 @@ func lsJsonFormatter(env *Env, backend *cache.RepoCache, bugExcerpts []*cache.Bu jsonBug.Actors = make([]JSONIdentity, len(b.Actors)) for i, element := range b.Actors { - actor, err := backend.ResolveIdentityExcerpt(element) + actor, err := env.backend.ResolveIdentityExcerpt(element) if err != nil { return err } @@ -175,7 +173,7 @@ func lsJsonFormatter(env *Env, backend *cache.RepoCache, bugExcerpts []*cache.Bu jsonBug.Participants = make([]JSONIdentity, len(b.Participants)) for i, element := range b.Participants { - participant, err := backend.ResolveIdentityExcerpt(element) + participant, err := env.backend.ResolveIdentityExcerpt(element) if err != nil { return err } @@ -189,11 +187,11 @@ func lsJsonFormatter(env *Env, backend *cache.RepoCache, bugExcerpts []*cache.Bu return nil } -func lsDefaultFormatter(env *Env, backend *cache.RepoCache, bugExcerpts []*cache.BugExcerpt) error { +func lsDefaultFormatter(env *Env, bugExcerpts []*cache.BugExcerpt) error { for _, b := range bugExcerpts { var name string if b.AuthorId != "" { - author, err := backend.ResolveIdentityExcerpt(b.AuthorId) + author, err := env.backend.ResolveIdentityExcerpt(b.AuthorId) if err != nil { return err } @@ -231,14 +229,14 @@ func lsDefaultFormatter(env *Env, backend *cache.RepoCache, bugExcerpts []*cache return nil } -func lsPlainFormatter(env *Env, _ *cache.RepoCache, bugExcerpts []*cache.BugExcerpt) error { +func lsPlainFormatter(env *Env, bugExcerpts []*cache.BugExcerpt) error { for _, b := range bugExcerpts { env.out.Printf("%s [%s] %s\n", b.Id.Human(), b.Status, b.Title) } return nil } -func lsOrgmodeFormatter(env *Env, backend *cache.RepoCache, bugExcerpts []*cache.BugExcerpt) error { +func lsOrgmodeFormatter(env *Env, bugExcerpts []*cache.BugExcerpt) error { env.out.Println("+TODO: OPEN | CLOSED") for _, b := range bugExcerpts { @@ -253,7 +251,7 @@ func lsOrgmodeFormatter(env *Env, backend *cache.RepoCache, bugExcerpts []*cache var name string if b.AuthorId != "" { - author, err := backend.ResolveIdentityExcerpt(b.AuthorId) + author, err := env.backend.ResolveIdentityExcerpt(b.AuthorId) if err != nil { return err } @@ -283,7 +281,7 @@ func lsOrgmodeFormatter(env *Env, backend *cache.RepoCache, bugExcerpts []*cache env.out.Printf("** Actors:\n") for _, element := range b.Actors { - actor, err := backend.ResolveIdentityExcerpt(element) + actor, err := env.backend.ResolveIdentityExcerpt(element) if err != nil { return err } @@ -296,7 +294,7 @@ func lsOrgmodeFormatter(env *Env, backend *cache.RepoCache, bugExcerpts []*cache env.out.Printf("** Participants:\n") for _, element := range b.Participants { - participant, err := backend.ResolveIdentityExcerpt(element) + participant, err := env.backend.ResolveIdentityExcerpt(element) if err != nil { return err } diff --git a/commands/pull.go b/commands/pull.go index fb50a03b..3f032593 100644 --- a/commands/pull.go +++ b/commands/pull.go @@ -5,18 +5,17 @@ import ( "github.com/spf13/cobra" - "github.com/MichaelMure/git-bug/cache" "github.com/MichaelMure/git-bug/entity" - "github.com/MichaelMure/git-bug/util/interrupt" ) func newPullCommand() *cobra.Command { env := newEnv() cmd := &cobra.Command{ - Use: "pull [<remote>]", - Short: "Pull bugs update from a git remote.", - PreRunE: loadRepo(env), + 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 { return runPull(env, args) }, @@ -35,16 +34,9 @@ func runPull(env *Env, args []string) error { remote = args[0] } - backend, err := cache.NewRepoCache(env.repo) - if err != nil { - return err - } - defer backend.Close() - interrupt.RegisterCleaner(backend.Close) - env.out.Println("Fetching remote ...") - stdout, err := backend.Fetch(remote) + stdout, err := env.backend.Fetch(remote) if err != nil { return err } @@ -53,7 +45,7 @@ func runPull(env *Env, args []string) error { env.out.Println("Merging data ...") - for result := range backend.MergeAll(remote) { + for result := range env.backend.MergeAll(remote) { if result.Err != nil { env.err.Println(result.Err) } diff --git a/commands/push.go b/commands/push.go index fee8a50d..f4b83fab 100644 --- a/commands/push.go +++ b/commands/push.go @@ -4,18 +4,16 @@ import ( "errors" "github.com/spf13/cobra" - - "github.com/MichaelMure/git-bug/cache" - "github.com/MichaelMure/git-bug/util/interrupt" ) func newPushCommand() *cobra.Command { env := newEnv() cmd := &cobra.Command{ - Use: "push [<remote>]", - Short: "Push bugs update to a git remote.", - PreRunE: loadRepo(env), + 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 { return runPush(env, args) }, @@ -34,14 +32,7 @@ func runPush(env *Env, args []string) error { remote = args[0] } - backend, err := cache.NewRepoCache(env.repo) - if err != nil { - return err - } - defer backend.Close() - interrupt.RegisterCleaner(backend.Close) - - stdout, err := backend.Push(remote) + stdout, err := env.backend.Push(remote) if err != nil { return err } diff --git a/commands/root.go b/commands/root.go index 42859d19..a67fec1a 100644 --- a/commands/root.go +++ b/commands/root.go @@ -6,10 +6,6 @@ import ( "os" "github.com/spf13/cobra" - - "github.com/MichaelMure/git-bug/bug" - "github.com/MichaelMure/git-bug/identity" - "github.com/MichaelMure/git-bug/repository" ) const rootCommandName = "git-bug" @@ -92,43 +88,3 @@ func Execute() { os.Exit(1) } } - -// loadRepo is a pre-run function that load the repository for use in a command -func loadRepo(env *Env) func(*cobra.Command, []string) error { - return func(cmd *cobra.Command, args []string) error { - cwd, err := os.Getwd() - if err != nil { - return fmt.Errorf("unable to get the current working directory: %q", err) - } - - env.repo, err = repository.NewGitRepo(cwd, []repository.ClockLoader{bug.ClockLoader}) - if err == repository.ErrNotARepo { - return fmt.Errorf("%s must be run from within a git repo", rootCommandName) - } - - if err != nil { - return err - } - - return nil - } -} - -// loadRepoEnsureUser is the same as loadRepo, but also ensure that the user has configured -// an identity. Use this pre-run function when an error after using the configured user won't -// do. -func loadRepoEnsureUser(env *Env) func(*cobra.Command, []string) error { - return func(cmd *cobra.Command, args []string) error { - err := loadRepo(env)(cmd, args) - if err != nil { - return err - } - - _, err = identity.GetUserIdentity(env.repo) - if err != nil { - return err - } - - return nil - } -} diff --git a/commands/select.go b/commands/select.go index e4916650..f2cf2d47 100644 --- a/commands/select.go +++ b/commands/select.go @@ -5,9 +5,7 @@ import ( "github.com/spf13/cobra" - "github.com/MichaelMure/git-bug/cache" "github.com/MichaelMure/git-bug/commands/select" - "github.com/MichaelMure/git-bug/util/interrupt" ) func newSelectCommand() *cobra.Command { @@ -29,7 +27,8 @@ instead of The complementary command is "git bug deselect" performing the opposite operation. `, - PreRunE: loadRepo(env), + PreRunE: loadBackend(env), + PostRunE: closeBackend(env), RunE: func(cmd *cobra.Command, args []string) error { return runSelect(env, args) }, @@ -43,21 +42,14 @@ func runSelect(env *Env, args []string) error { return errors.New("You must provide a bug id") } - backend, err := cache.NewRepoCache(env.repo) - if err != nil { - return err - } - defer backend.Close() - interrupt.RegisterCleaner(backend.Close) - prefix := args[0] - b, err := backend.ResolveBugPrefix(prefix) + b, err := env.backend.ResolveBugPrefix(prefix) if err != nil { return err } - err = _select.Select(backend, b.Id()) + err = _select.Select(env.backend, b.Id()) if err != nil { return err } diff --git a/commands/show.go b/commands/show.go index 9e6a8e8c..f3995205 100644 --- a/commands/show.go +++ b/commands/show.go @@ -9,10 +9,8 @@ import ( "github.com/spf13/cobra" "github.com/MichaelMure/git-bug/bug" - "github.com/MichaelMure/git-bug/cache" _select "github.com/MichaelMure/git-bug/commands/select" "github.com/MichaelMure/git-bug/util/colors" - "github.com/MichaelMure/git-bug/util/interrupt" ) type showOptions struct { @@ -25,9 +23,10 @@ func newShowCommand() *cobra.Command { options := showOptions{} cmd := &cobra.Command{ - Use: "show [<id>]", - Short: "Display the details of a bug.", - PreRunE: loadRepo(env), + Use: "show [<id>]", + Short: "Display the details of a bug.", + PreRunE: loadBackend(env), + PostRunE: closeBackend(env), RunE: func(cmd *cobra.Command, args []string) error { return runShow(env, options, args) }, @@ -45,14 +44,7 @@ func newShowCommand() *cobra.Command { } func runShow(env *Env, opts showOptions, args []string) error { - backend, err := cache.NewRepoCache(env.repo) - if err != nil { - return err - } - defer backend.Close() - interrupt.RegisterCleaner(backend.Close) - - b, args, err := _select.ResolveBug(backend, args) + b, args, err := _select.ResolveBug(env.backend, args) if err != nil { return err } diff --git a/commands/status.go b/commands/status.go index aaf9e636..57771bca 100644 --- a/commands/status.go +++ b/commands/status.go @@ -3,18 +3,17 @@ package commands import ( "github.com/spf13/cobra" - "github.com/MichaelMure/git-bug/cache" "github.com/MichaelMure/git-bug/commands/select" - "github.com/MichaelMure/git-bug/util/interrupt" ) func newStatusCommand() *cobra.Command { env := newEnv() cmd := &cobra.Command{ - Use: "status [<id>]", - Short: "Display or change a bug status.", - PreRunE: loadRepo(env), + Use: "status [<id>]", + Short: "Display or change a bug status.", + PreRunE: loadBackend(env), + PostRunE: closeBackend(env), RunE: func(cmd *cobra.Command, args []string) error { return runStatus(env, args) }, @@ -27,14 +26,7 @@ func newStatusCommand() *cobra.Command { } func runStatus(env *Env, args []string) error { - backend, err := cache.NewRepoCache(env.repo) - if err != nil { - return err - } - defer backend.Close() - interrupt.RegisterCleaner(backend.Close) - - b, args, err := _select.ResolveBug(backend, args) + b, args, err := _select.ResolveBug(env.backend, args) if err != nil { return err } diff --git a/commands/status_close.go b/commands/status_close.go index 2c7079e6..29092a7b 100644 --- a/commands/status_close.go +++ b/commands/status_close.go @@ -3,18 +3,17 @@ package commands import ( "github.com/spf13/cobra" - "github.com/MichaelMure/git-bug/cache" "github.com/MichaelMure/git-bug/commands/select" - "github.com/MichaelMure/git-bug/util/interrupt" ) func newStatusCloseCommand() *cobra.Command { env := newEnv() cmd := &cobra.Command{ - Use: "close [<id>]", - Short: "Mark a bug as closed.", - PreRunE: loadRepoEnsureUser(env), + Use: "close [<id>]", + Short: "Mark a bug as closed.", + PreRunE: loadBackendEnsureUser(env), + PostRunE: closeBackend(env), RunE: func(cmd *cobra.Command, args []string) error { return runStatusClose(env, args) }, @@ -24,14 +23,7 @@ func newStatusCloseCommand() *cobra.Command { } func runStatusClose(env *Env, args []string) error { - backend, err := cache.NewRepoCache(env.repo) - if err != nil { - return err - } - defer backend.Close() - interrupt.RegisterCleaner(backend.Close) - - b, args, err := _select.ResolveBug(backend, args) + b, args, err := _select.ResolveBug(env.backend, args) if err != nil { return err } diff --git a/commands/status_open.go b/commands/status_open.go index ff98fa33..9dd9082c 100644 --- a/commands/status_open.go +++ b/commands/status_open.go @@ -3,18 +3,17 @@ package commands import ( "github.com/spf13/cobra" - "github.com/MichaelMure/git-bug/cache" "github.com/MichaelMure/git-bug/commands/select" - "github.com/MichaelMure/git-bug/util/interrupt" ) func newStatusOpenCommand() *cobra.Command { env := newEnv() cmd := &cobra.Command{ - Use: "open [<id>]", - Short: "Mark a bug as open.", - PreRunE: loadRepoEnsureUser(env), + Use: "open [<id>]", + Short: "Mark a bug as open.", + PreRunE: loadBackendEnsureUser(env), + PostRunE: closeBackend(env), RunE: func(cmd *cobra.Command, args []string) error { return runStatusOpen(env, args) }, @@ -24,14 +23,7 @@ func newStatusOpenCommand() *cobra.Command { } func runStatusOpen(env *Env, args []string) error { - backend, err := cache.NewRepoCache(env.repo) - if err != nil { - return err - } - defer backend.Close() - interrupt.RegisterCleaner(backend.Close) - - b, args, err := _select.ResolveBug(backend, args) + b, args, err := _select.ResolveBug(env.backend, args) if err != nil { return err } diff --git a/commands/termui.go b/commands/termui.go index 174d60ec..1470790f 100644 --- a/commands/termui.go +++ b/commands/termui.go @@ -3,19 +3,18 @@ package commands import ( "github.com/spf13/cobra" - "github.com/MichaelMure/git-bug/cache" "github.com/MichaelMure/git-bug/termui" - "github.com/MichaelMure/git-bug/util/interrupt" ) func newTermUICommand() *cobra.Command { env := newEnv() cmd := &cobra.Command{ - Use: "termui", - Aliases: []string{"tui"}, - Short: "Launch the terminal UI.", - PreRunE: loadRepoEnsureUser(env), + Use: "termui", + Aliases: []string{"tui"}, + Short: "Launch the terminal UI.", + PreRunE: loadBackendEnsureUser(env), + PostRunE: closeBackend(env), RunE: func(cmd *cobra.Command, args []string) error { return runTermUI(env) }, @@ -25,12 +24,5 @@ func newTermUICommand() *cobra.Command { } func runTermUI(env *Env) error { - backend, err := cache.NewRepoCache(env.repo) - if err != nil { - return err - } - defer backend.Close() - interrupt.RegisterCleaner(backend.Close) - - return termui.Run(backend) + return termui.Run(env.backend) } diff --git a/commands/title.go b/commands/title.go index c1a0e7fb..d11fb40e 100644 --- a/commands/title.go +++ b/commands/title.go @@ -3,18 +3,17 @@ package commands import ( "github.com/spf13/cobra" - "github.com/MichaelMure/git-bug/cache" "github.com/MichaelMure/git-bug/commands/select" - "github.com/MichaelMure/git-bug/util/interrupt" ) func newTitleCommand() *cobra.Command { env := newEnv() cmd := &cobra.Command{ - Use: "title [<id>]", - Short: "Display or change a title of a bug.", - PreRunE: loadRepo(env), + 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 { return runTitle(env, args) }, @@ -26,14 +25,7 @@ func newTitleCommand() *cobra.Command { } func runTitle(env *Env, args []string) error { - backend, err := cache.NewRepoCache(env.repo) - if err != nil { - return err - } - defer backend.Close() - interrupt.RegisterCleaner(backend.Close) - - b, args, err := _select.ResolveBug(backend, args) + b, args, err := _select.ResolveBug(env.backend, args) if err != nil { return err } diff --git a/commands/title_edit.go b/commands/title_edit.go index 853622cd..e2dbc245 100644 --- a/commands/title_edit.go +++ b/commands/title_edit.go @@ -3,10 +3,8 @@ package commands import ( "github.com/spf13/cobra" - "github.com/MichaelMure/git-bug/cache" "github.com/MichaelMure/git-bug/commands/select" "github.com/MichaelMure/git-bug/input" - "github.com/MichaelMure/git-bug/util/interrupt" ) type titleEditOptions struct { @@ -18,9 +16,10 @@ func newTitleEditCommand() *cobra.Command { options := titleEditOptions{} cmd := &cobra.Command{ - Use: "edit [<id>]", - Short: "Edit a title of a bug.", - PreRunE: loadRepoEnsureUser(env), + Use: "edit [<id>]", + Short: "Edit a title of a bug.", + PreRunE: loadBackendEnsureUser(env), + PostRunE: closeBackend(env), RunE: func(cmd *cobra.Command, args []string) error { return runTitleEdit(env, options, args) }, @@ -37,14 +36,7 @@ func newTitleEditCommand() *cobra.Command { } func runTitleEdit(env *Env, opts titleEditOptions, args []string) error { - backend, err := cache.NewRepoCache(env.repo) - if err != nil { - return err - } - defer backend.Close() - interrupt.RegisterCleaner(backend.Close) - - b, args, err := _select.ResolveBug(backend, args) + b, args, err := _select.ResolveBug(env.backend, args) if err != nil { return err } diff --git a/commands/user.go b/commands/user.go index 35e00a7c..8a6aef53 100644 --- a/commands/user.go +++ b/commands/user.go @@ -7,7 +7,6 @@ import ( "github.com/spf13/cobra" "github.com/MichaelMure/git-bug/cache" - "github.com/MichaelMure/git-bug/util/interrupt" ) type userOptions struct { @@ -19,9 +18,10 @@ func newUserCommand() *cobra.Command { options := userOptions{} cmd := &cobra.Command{ - Use: "user [<user-id>]", - Short: "Display or change the user identity.", - PreRunE: loadRepoEnsureUser(env), + 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 { return runUser(env, options, args) }, @@ -41,22 +41,16 @@ func newUserCommand() *cobra.Command { } func runUser(env *Env, opts userOptions, args []string) error { - backend, err := cache.NewRepoCache(env.repo) - if err != nil { - return err - } - defer backend.Close() - interrupt.RegisterCleaner(backend.Close) - if len(args) > 1 { return errors.New("only one identity can be displayed at a time") } var id *cache.IdentityCache + var err error if len(args) == 1 { - id, err = backend.ResolveIdentityPrefix(args[0]) + id, err = env.backend.ResolveIdentityPrefix(args[0]) } else { - id, err = backend.GetUserIdentity() + id, err = env.backend.GetUserIdentity() } if err != nil { diff --git a/commands/user_adopt.go b/commands/user_adopt.go index 0eb8b467..521f032f 100644 --- a/commands/user_adopt.go +++ b/commands/user_adopt.go @@ -2,19 +2,17 @@ package commands import ( "github.com/spf13/cobra" - - "github.com/MichaelMure/git-bug/cache" - "github.com/MichaelMure/git-bug/util/interrupt" ) 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: loadRepo(env), + 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 { return runUserAdopt(env, args) }, @@ -24,21 +22,14 @@ func newUserAdoptCommand() *cobra.Command { } func runUserAdopt(env *Env, args []string) error { - backend, err := cache.NewRepoCache(env.repo) - if err != nil { - return err - } - defer backend.Close() - interrupt.RegisterCleaner(backend.Close) - prefix := args[0] - i, err := backend.ResolveIdentityPrefix(prefix) + i, err := env.backend.ResolveIdentityPrefix(prefix) if err != nil { return err } - err = backend.SetUserIdentity(i) + err = env.backend.SetUserIdentity(i) if err != nil { return err } diff --git a/commands/user_create.go b/commands/user_create.go index 6433d38e..3da712f3 100644 --- a/commands/user_create.go +++ b/commands/user_create.go @@ -3,18 +3,17 @@ 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" ) func newUserCreateCommand() *cobra.Command { env := newEnv() cmd := &cobra.Command{ - Use: "create", - Short: "Create a new identity.", - PreRunE: loadRepo(env), + Use: "create", + Short: "Create a new identity.", + PreRunE: loadBackend(env), + PostRunE: closeBackend(env), RunE: func(cmd *cobra.Command, args []string) error { return runUserCreate(env) }, @@ -24,14 +23,7 @@ func newUserCreateCommand() *cobra.Command { } func runUserCreate(env *Env) error { - backend, err := cache.NewRepoCache(env.repo) - if err != nil { - return err - } - defer backend.Close() - interrupt.RegisterCleaner(backend.Close) - - preName, err := backend.GetUserName() + preName, err := env.backend.GetUserName() if err != nil { return err } @@ -41,7 +33,7 @@ func runUserCreate(env *Env) error { return err } - preEmail, err := backend.GetUserEmail() + preEmail, err := env.backend.GetUserEmail() if err != nil { return err } @@ -56,7 +48,7 @@ func runUserCreate(env *Env) error { return err } - id, err := backend.NewIdentityRaw(name, email, "", avatarURL, nil) + id, err := env.backend.NewIdentityRaw(name, email, "", avatarURL, nil) if err != nil { return err } @@ -66,13 +58,13 @@ func runUserCreate(env *Env) error { return err } - set, err := backend.IsUserIdentitySet() + set, err := env.backend.IsUserIdentitySet() if err != nil { return err } if !set { - err = backend.SetUserIdentity(id) + err = env.backend.SetUserIdentity(id) if err != nil { return err } diff --git a/commands/user_ls.go b/commands/user_ls.go index 5087ebd4..05d343d6 100644 --- a/commands/user_ls.go +++ b/commands/user_ls.go @@ -8,7 +8,6 @@ import ( "github.com/MichaelMure/git-bug/cache" "github.com/MichaelMure/git-bug/util/colors" - "github.com/MichaelMure/git-bug/util/interrupt" ) type userLsOptions struct { @@ -20,9 +19,10 @@ func newUserLsCommand() *cobra.Command { options := userLsOptions{} cmd := &cobra.Command{ - Use: "ls", - Short: "List identities.", - PreRunE: loadRepo(env), + Use: "ls", + Short: "List identities.", + PreRunE: loadBackend(env), + PostRunE: closeBackend(env), RunE: func(cmd *cobra.Command, args []string) error { return runUserLs(env, options) }, @@ -38,17 +38,10 @@ func newUserLsCommand() *cobra.Command { } func runUserLs(env *Env, opts userLsOptions) error { - backend, err := cache.NewRepoCache(env.repo) - if err != nil { - return err - } - defer backend.Close() - interrupt.RegisterCleaner(backend.Close) - - ids := backend.AllIdentityIds() + ids := env.backend.AllIdentityIds() var users []*cache.IdentityExcerpt for _, id := range ids { - user, err := backend.ResolveIdentityExcerpt(id) + user, err := env.backend.ResolveIdentityExcerpt(id) if err != nil { return err } |