1package commands
2
3import (
4 "fmt"
5 "github.com/MichaelMure/git-bug/repository"
6 "github.com/spf13/cobra"
7 "os"
8)
9
10// Will display "git bug"
11// \u00A0 is a non-breaking space
12// It's used to avoid cobra to split the Use string at the first space to get the root command name
13//const rootCommandName = "git\u00A0bug"
14const rootCommandName = "git-bug"
15
16// package scoped var to hold the repo after the PreRun execution
17var repo repository.Repo
18
19// RootCmd represents the base command when called without any subcommands
20var RootCmd = &cobra.Command{
21 Use: rootCommandName,
22 Short: "A bugtracker embedded in Git",
23 Long: `git-bug is a bugtracker embedded in git.
24
25It 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.`,
26
27 // Force the execution of the PreRun while still displaying the help
28 Run: func(cmd *cobra.Command, args []string) {
29 cmd.Help()
30 },
31
32 // Load the repo before any command execution
33 // Note, this concern only commands that actually have a Run function
34 PersistentPreRunE: loadRepo,
35
36 DisableAutoGenTag: true,
37}
38
39func Execute() {
40 if err := RootCmd.Execute(); err != nil {
41 fmt.Println(err)
42 os.Exit(1)
43 }
44}
45
46func loadRepo(cmd *cobra.Command, args []string) error {
47 cwd, err := os.Getwd()
48 if err != nil {
49 return fmt.Errorf("Unable to get the current working directory: %q\n", err)
50 }
51
52 repo, err = repository.NewGitRepo(cwd)
53 if err != nil {
54 return fmt.Errorf("%s must be run from within a git repo.\n", rootCommandName)
55
56 }
57
58 return nil
59}