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