2018-09-16 14:50:53 +03:00
|
|
|
// Package commands contains the CLI commands
|
2018-07-19 13:30:25 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-08-13 19:32:11 +03:00
|
|
|
"os"
|
|
|
|
|
2019-09-06 12:01:09 +03:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2018-08-06 21:31:20 +03:00
|
|
|
"github.com/MichaelMure/git-bug/bug"
|
2019-02-23 15:01:46 +03:00
|
|
|
"github.com/MichaelMure/git-bug/identity"
|
2018-07-19 13:30:25 +03:00
|
|
|
"github.com/MichaelMure/git-bug/repository"
|
|
|
|
)
|
|
|
|
|
2018-07-20 16:46:14 +03:00
|
|
|
const rootCommandName = "git-bug"
|
2018-07-19 13:30:25 +03:00
|
|
|
|
|
|
|
// package scoped var to hold the repo after the PreRun execution
|
2018-09-21 19:18:51 +03:00
|
|
|
var repo repository.ClockedRepo
|
2018-07-19 13:30:25 +03:00
|
|
|
|
2018-07-20 16:46:14 +03:00
|
|
|
// RootCmd represents the base command when called without any subcommands
|
|
|
|
var RootCmd = &cobra.Command{
|
2018-07-19 13:30:25 +03:00
|
|
|
Use: rootCommandName,
|
2019-02-24 16:46:08 +03:00
|
|
|
Short: "A bug tracker embedded in Git.",
|
2018-09-21 14:37:22 +03:00
|
|
|
Long: `git-bug is a bug tracker embedded in git.
|
2018-07-19 13:30:25 +03:00
|
|
|
|
2018-10-17 21:47:31 +03:00
|
|
|
git-bug use git objects to store the bug tracking separated from the files
|
2018-10-17 21:05:32 +03:00
|
|
|
history. As bugs are regular git objects, they can be pushed and pulled from/to
|
2020-04-30 23:24:03 +03:00
|
|
|
the same git remote you are already using to collaborate with other people.
|
2018-10-17 21:05:32 +03:00
|
|
|
|
|
|
|
`,
|
2018-07-19 13:30:25 +03:00
|
|
|
|
2018-09-11 20:28:32 +03:00
|
|
|
// For the root command, force the execution of the PreRun
|
|
|
|
// even if we just display the help. This is to make sure that we check
|
|
|
|
// the repository and give the user early feedback.
|
2018-07-19 13:30:25 +03:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2018-09-13 13:43:47 +03:00
|
|
|
if err := cmd.Help(); err != nil {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2018-07-19 13:30:25 +03:00
|
|
|
},
|
|
|
|
|
2019-09-06 12:01:09 +03:00
|
|
|
SilenceUsage: true,
|
2018-07-20 16:57:21 +03:00
|
|
|
DisableAutoGenTag: true,
|
2018-08-08 21:12:04 +03:00
|
|
|
|
2018-09-11 20:28:32 +03:00
|
|
|
// Custom bash code to connect the git completion for "git bug" to the
|
|
|
|
// git-bug completion for "git-bug"
|
2018-08-08 21:12:04 +03:00
|
|
|
BashCompletionFunction: `
|
|
|
|
_git_bug() {
|
|
|
|
__start_git-bug "$@"
|
|
|
|
}
|
|
|
|
`,
|
2018-07-19 13:30:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func Execute() {
|
2018-07-20 16:46:14 +03:00
|
|
|
if err := RootCmd.Execute(); err != nil {
|
2018-07-19 13:30:25 +03:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-23 15:01:46 +03:00
|
|
|
// loadRepo is a pre-run function that load the repository for use in a command
|
2018-07-19 13:30:25 +03:00
|
|
|
func loadRepo(cmd *cobra.Command, args []string) error {
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
2019-09-06 12:01:09 +03:00
|
|
|
return fmt.Errorf("unable to get the current working directory: %q", err)
|
2018-07-19 13:30:25 +03:00
|
|
|
}
|
|
|
|
|
2018-08-06 21:31:20 +03:00
|
|
|
repo, err = repository.NewGitRepo(cwd, bug.Witnesser)
|
|
|
|
if err == repository.ErrNotARepo {
|
2019-09-06 12:01:09 +03:00
|
|
|
return fmt.Errorf("%s must be run from within a git repo", rootCommandName)
|
2018-08-06 21:31:20 +03:00
|
|
|
}
|
2018-07-19 13:30:25 +03:00
|
|
|
|
2018-08-06 21:31:20 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-07-19 13:30:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-02-23 15:01:46 +03:00
|
|
|
|
|
|
|
// loadRepoEnsureUser is the same as loadRepo, but also ensure that the user has configured
|
|
|
|
// an identity. Use this pre-run function when an error after using the configured user won't
|
|
|
|
// do.
|
|
|
|
func loadRepoEnsureUser(cmd *cobra.Command, args []string) error {
|
|
|
|
err := loadRepo(cmd, args)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-08 23:15:06 +03:00
|
|
|
_, err = identity.GetUserIdentity(repo)
|
2019-02-23 15:01:46 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|