commands: convert compatible commands to the implicit select mechanism

Michael Muré created

Change summary

commands/comment.go            | 18 +++---------------
commands/comment_add.go        | 18 +++---------------
commands/label add.go          | 15 ++++-----------
commands/label rm.go           | 15 ++++-----------
commands/label.go              | 16 +++-------------
commands/select.go             |  2 +-
commands/show.go               | 15 +++------------
commands/status.go             | 18 +++---------------
commands/status_close.go       | 17 +++--------------
commands/status_open.go        | 17 +++--------------
commands/title.go              | 18 +++---------------
commands/title_edit.go         | 18 +++---------------
doc/man/git-bug-comment-add.1  |  2 +-
doc/man/git-bug-comment.1      |  2 +-
doc/man/git-bug-label-add.1    |  2 +-
doc/man/git-bug-label-rm.1     |  2 +-
doc/man/git-bug-label.1        |  2 +-
doc/man/git-bug-select.1       |  2 +-
doc/man/git-bug-show.1         |  2 +-
doc/man/git-bug-status-close.1 |  2 +-
doc/man/git-bug-status-open.1  |  2 +-
doc/man/git-bug-status.1       |  2 +-
doc/man/git-bug-title-edit.1   |  2 +-
doc/man/git-bug-title.1        |  2 +-
doc/md/git-bug_comment.md      |  2 +-
doc/md/git-bug_comment_add.md  |  2 +-
doc/md/git-bug_label.md        |  2 +-
doc/md/git-bug_label_add.md    |  2 +-
doc/md/git-bug_label_rm.md     |  2 +-
doc/md/git-bug_select.md       |  2 +-
doc/md/git-bug_show.md         |  2 +-
doc/md/git-bug_status.md       |  2 +-
doc/md/git-bug_status_close.md |  2 +-
doc/md/git-bug_status_open.md  |  2 +-
doc/md/git-bug_title.md        |  2 +-
doc/md/git-bug_title_edit.md   |  2 +-
36 files changed, 60 insertions(+), 175 deletions(-)

Detailed changes

commands/comment.go 🔗

@@ -1,36 +1,24 @@
 package commands
 
 import (
-	"errors"
 	"fmt"
 
 	"github.com/MichaelMure/git-bug/bug"
 	"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/text"
 	"github.com/spf13/cobra"
 )
 
 func runComment(cmd *cobra.Command, args []string) error {
-	var err error
-
-	if len(args) > 1 {
-		return errors.New("Only one bug id is supported")
-	}
-
-	if len(args) == 0 {
-		return errors.New("You must provide a bug id")
-	}
-
 	backend, err := cache.NewRepoCache(repo)
 	if err != nil {
 		return err
 	}
 	defer backend.Close()
 
-	prefix := args[0]
-
-	b, err := backend.ResolveBugPrefix(prefix)
+	b, args, err := _select.ResolveBug(backend, args)
 	if err != nil {
 		return err
 	}
@@ -55,7 +43,7 @@ func commentsTextOutput(comments []bug.Comment) {
 }
 
 var commentCmd = &cobra.Command{
-	Use:   "comment <id>",
+	Use:   "comment [<id>]",
 	Short: "Show a bug's comments",
 	RunE:  runComment,
 }

commands/comment_add.go 🔗

@@ -4,8 +4,8 @@ import (
 	"fmt"
 
 	"github.com/MichaelMure/git-bug/cache"
+	"github.com/MichaelMure/git-bug/commands/select"
 	"github.com/MichaelMure/git-bug/input"
-	"github.com/pkg/errors"
 	"github.com/spf13/cobra"
 )
 
@@ -15,24 +15,12 @@ var (
 )
 
 func runCommentAdd(cmd *cobra.Command, args []string) error {
-	var err error
-
-	if len(args) > 1 {
-		return errors.New("Only one bug id is supported")
-	}
-
-	if len(args) == 0 {
-		return errors.New("You must provide a bug id")
-	}
-
 	backend, err := cache.NewRepoCache(repo)
 	if err != nil {
 		return err
 	}
 	defer backend.Close()
 
-	prefix := args[0]
-
 	if commentAddMessageFile != "" && commentAddMessage == "" {
 		commentAddMessage, err = input.FromFile(commentAddMessageFile)
 		if err != nil {
@@ -51,7 +39,7 @@ func runCommentAdd(cmd *cobra.Command, args []string) error {
 		}
 	}
 
-	b, err := backend.ResolveBugPrefix(prefix)
+	b, args, err := _select.ResolveBug(backend, args)
 	if err != nil {
 		return err
 	}
@@ -65,7 +53,7 @@ func runCommentAdd(cmd *cobra.Command, args []string) error {
 }
 
 var commentAddCmd = &cobra.Command{
-	Use:   "add <id>",
+	Use:   "add [<id>]",
 	Short: "Add a new comment to a bug",
 	RunE:  runCommentAdd,
 }

commands/label add.go 🔗

@@ -1,33 +1,26 @@
 package commands
 
 import (
-	"errors"
 	"fmt"
 
 	"github.com/MichaelMure/git-bug/cache"
+	"github.com/MichaelMure/git-bug/commands/select"
 	"github.com/spf13/cobra"
 )
 
 func runLabelAdd(cmd *cobra.Command, args []string) error {
-	if len(args) == 0 {
-		return errors.New("You must provide a bug id")
-	}
-
 	backend, err := cache.NewRepoCache(repo)
 	if err != nil {
 		return err
 	}
 	defer backend.Close()
 
-	prefix := args[0]
-	add := args[1:]
-
-	b, err := backend.ResolveBugPrefix(prefix)
+	b, args, err := _select.ResolveBug(backend, args)
 	if err != nil {
 		return err
 	}
 
-	changes, err := b.ChangeLabels(add, nil)
+	changes, err := b.ChangeLabels(args, nil)
 
 	for _, change := range changes {
 		fmt.Println(change)
@@ -41,7 +34,7 @@ func runLabelAdd(cmd *cobra.Command, args []string) error {
 }
 
 var labelAddCmd = &cobra.Command{
-	Use:   "add <id> [<label>...]",
+	Use:   "add [<id>] <label>[...]",
 	Short: "Add a label to a bug",
 	RunE:  runLabelAdd,
 }

commands/label rm.go 🔗

@@ -1,33 +1,26 @@
 package commands
 
 import (
-	"errors"
 	"fmt"
 
 	"github.com/MichaelMure/git-bug/cache"
+	"github.com/MichaelMure/git-bug/commands/select"
 	"github.com/spf13/cobra"
 )
 
 func runLabelRm(cmd *cobra.Command, args []string) error {
-	if len(args) == 0 {
-		return errors.New("You must provide a bug id")
-	}
-
 	backend, err := cache.NewRepoCache(repo)
 	if err != nil {
 		return err
 	}
 	defer backend.Close()
 
-	prefix := args[0]
-	remove := args[1:]
-
-	b, err := backend.ResolveBugPrefix(prefix)
+	b, args, err := _select.ResolveBug(backend, args)
 	if err != nil {
 		return err
 	}
 
-	changes, err := b.ChangeLabels(nil, remove)
+	changes, err := b.ChangeLabels(nil, args)
 
 	for _, change := range changes {
 		fmt.Println(change)
@@ -41,7 +34,7 @@ func runLabelRm(cmd *cobra.Command, args []string) error {
 }
 
 var labelRmCmd = &cobra.Command{
-	Use:   "rm <id> [<label>...]",
+	Use:   "rm [<id>] <label>[...]",
 	Short: "Remove a label from a bug",
 	RunE:  runLabelRm,
 }

commands/label.go 🔗

@@ -1,31 +1,21 @@
 package commands
 
 import (
-	"errors"
 	"fmt"
 
 	"github.com/MichaelMure/git-bug/cache"
+	"github.com/MichaelMure/git-bug/commands/select"
 	"github.com/spf13/cobra"
 )
 
 func runLabel(cmd *cobra.Command, args []string) error {
-	if len(args) > 1 {
-		return errors.New("Only one bug id is supported")
-	}
-
-	if len(args) == 0 {
-		return errors.New("You must provide a bug id")
-	}
-
 	backend, err := cache.NewRepoCache(repo)
 	if err != nil {
 		return err
 	}
 	defer backend.Close()
 
-	prefix := args[0]
-
-	b, err := backend.ResolveBugPrefix(prefix)
+	b, args, err := _select.ResolveBug(backend, args)
 	if err != nil {
 		return err
 	}
@@ -40,7 +30,7 @@ func runLabel(cmd *cobra.Command, args []string) error {
 }
 
 var labelCmd = &cobra.Command{
-	Use:   "label <id>",
+	Use:   "label [<id>]",
 	Short: "Display a bug labels",
 	RunE:  runLabel,
 }

commands/select.go 🔗

@@ -35,7 +35,7 @@ func runSelect(cmd *cobra.Command, args []string) error {
 }
 
 var selectCmd = &cobra.Command{
-	Use:   "select [<id>]",
+	Use:   "select <id>",
 	Short: "Select a bug for implicit use in future commands",
 	Example: `git bug select 2f15
 git bug comment

commands/show.go 🔗

@@ -6,28 +6,19 @@ import (
 	"strings"
 
 	"github.com/MichaelMure/git-bug/cache"
+	"github.com/MichaelMure/git-bug/commands/select"
 	"github.com/MichaelMure/git-bug/util/colors"
 	"github.com/spf13/cobra"
 )
 
 func runShowBug(cmd *cobra.Command, args []string) error {
-	if len(args) > 1 {
-		return errors.New("Only showing one bug at a time is supported")
-	}
-
-	if len(args) == 0 {
-		return errors.New("You must provide a bug id")
-	}
-
 	backend, err := cache.NewRepoCache(repo)
 	if err != nil {
 		return err
 	}
 	defer backend.Close()
 
-	prefix := args[0]
-
-	b, err := backend.ResolveBugPrefix(prefix)
+	b, args, err := _select.ResolveBug(backend, args)
 	if err != nil {
 		return err
 	}
@@ -82,7 +73,7 @@ func runShowBug(cmd *cobra.Command, args []string) error {
 }
 
 var showCmd = &cobra.Command{
-	Use:   "show <id>",
+	Use:   "show [<id>]",
 	Short: "Display the details of a bug",
 	RunE:  runShowBug,
 }

commands/status.go 🔗

@@ -4,30 +4,18 @@ import (
 	"fmt"
 
 	"github.com/MichaelMure/git-bug/cache"
-	"github.com/pkg/errors"
+	"github.com/MichaelMure/git-bug/commands/select"
 	"github.com/spf13/cobra"
 )
 
 func runStatus(cmd *cobra.Command, args []string) error {
-	var err error
-
-	if len(args) > 1 {
-		return errors.New("Only one bug id is supported")
-	}
-
-	if len(args) == 0 {
-		return errors.New("You must provide a bug id")
-	}
-
 	backend, err := cache.NewRepoCache(repo)
 	if err != nil {
 		return err
 	}
 	defer backend.Close()
 
-	prefix := args[0]
-
-	b, err := backend.ResolveBugPrefix(prefix)
+	b, args, err := _select.ResolveBug(backend, args)
 	if err != nil {
 		return err
 	}
@@ -40,7 +28,7 @@ func runStatus(cmd *cobra.Command, args []string) error {
 }
 
 var statusCmd = &cobra.Command{
-	Use:   "status <id>",
+	Use:   "status [<id>]",
 	Short: "Show the bug status",
 	RunE:  runStatus,
 }

commands/status_close.go 🔗

@@ -1,30 +1,19 @@
 package commands
 
 import (
-	"errors"
-
 	"github.com/MichaelMure/git-bug/cache"
+	"github.com/MichaelMure/git-bug/commands/select"
 	"github.com/spf13/cobra"
 )
 
 func runStatusClose(cmd *cobra.Command, args []string) error {
-	if len(args) > 1 {
-		return errors.New("Only closing one bug at a time is supported")
-	}
-
-	if len(args) == 0 {
-		return errors.New("You must provide a bug id")
-	}
-
 	backend, err := cache.NewRepoCache(repo)
 	if err != nil {
 		return err
 	}
 	defer backend.Close()
 
-	prefix := args[0]
-
-	b, err := backend.ResolveBugPrefix(prefix)
+	b, args, err := _select.ResolveBug(backend, args)
 	if err != nil {
 		return err
 	}
@@ -38,7 +27,7 @@ func runStatusClose(cmd *cobra.Command, args []string) error {
 }
 
 var closeCmd = &cobra.Command{
-	Use:   "close <id>",
+	Use:   "close [<id>]",
 	Short: "Mark the bug as closed",
 	RunE:  runStatusClose,
 }

commands/status_open.go 🔗

@@ -1,30 +1,19 @@
 package commands
 
 import (
-	"errors"
-
 	"github.com/MichaelMure/git-bug/cache"
+	"github.com/MichaelMure/git-bug/commands/select"
 	"github.com/spf13/cobra"
 )
 
 func runStatusOpen(cmd *cobra.Command, args []string) error {
-	if len(args) > 1 {
-		return errors.New("Only opening one bug at a time is supported")
-	}
-
-	if len(args) == 0 {
-		return errors.New("You must provide a bug id")
-	}
-
 	backend, err := cache.NewRepoCache(repo)
 	if err != nil {
 		return err
 	}
 	defer backend.Close()
 
-	prefix := args[0]
-
-	b, err := backend.ResolveBugPrefix(prefix)
+	b, args, err := _select.ResolveBug(backend, args)
 	if err != nil {
 		return err
 	}
@@ -38,7 +27,7 @@ func runStatusOpen(cmd *cobra.Command, args []string) error {
 }
 
 var openCmd = &cobra.Command{
-	Use:   "open <id>",
+	Use:   "open [<id>]",
 	Short: "Mark the bug as open",
 	RunE:  runStatusOpen,
 }

commands/title.go 🔗

@@ -4,30 +4,18 @@ import (
 	"fmt"
 
 	"github.com/MichaelMure/git-bug/cache"
-	"github.com/pkg/errors"
+	"github.com/MichaelMure/git-bug/commands/select"
 	"github.com/spf13/cobra"
 )
 
 func runTitle(cmd *cobra.Command, args []string) error {
-	var err error
-
-	if len(args) > 1 {
-		return errors.New("Only one bug id is supported")
-	}
-
-	if len(args) == 0 {
-		return errors.New("You must provide a bug id")
-	}
-
 	backend, err := cache.NewRepoCache(repo)
 	if err != nil {
 		return err
 	}
 	defer backend.Close()
 
-	prefix := args[0]
-
-	b, err := backend.ResolveBugPrefix(prefix)
+	b, args, err := _select.ResolveBug(backend, args)
 	if err != nil {
 		return err
 	}
@@ -40,7 +28,7 @@ func runTitle(cmd *cobra.Command, args []string) error {
 }
 
 var titleCmd = &cobra.Command{
-	Use:   "title <id>",
+	Use:   "title [<id>]",
 	Short: "Display a bug's title",
 	RunE:  runTitle,
 }

commands/title_edit.go 🔗

@@ -1,10 +1,10 @@
 package commands
 
 import (
-	"errors"
 	"fmt"
 
 	"github.com/MichaelMure/git-bug/cache"
+	"github.com/MichaelMure/git-bug/commands/select"
 	"github.com/MichaelMure/git-bug/input"
 	"github.com/spf13/cobra"
 )
@@ -14,25 +14,13 @@ var (
 )
 
 func runTitleEdit(cmd *cobra.Command, args []string) error {
-	var err error
-
-	if len(args) > 1 {
-		return errors.New("Only one bug id is supported")
-	}
-
-	if len(args) == 0 {
-		return errors.New("You must provide a bug id")
-	}
-
 	backend, err := cache.NewRepoCache(repo)
 	if err != nil {
 		return err
 	}
 	defer backend.Close()
 
-	prefix := args[0]
-
-	b, err := backend.ResolveBugPrefix(prefix)
+	b, args, err := _select.ResolveBug(backend, args)
 	if err != nil {
 		return err
 	}
@@ -59,7 +47,7 @@ func runTitleEdit(cmd *cobra.Command, args []string) error {
 }
 
 var titleEditCmd = &cobra.Command{
-	Use:   "edit <id>",
+	Use:   "edit [<id>]",
 	Short: "Edit a bug title",
 	RunE:  runTitleEdit,
 }

doc/man/git-bug-comment-add.1 🔗

@@ -10,7 +10,7 @@ git\-bug\-comment\-add \- Add a new comment to a bug
 
 .SH SYNOPSIS
 .PP
-\fBgit\-bug comment add <id> [flags]\fP
+\fBgit\-bug comment add [<id>] [flags]\fP
 
 
 .SH DESCRIPTION

doc/man/git-bug-comment.1 🔗

@@ -10,7 +10,7 @@ git\-bug\-comment \- Show a bug's comments
 
 .SH SYNOPSIS
 .PP
-\fBgit\-bug comment <id> [flags]\fP
+\fBgit\-bug comment [<id>] [flags]\fP
 
 
 .SH DESCRIPTION

doc/man/git-bug-label-add.1 🔗

@@ -10,7 +10,7 @@ git\-bug\-label\-add \- Add a label to a bug
 
 .SH SYNOPSIS
 .PP
-\fBgit\-bug label add <id> [<label>\&...] [flags]\fP
+\fBgit\-bug label add [<id>] <label>[...] [flags]\fP
 
 
 .SH DESCRIPTION

doc/man/git-bug-label-rm.1 🔗

@@ -10,7 +10,7 @@ git\-bug\-label\-rm \- Remove a label from a bug
 
 .SH SYNOPSIS
 .PP
-\fBgit\-bug label rm <id> [<label>\&...] [flags]\fP
+\fBgit\-bug label rm [<id>] <label>[...] [flags]\fP
 
 
 .SH DESCRIPTION

doc/man/git-bug-label.1 🔗

@@ -10,7 +10,7 @@ git\-bug\-label \- Display a bug labels
 
 .SH SYNOPSIS
 .PP
-\fBgit\-bug label <id> [flags]\fP
+\fBgit\-bug label [<id>] [flags]\fP
 
 
 .SH DESCRIPTION

doc/man/git-bug-select.1 🔗

@@ -10,7 +10,7 @@ git\-bug\-select \- Select a bug for implicit use in future commands
 
 .SH SYNOPSIS
 .PP
-\fBgit\-bug select [<id>] [flags]\fP
+\fBgit\-bug select <id> [flags]\fP
 
 
 .SH DESCRIPTION

doc/man/git-bug-show.1 🔗

@@ -10,7 +10,7 @@ git\-bug\-show \- Display the details of a bug
 
 .SH SYNOPSIS
 .PP
-\fBgit\-bug show <id> [flags]\fP
+\fBgit\-bug show [<id>] [flags]\fP
 
 
 .SH DESCRIPTION

doc/man/git-bug-status-close.1 🔗

@@ -10,7 +10,7 @@ git\-bug\-status\-close \- Mark the bug as closed
 
 .SH SYNOPSIS
 .PP
-\fBgit\-bug status close <id> [flags]\fP
+\fBgit\-bug status close [<id>] [flags]\fP
 
 
 .SH DESCRIPTION

doc/man/git-bug-status-open.1 🔗

@@ -10,7 +10,7 @@ git\-bug\-status\-open \- Mark the bug as open
 
 .SH SYNOPSIS
 .PP
-\fBgit\-bug status open <id> [flags]\fP
+\fBgit\-bug status open [<id>] [flags]\fP
 
 
 .SH DESCRIPTION

doc/man/git-bug-status.1 🔗

@@ -10,7 +10,7 @@ git\-bug\-status \- Show the bug status
 
 .SH SYNOPSIS
 .PP
-\fBgit\-bug status <id> [flags]\fP
+\fBgit\-bug status [<id>] [flags]\fP
 
 
 .SH DESCRIPTION

doc/man/git-bug-title-edit.1 🔗

@@ -10,7 +10,7 @@ git\-bug\-title\-edit \- Edit a bug title
 
 .SH SYNOPSIS
 .PP
-\fBgit\-bug title edit <id> [flags]\fP
+\fBgit\-bug title edit [<id>] [flags]\fP
 
 
 .SH DESCRIPTION

doc/man/git-bug-title.1 🔗

@@ -10,7 +10,7 @@ git\-bug\-title \- Display a bug's title
 
 .SH SYNOPSIS
 .PP
-\fBgit\-bug title <id> [flags]\fP
+\fBgit\-bug title [<id>] [flags]\fP
 
 
 .SH DESCRIPTION

doc/md/git-bug_comment.md 🔗

@@ -7,7 +7,7 @@ Show a bug's comments
 Show a bug's comments
 
 ```
-git-bug comment <id> [flags]
+git-bug comment [<id>] [flags]
 ```
 
 ### Options

doc/md/git-bug_comment_add.md 🔗

@@ -7,7 +7,7 @@ Add a new comment to a bug
 Add a new comment to a bug
 
 ```
-git-bug comment add <id> [flags]
+git-bug comment add [<id>] [flags]
 ```
 
 ### Options

doc/md/git-bug_label.md 🔗

@@ -7,7 +7,7 @@ Display a bug labels
 Display a bug labels
 
 ```
-git-bug label <id> [flags]
+git-bug label [<id>] [flags]
 ```
 
 ### Options

doc/md/git-bug_label_add.md 🔗

@@ -7,7 +7,7 @@ Add a label to a bug
 Add a label to a bug
 
 ```
-git-bug label add <id> [<label>...] [flags]
+git-bug label add [<id>] <label>[...] [flags]
 ```
 
 ### Options

doc/md/git-bug_label_rm.md 🔗

@@ -7,7 +7,7 @@ Remove a label from a bug
 Remove a label from a bug
 
 ```
-git-bug label rm <id> [<label>...] [flags]
+git-bug label rm [<id>] <label>[...] [flags]
 ```
 
 ### Options

doc/md/git-bug_select.md 🔗

@@ -7,7 +7,7 @@ Select a bug for implicit use in future commands
 Select a bug for implicit use in future commands
 
 ```
-git-bug select [<id>] [flags]
+git-bug select <id> [flags]
 ```
 
 ### Examples

doc/md/git-bug_show.md 🔗

@@ -7,7 +7,7 @@ Display the details of a bug
 Display the details of a bug
 
 ```
-git-bug show <id> [flags]
+git-bug show [<id>] [flags]
 ```
 
 ### Options

doc/md/git-bug_status.md 🔗

@@ -7,7 +7,7 @@ Show the bug status
 Show the bug status
 
 ```
-git-bug status <id> [flags]
+git-bug status [<id>] [flags]
 ```
 
 ### Options

doc/md/git-bug_status_close.md 🔗

@@ -7,7 +7,7 @@ Mark the bug as closed
 Mark the bug as closed
 
 ```
-git-bug status close <id> [flags]
+git-bug status close [<id>] [flags]
 ```
 
 ### Options

doc/md/git-bug_status_open.md 🔗

@@ -7,7 +7,7 @@ Mark the bug as open
 Mark the bug as open
 
 ```
-git-bug status open <id> [flags]
+git-bug status open [<id>] [flags]
 ```
 
 ### Options

doc/md/git-bug_title.md 🔗

@@ -7,7 +7,7 @@ Display a bug's title
 Display a bug's title
 
 ```
-git-bug title <id> [flags]
+git-bug title [<id>] [flags]
 ```
 
 ### Options

doc/md/git-bug_title_edit.md 🔗

@@ -7,7 +7,7 @@ Edit a bug title
 Edit a bug title
 
 ```
-git-bug title edit <id> [flags]
+git-bug title edit [<id>] [flags]
 ```
 
 ### Options