aboutsummaryrefslogtreecommitdiffstats
path: root/commands/title_edit.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2020-06-28 18:26:29 +0200
committerMichael Muré <batolettre@gmail.com>2020-06-28 18:26:29 +0200
commit26bd1dd11010b4d86cebe2510ad7085a6b316334 (patch)
treef1fe939311c75bd615071e96f3d37822cccd77a7 /commands/title_edit.go
parentc0dbc149d5c0c3610476ba14a800c9ba803a2c2c (diff)
downloadgit-bug-26bd1dd11010b4d86cebe2510ad7085a6b316334.tar.gz
commands: refactor to avoid globals
Diffstat (limited to 'commands/title_edit.go')
-rw-r--r--commands/title_edit.go65
1 files changed, 35 insertions, 30 deletions
diff --git a/commands/title_edit.go b/commands/title_edit.go
index 3e40bd9e..853622cd 100644
--- a/commands/title_edit.go
+++ b/commands/title_edit.go
@@ -1,21 +1,43 @@
package commands
import (
- "fmt"
+ "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"
- "github.com/spf13/cobra"
)
-var (
- titleEditTitle string
-)
+type titleEditOptions struct {
+ title string
+}
+
+func newTitleEditCommand() *cobra.Command {
+ env := newEnv()
+ options := titleEditOptions{}
+
+ cmd := &cobra.Command{
+ Use: "edit [<id>]",
+ Short: "Edit a title of a bug.",
+ PreRunE: loadRepoEnsureUser(env),
+ RunE: func(cmd *cobra.Command, args []string) error {
+ return runTitleEdit(env, options, args)
+ },
+ }
+
+ flags := cmd.Flags()
+ flags.SortFlags = false
+
+ flags.StringVarP(&options.title, "title", "t", "",
+ "Provide a title to describe the issue",
+ )
-func runTitleEdit(cmd *cobra.Command, args []string) error {
- backend, err := cache.NewRepoCache(repo)
+ return cmd
+}
+
+func runTitleEdit(env *Env, opts titleEditOptions, args []string) error {
+ backend, err := cache.NewRepoCache(env.repo)
if err != nil {
return err
}
@@ -29,10 +51,10 @@ func runTitleEdit(cmd *cobra.Command, args []string) error {
snap := b.Snapshot()
- if titleEditTitle == "" {
- titleEditTitle, err = input.BugTitleEditorInput(repo, snap.Title)
+ if opts.title == "" {
+ opts.title, err = input.BugTitleEditorInput(env.repo, snap.Title)
if err == input.ErrEmptyTitle {
- fmt.Println("Empty title, aborting.")
+ env.out.Println("Empty title, aborting.")
return nil
}
if err != nil {
@@ -40,31 +62,14 @@ func runTitleEdit(cmd *cobra.Command, args []string) error {
}
}
- if titleEditTitle == snap.Title {
- fmt.Println("No change, aborting.")
+ if opts.title == snap.Title {
+ env.err.Println("No change, aborting.")
}
- _, err = b.SetTitle(titleEditTitle)
+ _, err = b.SetTitle(opts.title)
if err != nil {
return err
}
return b.Commit()
}
-
-var titleEditCmd = &cobra.Command{
- Use: "edit [<id>]",
- Short: "Edit a title of a bug.",
- PreRunE: loadRepoEnsureUser,
- RunE: runTitleEdit,
-}
-
-func init() {
- titleCmd.AddCommand(titleEditCmd)
-
- titleEditCmd.Flags().SortFlags = false
-
- titleEditCmd.Flags().StringVarP(&titleEditTitle, "title", "t", "",
- "Provide a title to describe the issue",
- )
-}