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