diff options
author | Michael Muré <batolettre@gmail.com> | 2018-07-18 16:41:09 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2018-07-18 16:41:09 +0200 |
commit | cfa11372cbcbb5f4553f3dcd8897d5f5207c6507 (patch) | |
tree | a5136685378a9c3310c86700bbeaf34338281f9a /commands | |
parent | ba3281dc9918fa49f10c2a166b5b631a931d2d51 (diff) | |
download | git-bug-cfa11372cbcbb5f4553f3dcd8897d5f5207c6507.tar.gz |
implement label op+command
Diffstat (limited to 'commands')
-rw-r--r-- | commands/command.go | 1 | ||||
-rw-r--r-- | commands/label.go | 108 | ||||
-rw-r--r-- | commands/show.go | 11 |
3 files changed, 118 insertions, 2 deletions
diff --git a/commands/command.go b/commands/command.go index 30f86848..89420b46 100644 --- a/commands/command.go +++ b/commands/command.go @@ -47,6 +47,7 @@ func init() { "close": closeCmd, "commands": commandsCmd, "comment": commentCmd, + "label": labelCmd, "ls": lsCmd, "new": newCmd, "open": openCmd, diff --git a/commands/label.go b/commands/label.go new file mode 100644 index 00000000..ad3a388b --- /dev/null +++ b/commands/label.go @@ -0,0 +1,108 @@ +package commands + +import ( + "errors" + "flag" + "fmt" + "github.com/MichaelMure/git-bug/bug" + "github.com/MichaelMure/git-bug/bug/operations" + "github.com/MichaelMure/git-bug/repository" +) + +var labelFlagSet = flag.NewFlagSet("label", flag.ExitOnError) + +var ( + labelRemove = newFlagSet.Bool("r", false, "Remove a label") +) + +func runLabel(repo repository.Repo, args []string) error { + labelFlagSet.Parse(args) + args = labelFlagSet.Args() + + if len(args) == 0 { + return errors.New("You must provide a bug id") + } + + if len(args) == 1 { + return errors.New("You must provide a label") + } + + prefix := args[0] + + b, err := bug.FindBug(repo, prefix) + if err != nil { + return err + } + + author, err := bug.GetUser(repo) + if err != nil { + return err + } + + var added, removed []bug.Label + + snap := b.Compile() + + for _, arg := range args[1:] { + label := bug.Label(arg) + + if *labelRemove { + // check for duplicate + if labelExist(removed, label) { + fmt.Printf("label \"%s\" is a duplicate\n", arg) + continue + } + + // check that the label actually exist + if !labelExist(snap.Labels, label) { + fmt.Printf("label \"%s\" doesn't exist on this bug\n", arg) + continue + } + + removed = append(removed, label) + } else { + // check for duplicate + if labelExist(added, label) { + fmt.Printf("label \"%s\" is a duplicate\n", arg) + continue + } + + // check that the label doesn't already exist + if labelExist(snap.Labels, label) { + fmt.Printf("label \"%s\" is already set on this bug\n", arg) + continue + } + + added = append(added, label) + } + } + + if len(added) == 0 && len(removed) == 0 { + return errors.New("no label added or removed") + } + + labelOp := operations.NewLabelChangeOperation(author, added, removed) + + b.Append(labelOp) + + err = b.Commit(repo) + + return err +} + +func labelExist(labels []bug.Label, label bug.Label) bool { + for _, l := range labels { + if l == label { + return true + } + } + + return false +} + +var labelCmd = &Command{ + Description: "Manipulate bug's label", + Usage: "<id> [<option>...] [<label>...]", + flagSet: labelFlagSet, + RunMethod: runLabel, +} diff --git a/commands/show.go b/commands/show.go index a8414384..c5a043c7 100644 --- a/commands/show.go +++ b/commands/show.go @@ -9,8 +9,6 @@ import ( "strings" ) -var line = strings.Repeat("-", 50) - func runShowBug(repo repository.Repo, args []string) error { if len(args) > 1 { return errors.New("Only showing one bug at a time is supported") @@ -47,6 +45,15 @@ func runShowBug(repo repository.Repo, args []string) error { firstComment.FormatTime(), ) + var labels = make([]string, len(snapshot.Labels)) + for i := range snapshot.Labels { + labels[i] = string(snapshot.Labels[i]) + } + + fmt.Printf("labels: %s\n\n", + strings.Join(labels, ", "), + ) + // Comments indent := " " |