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 Use: rootCommandName,
21 Short: "A bug tracker embedded in Git",
22 Long: `git-bug is a bug tracker embedded in git.
23
24git-bug use git objects to store the bug tracking separated from the files
25history. As bugs are regular git objects, they can be pushed and pulled from/to
26the same git remote your are already using to collaborate with other peoples.
27
28`,
29
30 // For the root command, force the execution of the PreRun
31 // even if we just display the help. This is to make sure that we check
32 // the repository and give the user early feedback.
33 Run: func(cmd *cobra.Command, args []string) {
34 if err := cmd.Help(); err != nil {
35 os.Exit(1)
36 }
37 },
38
39 DisableAutoGenTag: true,
40
41 // Custom bash code to connect the git completion for "git bug" to the
42 // git-bug completion for "git-bug"
43 BashCompletionFunction: `
44_git_bug() {
45 __start_git-bug "$@"
46}
47`,
48}
49
50func Execute() {
51 if err := RootCmd.Execute(); err != nil {
52 os.Exit(1)
53 }
54}
55
56func loadRepo(cmd *cobra.Command, args []string) error {
57 cwd, err := os.Getwd()
58 if err != nil {
59 return fmt.Errorf("Unable to get the current working directory: %q\n", err)
60 }
61
62 repo, err = repository.NewGitRepo(cwd, bug.Witnesser)
63 if err == repository.ErrNotARepo {
64 return fmt.Errorf("%s must be run from within a git repo.\n", rootCommandName)
65 }
66
67 if err != nil {
68 return err
69 }
70
71 return nil
72}