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.ClockedRepo
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
26git-bug use git objects to store the bug tracking separated from the files
27history. As bugs are regular git objects, they can be pushed and pulled from/to
28the same git remote your are already using to collaborate with other peoples.
29
30`,
31
32	// For the root command, force the execution of the PreRun
33	// even if we just display the help. This is to make sure that we check
34	// the repository and give the user early feedback.
35	Run: func(cmd *cobra.Command, args []string) {
36		if err := cmd.Help(); err != nil {
37			os.Exit(1)
38		}
39	},
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}