commands: add a "ls-labels" command that output valid labels

Michael Muré created

Change summary

cache/repo_cache.go          | 30 ++++++++++++++++++++++++++++++
commands/ls-labels.go        | 37 +++++++++++++++++++++++++++++++++++++
doc/man/git-bug-ls-label.1   | 32 ++++++++++++++++++++++++++++++++
doc/man/git-bug.1            |  2 +-
doc/md/git-bug.md            |  1 +
doc/md/git-bug_ls-label.md   | 24 ++++++++++++++++++++++++
misc/bash_completion/git-bug | 21 +++++++++++++++++++++
misc/zsh_completion/git-bug  |  2 +-
8 files changed, 147 insertions(+), 2 deletions(-)

Detailed changes

cache/repo_cache.go 🔗

@@ -281,6 +281,36 @@ func (c *RepoCache) ClearAllBugs() {
 	c.bugs = make(map[string]*BugCache)
 }
 
+// ValidLabels 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.
+func (c *RepoCache) ValidLabels() []bug.Label {
+	set := map[bug.Label]interface{}{}
+
+	for _, excerpt := range c.excerpts {
+		for _, l := range excerpt.Labels {
+			set[l] = nil
+		}
+	}
+
+	result := make([]bug.Label, len(set))
+
+	i := 0
+	for l := range set {
+		result[i] = l
+		i++
+	}
+
+	// Sort
+	sort.Slice(result, func(i, j int) bool {
+		return string(result[i]) < string(result[j])
+	})
+
+	return result
+}
+
 // NewBug create a new bug
 // The new bug is written in the repository (commit)
 func (c *RepoCache) NewBug(title string, message string) (*BugCache, error) {

commands/ls-labels.go 🔗

@@ -0,0 +1,37 @@
+package commands
+
+import (
+	"fmt"
+
+	"github.com/MichaelMure/git-bug/cache"
+	"github.com/spf13/cobra"
+)
+
+func runLsLabel(cmd *cobra.Command, args []string) error {
+	backend, err := cache.NewRepoCache(repo)
+	if err != nil {
+		return err
+	}
+	defer backend.Close()
+
+	labels := backend.ValidLabels()
+
+	for _, l := range labels {
+		fmt.Println(l)
+	}
+
+	return nil
+}
+
+var lsLabelCmd = &cobra.Command{
+	Use:   "ls-label",
+	Short: "List valid labels",
+	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.`,
+	RunE: runLsLabel,
+}
+
+func init() {
+	RootCmd.AddCommand(lsLabelCmd)
+}

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

@@ -0,0 +1,32 @@
+.TH "GIT-BUG" "1" "Sep 2018" "Generated from git-bug's source code" "" 
+.nh
+.ad l
+
+
+.SH NAME
+.PP
+git\-bug\-ls\-label \- List valid labels
+
+
+.SH SYNOPSIS
+.PP
+\fBgit\-bug ls\-label [flags]\fP
+
+
+.SH DESCRIPTION
+.PP
+List valid labels.
+
+.PP
+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.
+
+
+.SH OPTIONS
+.PP
+\fB\-h\fP, \fB\-\-help\fP[=false]
+    help for ls\-label
+
+
+.SH SEE ALSO
+.PP
+\fBgit\-bug(1)\fP

doc/man/git-bug.1 🔗

@@ -29,4 +29,4 @@ It use the same internal storage so it doesn't pollute your project. As you woul
 
 .SH SEE ALSO
 .PP
-\fBgit\-bug\-add(1)\fP, \fBgit\-bug\-commands(1)\fP, \fBgit\-bug\-comment(1)\fP, \fBgit\-bug\-label(1)\fP, \fBgit\-bug\-ls(1)\fP, \fBgit\-bug\-pull(1)\fP, \fBgit\-bug\-push(1)\fP, \fBgit\-bug\-select(1)\fP, \fBgit\-bug\-show(1)\fP, \fBgit\-bug\-status(1)\fP, \fBgit\-bug\-termui(1)\fP, \fBgit\-bug\-title(1)\fP, \fBgit\-bug\-webui(1)\fP
+\fBgit\-bug\-add(1)\fP, \fBgit\-bug\-commands(1)\fP, \fBgit\-bug\-comment(1)\fP, \fBgit\-bug\-label(1)\fP, \fBgit\-bug\-ls(1)\fP, \fBgit\-bug\-ls\-label(1)\fP, \fBgit\-bug\-pull(1)\fP, \fBgit\-bug\-push(1)\fP, \fBgit\-bug\-select(1)\fP, \fBgit\-bug\-show(1)\fP, \fBgit\-bug\-status(1)\fP, \fBgit\-bug\-termui(1)\fP, \fBgit\-bug\-title(1)\fP, \fBgit\-bug\-webui(1)\fP

doc/md/git-bug.md 🔗

@@ -25,6 +25,7 @@ git-bug [flags]
 * [git-bug comment](git-bug_comment.md)	 - Display or add comments
 * [git-bug label](git-bug_label.md)	 - Display, add or remove labels
 * [git-bug ls](git-bug_ls.md)	 - List bugs
+* [git-bug ls-label](git-bug_ls-label.md)	 - List valid labels
 * [git-bug pull](git-bug_pull.md)	 - Pull bugs update from a git remote
 * [git-bug push](git-bug_push.md)	 - Push bugs update to a git remote
 * [git-bug select](git-bug_select.md)	 - Select a bug for implicit use in future commands

doc/md/git-bug_ls-label.md 🔗

@@ -0,0 +1,24 @@
+## git-bug ls-label
+
+List valid labels
+
+### Synopsis
+
+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.
+
+```
+git-bug ls-label [flags]
+```
+
+### Options
+
+```
+  -h, --help   help for ls-label
+```
+
+### SEE ALSO
+
+* [git-bug](git-bug.md)	 - A bug tracker embedded in Git
+

misc/bash_completion/git-bug 🔗

@@ -447,6 +447,26 @@ _git-bug_ls()
     noun_aliases=()
 }
 
+_git-bug_ls-label()
+{
+    last_command="git-bug_ls-label"
+
+    command_aliases=()
+
+    commands=()
+
+    flags=()
+    two_word_flags=()
+    local_nonpersistent_flags=()
+    flags_with_completion=()
+    flags_completion=()
+
+
+    must_have_one_flag=()
+    must_have_one_noun=()
+    noun_aliases=()
+}
+
 _git-bug_pull()
 {
     last_command="git-bug_pull"
@@ -688,6 +708,7 @@ _git-bug_root_command()
     commands+=("comment")
     commands+=("label")
     commands+=("ls")
+    commands+=("ls-label")
     commands+=("pull")
     commands+=("push")
     commands+=("select")

misc/zsh_completion/git-bug 🔗

@@ -8,7 +8,7 @@ case $state in
   level1)
     case $words[1] in
       git-bug)
-        _arguments '1: :(add commands comment label ls pull push select show status termui title webui)'
+        _arguments '1: :(add commands comment label ls ls-label pull push select show status termui title webui)'
       ;;
       *)
         _arguments '*: :_files'