aboutsummaryrefslogtreecommitdiffstats
path: root/commands/ls-labels.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/ls-labels.go
parentc0dbc149d5c0c3610476ba14a800c9ba803a2c2c (diff)
downloadgit-bug-26bd1dd11010b4d86cebe2510ad7085a6b316334.tar.gz
commands: refactor to avoid globals
Diffstat (limited to 'commands/ls-labels.go')
-rw-r--r--commands/ls-labels.go41
1 files changed, 22 insertions, 19 deletions
diff --git a/commands/ls-labels.go b/commands/ls-labels.go
index 5610fb56..3473aadd 100644
--- a/commands/ls-labels.go
+++ b/commands/ls-labels.go
@@ -1,15 +1,32 @@
package commands
import (
- "fmt"
+ "github.com/spf13/cobra"
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/util/interrupt"
- "github.com/spf13/cobra"
)
-func runLsLabel(cmd *cobra.Command, args []string) error {
- backend, err := cache.NewRepoCache(repo)
+func newLsLabelCommand() *cobra.Command {
+ env := newEnv()
+
+ cmd := &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.`,
+ PreRunE: loadRepo(env),
+ RunE: func(cmd *cobra.Command, args []string) error {
+ return runLsLabel(env)
+ },
+ }
+
+ return cmd
+}
+
+func runLsLabel(env *Env) error {
+ backend, err := cache.NewRepoCache(env.repo)
if err != nil {
return err
}
@@ -19,22 +36,8 @@ func runLsLabel(cmd *cobra.Command, args []string) error {
labels := backend.ValidLabels()
for _, l := range labels {
- fmt.Println(l)
+ env.out.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.`,
- PreRunE: loadRepo,
- RunE: runLsLabel,
-}
-
-func init() {
- RootCmd.AddCommand(lsLabelCmd)
-}