github.com/Benchkram/bob@v0.0.0-20220321080157-7c8f3876e225/cli/cmd_git.go (about) 1 package cli 2 3 import ( 4 "errors" 5 "fmt" 6 "os" 7 8 "github.com/logrusorgru/aurora" 9 "github.com/spf13/cobra" 10 11 "github.com/Benchkram/bob/bobgit" 12 "github.com/Benchkram/bob/pkg/boblog" 13 "github.com/Benchkram/bob/pkg/bobutil" 14 "github.com/Benchkram/bob/pkg/usererror" 15 "github.com/Benchkram/errz" 16 ) 17 18 var CmdGit = &cobra.Command{ 19 Use: "git", 20 Short: "Run git cmds on all child repos", 21 Long: ``, 22 Run: func(cmd *cobra.Command, args []string) { 23 err := cmd.Help() 24 errz.Fatal(err) 25 }, 26 } 27 28 var CmdGitAdd = &cobra.Command{ 29 Use: "add", 30 Short: "Run git add on all child repos", 31 Long: ``, 32 Run: func(cmd *cobra.Command, args []string) { 33 runGitAdd(args...) 34 }, 35 } 36 37 var CmdGitCommit = &cobra.Command{ 38 Use: "commit", 39 Short: "Run git commit on all child repos using the given message", 40 Long: ``, 41 Run: func(cmd *cobra.Command, args []string) { 42 message, _ := cmd.Flags().GetString("message") 43 runGitCommit(message) 44 }, 45 } 46 47 var CmdGitStatus = &cobra.Command{ 48 Use: "status", 49 Short: "Run git status on all child repos", 50 Long: ``, 51 Run: func(cmd *cobra.Command, args []string) { 52 runGitStatus() 53 }, 54 } 55 56 func runGitAdd(targets ...string) { 57 err := bobgit.Add(targets...) 58 if err != nil { 59 if errors.As(err, &usererror.Err) { 60 boblog.Log.UserError(err) 61 os.Exit(1) 62 } else { 63 errz.Fatal(err) 64 } 65 } 66 } 67 68 func runGitCommit(m string) { 69 s, err := bobgit.Commit(m) 70 if err != nil { 71 if errors.As(err, &usererror.Err) { 72 boblog.Log.UserError(err) 73 os.Exit(1) 74 } else if errors.Is(err, bobgit.ErrEmptyCommitMessage) { 75 fmt.Printf("%s\n\n %s\n\n", "bob git requires a commit message", aurora.Bold("bob git commit -m \"msg\"")) 76 os.Exit(1) 77 } else { 78 errz.Fatal(err) 79 } 80 } 81 82 if s != "" { 83 fmt.Println(s) 84 } 85 } 86 87 func runGitStatus() { 88 s, err := bobgit.Status() 89 if err != nil { 90 if errors.Is(err, bobutil.ErrCouldNotFindBobWorkspace) { 91 fmt.Println("fatal: not a bob repository (or any of the parent directories): .bob") 92 os.Exit(1) 93 } 94 if errors.Is(err, bobgit.ErrCouldNotFindGitDir) { 95 fmt.Println("fatal: bob workspace is not a git repository") 96 os.Exit(1) 97 } 98 errz.Fatal(err) 99 } 100 fmt.Println(s.String()) 101 }