Skip to content

Commit

Permalink
♻️ improved error handling for the CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
xarunoba committed Jun 1, 2024
1 parent 2c85098 commit 383f6d9
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 42 deletions.
20 changes: 14 additions & 6 deletions internal/cli/add.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package cli

import (
"log"

"github.com/spf13/cobra"
"github.com/xarunoba/ccoco/pkg/ccoco"
)

func init() {
Expand All @@ -13,14 +12,23 @@ func init() {
var addCmd = &cobra.Command{
Use: "add [file1 file2 ...]",
Aliases: []string{"a"},
Short: "Add file to config",
Short: "Add file/s to config",
Long: `Adds file/s to config.
This will add file/s to the ccoco.config.json for ccoco to generate.
This will add file/s to the ` + app.ConfigFile().Name + ` for ccoco to generate.
`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
PreRunE: func(cmd *cobra.Command, args []string) error {
instance, err := ccoco.New()
if err != nil {
return err
}
app = instance
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if err := app.AddToFiles(args); err != nil {
log.Fatalf("Error adding files: %v", err)
return err
}
return nil
},
}
16 changes: 12 additions & 4 deletions internal/cli/generate.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package cli

import (
"log"

"github.com/spf13/cobra"
"github.com/xarunoba/ccoco/pkg/ccoco"
)

func init() {
Expand All @@ -18,9 +17,18 @@ var generateCmd = &cobra.Command{
This will populate the branch configs folder based on the existing branches.
`,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
PreRunE: func(cmd *cobra.Command, args []string) error {
instance, err := ccoco.New()
if err != nil {
return err
}
app = instance
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if err := app.GenerateConfigs(); err != nil {
log.Fatalf("Error generating configs: %v", err)
return err
}
return nil
},
}
15 changes: 11 additions & 4 deletions internal/cli/githook.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cli

import (
"log"

"github.com/spf13/cobra"
"github.com/xarunoba/ccoco/pkg/ccoco"
)
Expand All @@ -19,11 +17,20 @@ var githookCmd = &cobra.Command{
Long: `Injects ccoco to git hooks without depending on a git hook manager.
This will add a post-checkout hook to automatically change config on checkout.
`,
Run: func(cmd *cobra.Command, args []string) {
PreRunE: func(cmd *cobra.Command, args []string) error {
instance, err := ccoco.New()
if err != nil {
return err
}
app = instance
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if err := app.AddToGitHooks(ccoco.AddToGitHooksOptions{
SkipExecution: skipGitHookExecute,
}); err != nil {
log.Fatalf("Error adding to git hooks: %v", err)
return err
}
return nil
},
}
19 changes: 13 additions & 6 deletions internal/cli/init.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cli

import (
"log"

"github.com/spf13/cobra"
"github.com/xarunoba/ccoco/pkg/ccoco"
)
Expand All @@ -17,17 +15,26 @@ func init() {
var initCmd = &cobra.Command{
Use: "init",
Aliases: []string{"i"},
Short: "Initialize config file",
Long: `Initialize config file.`,
Run: func(cmd *cobra.Command, args []string) {
Short: "Initialize ccoco",
Long: `Initialize ccoco in the current git repository.`,
PreRunE: func(cmd *cobra.Command, args []string) error {
instance, err := ccoco.New()
if err != nil {
return err
}
app = instance
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if err := app.Init(ccoco.InitOptions{
AddToGitIgnore: addToGitIgnore,
AddToGitHooks: injectCcocoToGitHooks,
AddToGitHooksOptions: ccoco.AddToGitHooksOptions{
SkipExecution: skipGitHookExecute,
},
}); err != nil {
log.Fatalf("Error initializing ccoco: %v", err)
return err
}
return nil
},
}
23 changes: 16 additions & 7 deletions internal/cli/remove.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package cli

import (
"log"

"github.com/spf13/cobra"
"github.com/xarunoba/ccoco/pkg/ccoco"
)

func init() {
Expand All @@ -13,12 +12,22 @@ func init() {
var removeCmd = &cobra.Command{
Use: "remove",
Aliases: []string{"rm"},
Short: "Remove config files",
Long: `Remove config files`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
Short: "Remove files to config",
Long: `Remove files to config.
This will remove file/s from the ` + app.ConfigFile().Name + ` for ccoco to generate.`,
Args: cobra.MinimumNArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
instance, err := ccoco.New()
if err != nil {
return err
}
app = instance
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if err := app.RemoveFromFiles(args); err != nil {
log.Fatalf("Error removing files: %v", err)
return err
}
return nil
},
}
14 changes: 3 additions & 11 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,17 @@ import (

var app *ccoco.Ccoco

func init() {
instance, err := ccoco.New()
if err != nil {
log.Fatalf("Error initializing ccoco: %v", err)
}

app = instance
}

var cli = &cobra.Command{
Use: filepath.Base(os.Args[0]),
Short: "Change config on checkout",
Long: `ccoco changes your config files based on your current branch.
Integrate with git hooks to automatically change config on checkout.`,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
err := cmd.Help()
if err != nil {
log.Fatalf("Error executing ccoco: %v", err)
return err
}
return nil
},
}

Expand Down
15 changes: 11 additions & 4 deletions internal/cli/run.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cli

import (
"log"

"github.com/spf13/cobra"
"github.com/xarunoba/ccoco/pkg/ccoco"
)
Expand All @@ -17,9 +15,18 @@ var runCmd = &cobra.Command{
Short: "Run ccoco",
Long: `Run ccoco.
This will change config files based on your current branch.`,
Run: func(cmd *cobra.Command, args []string) {
PreRunE: func(cmd *cobra.Command, args []string) error {
instance, err := ccoco.New()
if err != nil {
return err
}
app = instance
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if err := app.Run(ccoco.RunOptions{}); err != nil {
log.Fatalf("Error running ccoco: %v", err)
return err
}
return nil
},
}

0 comments on commit 383f6d9

Please sign in to comment.