1package commands
 2
 3import (
 4	"fmt"
 5	"os"
 6
 7	"github.com/MichaelMure/git-bug/bug"
 8	"github.com/MichaelMure/git-bug/repository"
 9	"github.com/spf13/cobra"
10)
11
12const rootCommandName = "git-bug"
13
14// package scoped var to hold the repo after the PreRun execution
15var repo repository.Repo
16
17// RootCmd represents the base command when called without any subcommands
18var RootCmd = &cobra.Command{
19	Version: "0.2.0",
20
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	BashCompletionFunction: `
39_git_bug() {
40    __start_git-bug "$@"
41}
42`,
43}
44
45func Execute() {
46	if err := RootCmd.Execute(); err != nil {
47		os.Exit(1)
48	}
49}
50
51func loadRepo(cmd *cobra.Command, args []string) error {
52	cwd, err := os.Getwd()
53	if err != nil {
54		return fmt.Errorf("Unable to get the current working directory: %q\n", err)
55	}
56
57	repo, err = repository.NewGitRepo(cwd, bug.Witnesser)
58	if err == repository.ErrNotARepo {
59		return fmt.Errorf("%s must be run from within a git repo.\n", rootCommandName)
60	}
61
62	if err != nil {
63		return err
64	}
65
66	return nil
67}