Skip to content

Commit

Permalink
clean up error handling, abstract a validation, add a cmd desc phrase
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickdevivo committed Nov 3, 2019
1 parent a3ffe40 commit c1295b9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
22 changes: 22 additions & 0 deletions cmd/commands/helpers.go
@@ -0,0 +1,22 @@
package commands

import (
"fmt"
"os"
"path/filepath"
)

func validateDir(dir string) {
if dir == "" {
cwd, err := os.Getwd()
handleError(err)
dir = cwd
}

abs, err := filepath.Abs(filepath.Join(dir, ".git"))
handleError(err)

if _, err := os.Stat(abs); os.IsNotExist(err) {
handleError(fmt.Errorf("%s is not a git repository", abs))
}
}
8 changes: 8 additions & 0 deletions cmd/commands/root.go
Expand Up @@ -13,6 +13,14 @@ var rootCmd = &cobra.Command{
Long: `tickgit is a tool for helping you manage tickets and todos in your codebase, as a part of your git history`,
}

// TODO clean this up
func handleError(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}

// Execute adds all child commands to the root command and sets flags appropriately.
func Execute() {
if err := rootCmd.Execute(); err != nil {
Expand Down
9 changes: 2 additions & 7 deletions cmd/commands/status.go
Expand Up @@ -13,13 +13,6 @@ func init() {
rootCmd.AddCommand(statusCmd)
}

// TODO clean this up
func handleError(err error) {
if err != nil {
panic(err)
}
}

var statusCmd = &cobra.Command{
Use: "status",
Short: "Print a status report of the current directory",
Expand All @@ -35,6 +28,8 @@ var statusCmd = &cobra.Command{
handleError(err)
}

validateDir(dir)

r, err := git.PlainOpen(dir)
handleError(err)

Expand Down
4 changes: 3 additions & 1 deletion cmd/commands/todos.go
Expand Up @@ -17,7 +17,7 @@ func init() {
var todosCmd = &cobra.Command{
Use: "todos",
Short: "Print a report of current TODOs",
Long: ``,
Long: `Scans a given git repository looking for any code comments with TODOs. Displays a report of all the TODO items found.`,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
cwd, err := os.Getwd()
Expand All @@ -29,6 +29,8 @@ var todosCmd = &cobra.Command{
handleError(err)
}

validateDir(dir)

r, err := git.PlainOpen(dir)
handleError(err)

Expand Down

0 comments on commit c1295b9

Please sign in to comment.