1// Package commands contains the CLI commands
2package commands
3
4import (
5 "fmt"
6 "os"
7
8 "github.com/MichaelMure/git-bug/bug"
9 "github.com/MichaelMure/git-bug/repository"
10 "github.com/spf13/cobra"
11)
12
13const rootCommandName = "git-bug"
14
15// package scoped var to hold the repo after the PreRun execution
16var repo repository.Repo
17
18// RootCmd represents the base command when called without any subcommands
19var RootCmd = &cobra.Command{
20 Version: "0.3.0",
21
22 Use: rootCommandName,
23 Short: "A bug tracker embedded in Git",
24 Long: `git-bug is a bug tracker embedded in git.
25
26It use the same internal storage so it doesn't pollute your project. As you would do with commits and branches, you can push your bugs to the same git remote your are already using to collaborate with other peoples.`,
27
28 // For the root command, force the execution of the PreRun
29 // even if we just display the help. This is to make sure that we check
30 // the repository and give the user early feedback.
31 Run: func(cmd *cobra.Command, args []string) {
32 if err := cmd.Help(); err != nil {
33 os.Exit(1)
34 }
35 },
36
37 // Load the repo before any command execution
38 // Note, this concern only commands that actually have a Run function
39 PersistentPreRunE: loadRepo,
40
41 DisableAutoGenTag: true,
42
43 // Custom bash code to connect the git completion for "git bug" to the
44 // git-bug completion for "git-bug"
45 BashCompletionFunction: `
46_git_bug() {
47 __start_git-bug "$@"
48}
49`,
50}
51
52func Execute() {
53 if err := RootCmd.Execute(); err != nil {
54 os.Exit(1)
55 }
56}
57
58func loadRepo(cmd *cobra.Command, args []string) error {
59 cwd, err := os.Getwd()
60 if err != nil {
61 return fmt.Errorf("Unable to get the current working directory: %q\n", err)
62 }
63
64 repo, err = repository.NewGitRepo(cwd, bug.Witnesser)
65 if err == repository.ErrNotARepo {
66 return fmt.Errorf("%s must be run from within a git repo.\n", rootCommandName)
67 }
68
69 if err != nil {
70 return err
71 }
72
73 return nil
74}