Skip to content

Commit bfcc111

Browse files
Merge pull request #21 from augmentable-dev/cmds-cleanup
clean up error handling, abstract a validation, add a cmd desc phrase
2 parents a3ffe40 + c1295b9 commit bfcc111

File tree

4 files changed

+35
-8
lines changed

4 files changed

+35
-8
lines changed

cmd/commands/helpers.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
)
8+
9+
func validateDir(dir string) {
10+
if dir == "" {
11+
cwd, err := os.Getwd()
12+
handleError(err)
13+
dir = cwd
14+
}
15+
16+
abs, err := filepath.Abs(filepath.Join(dir, ".git"))
17+
handleError(err)
18+
19+
if _, err := os.Stat(abs); os.IsNotExist(err) {
20+
handleError(fmt.Errorf("%s is not a git repository", abs))
21+
}
22+
}

cmd/commands/root.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ var rootCmd = &cobra.Command{
1313
Long: `tickgit is a tool for helping you manage tickets and todos in your codebase, as a part of your git history`,
1414
}
1515

16+
// TODO clean this up
17+
func handleError(err error) {
18+
if err != nil {
19+
fmt.Println(err)
20+
os.Exit(1)
21+
}
22+
}
23+
1624
// Execute adds all child commands to the root command and sets flags appropriately.
1725
func Execute() {
1826
if err := rootCmd.Execute(); err != nil {

cmd/commands/status.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,6 @@ func init() {
1313
rootCmd.AddCommand(statusCmd)
1414
}
1515

16-
// TODO clean this up
17-
func handleError(err error) {
18-
if err != nil {
19-
panic(err)
20-
}
21-
}
22-
2316
var statusCmd = &cobra.Command{
2417
Use: "status",
2518
Short: "Print a status report of the current directory",
@@ -35,6 +28,8 @@ var statusCmd = &cobra.Command{
3528
handleError(err)
3629
}
3730

31+
validateDir(dir)
32+
3833
r, err := git.PlainOpen(dir)
3934
handleError(err)
4035

cmd/commands/todos.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func init() {
1717
var todosCmd = &cobra.Command{
1818
Use: "todos",
1919
Short: "Print a report of current TODOs",
20-
Long: ``,
20+
Long: `Scans a given git repository looking for any code comments with TODOs. Displays a report of all the TODO items found.`,
2121
Args: cobra.MaximumNArgs(1),
2222
Run: func(cmd *cobra.Command, args []string) {
2323
cwd, err := os.Getwd()
@@ -29,6 +29,8 @@ var todosCmd = &cobra.Command{
2929
handleError(err)
3030
}
3131

32+
validateDir(dir)
33+
3234
r, err := git.PlainOpen(dir)
3335
handleError(err)
3436

0 commit comments

Comments
 (0)