Skip to content

Commit eb8831f

Browse files
refactor: standardize command layer to use RunE pattern with centralized error handling
- Convert deploy, docker, k8s, env commands to use RunE for proper exit codes - Add centralized cmd/errors.go with LogError, LogSuccess, LogInfo helpers - Standardize error formatting and messaging across CLI - Remove unnecessary fmt imports from refactored commands - Rename cmd/doctor.go to cmd/docker.go for clarity - Improves CLI scriptability and error handling consistency
1 parent 48665de commit eb8831f

9 files changed

Lines changed: 92 additions & 91 deletions

File tree

cmd/deploy.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"fmt"
54

65
"github.com/aryansharma9917/codewise-cli/pkg/deploy"
76
"github.com/spf13/cobra"
@@ -20,32 +19,22 @@ var deployCmd = &cobra.Command{
2019
var deployRunCmd = &cobra.Command{
2120
Use: "run",
2221
Short: "Execute deployment",
23-
Run: func(cmd *cobra.Command, args []string) {
24-
22+
RunE: func(cmd *cobra.Command, args []string) error {
2523
if deployEnv == "" {
26-
fmt.Println("please provide --env")
27-
return
28-
}
29-
30-
if err := deploy.Run(deployEnv, dryRun); err != nil {
31-
fmt.Println("deploy error:", err)
24+
return ExitError("please provide --env")
3225
}
26+
return deploy.Run(deployEnv, dryRun)
3327
},
3428
}
3529

3630
var deployPlanCmd = &cobra.Command{
3731
Use: "plan",
3832
Short: "Preview deployment execution plan",
39-
Run: func(cmd *cobra.Command, args []string) {
40-
33+
RunE: func(cmd *cobra.Command, args []string) error {
4134
if deployEnv == "" {
42-
fmt.Println("please provide --env")
43-
return
44-
}
45-
46-
if err := deploy.Plan(deployEnv); err != nil {
47-
fmt.Println("plan error:", err)
35+
return ExitError("please provide --env")
4836
}
37+
return deploy.Plan(deployEnv)
4938
},
5039
}
5140

cmd/doctor.go renamed to cmd/docker.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"fmt"
54

65
"github.com/aryansharma9917/codewise-cli/pkg/docker"
76
"github.com/spf13/cobra"
@@ -17,32 +16,35 @@ var dockerCmd = &cobra.Command{
1716
var dockerInitCmd = &cobra.Command{
1817
Use: "init",
1918
Short: "Generate a Dockerfile",
20-
Run: func(cmd *cobra.Command, args []string) {
19+
RunE: func(cmd *cobra.Command, args []string) error {
2120
if err := docker.InitDockerfile(); err != nil {
22-
fmt.Println("ℹ️", err.Error())
23-
return
21+
return LogError(err.Error())
2422
}
25-
fmt.Println("✅ Dockerfile created")
23+
LogSuccess("Dockerfile created")
24+
return nil
2625
},
2726
}
2827

2928
var dockerValidateCmd = &cobra.Command{
3029
Use: "validate",
3130
Short: "Validate Dockerfile best practices",
32-
Run: func(cmd *cobra.Command, args []string) {
31+
RunE: func(cmd *cobra.Command, args []string) error {
3332
if err := docker.ValidateDockerfile(); err != nil {
34-
fmt.Println("❌", err.Error())
33+
return LogError(err.Error())
3534
}
35+
return nil
3636
},
3737
}
3838

3939
var dockerBuildCmd = &cobra.Command{
4040
Use: "build",
4141
Short: "Build Docker image",
42-
Run: func(cmd *cobra.Command, args []string) {
42+
RunE: func(cmd *cobra.Command, args []string) error {
4343
if err := docker.BuildDockerImage(imageTag); err != nil {
44-
fmt.Println("❌ Docker build failed")
44+
return LogError("Docker build failed: %v", err)
4545
}
46+
LogSuccess("Docker image built successfully")
47+
return nil
4648
},
4749
}
4850

cmd/env_create.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,22 @@ var envCreateCmd = &cobra.Command{
1515
Use: "create <name>",
1616
Short: "Create a new environment",
1717
Args: cobra.ExactArgs(1),
18-
Run: func(cmd *cobra.Command, args []string) {
18+
RunE: func(cmd *cobra.Command, args []string) error {
1919
name := args[0]
2020

2121
if interactive {
2222
if err := createEnvInteractive(name); err != nil {
23-
fmt.Println("error:", err)
24-
return
23+
return LogError("error: %v", err)
2524
}
26-
fmt.Println("environment", name, "created")
27-
return
25+
LogSuccess("environment %s created", name)
26+
return nil
2827
}
2928

3029
if err := env.CreateEnv(name, env.CreateOptions{}); err != nil {
31-
fmt.Println("error:", err)
32-
return
30+
return LogError("error: %v", err)
3331
}
34-
fmt.Println("environment", name, "created")
32+
LogSuccess("environment %s created", name)
33+
return nil
3534
},
3635
}
3736

cmd/env_delete.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var envDeleteCmd = &cobra.Command{
1414
Use: "delete <name>",
1515
Short: "Delete an environment",
1616
Args: cobra.ExactArgs(1),
17-
Run: func(cmd *cobra.Command, args []string) {
17+
RunE: func(cmd *cobra.Command, args []string) error {
1818
name := args[0]
1919

2020
if !yes {
@@ -23,21 +23,19 @@ var envDeleteCmd = &cobra.Command{
2323
Message: fmt.Sprintf("Delete environment %q?", name),
2424
}
2525
if err := survey.AskOne(prompt, &confirm); err != nil {
26-
fmt.Println("error:", err)
27-
return
26+
return LogError("error: %v", err)
2827
}
2928
if !confirm {
30-
fmt.Println("aborted")
31-
return
29+
return LogError("aborted")
3230
}
3331
}
3432

3533
if err := env.DeleteEnv(name, env.DeleteOptions{Force: yes}); err != nil {
36-
fmt.Println("error:", err)
37-
return
34+
return LogError("error: %v", err)
3835
}
3936

40-
fmt.Println("environment", name, "deleted")
37+
LogSuccess("environment %s deleted", name)
38+
return nil
4139
},
4240
}
4341

cmd/env_list.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
package cmd
22

33
import (
4-
"fmt"
4+
"fmt"
55

6-
"github.com/aryansharma9917/codewise-cli/pkg/env"
7-
"github.com/spf13/cobra"
6+
"github.com/aryansharma9917/codewise-cli/pkg/env"
7+
"github.com/spf13/cobra"
88
)
99

1010
var envListCmd = &cobra.Command{
11-
Use: "list",
12-
Short: "List environments",
13-
Run: func(cmd *cobra.Command, args []string) {
14-
envs, err := env.ListEnvs()
15-
if err != nil {
16-
fmt.Println("error:", err)
17-
return
18-
}
11+
Use: "list",
12+
Short: "List environments",
13+
RunE: func(cmd *cobra.Command, args []string) error {
14+
envs, err := env.ListEnvs()
15+
if err != nil {
16+
return LogError(err.Error())
17+
}
1918

20-
if len(envs) == 0 {
21-
fmt.Println("no environments found")
22-
return
23-
}
19+
if len(envs) == 0 {
20+
fmt.Println("no environments found")
21+
return nil
22+
}
2423

25-
for _, e := range envs {
26-
fmt.Printf("%-10s namespace=%s context=%s\n",
27-
e.Name, e.K8s.Namespace, e.K8s.Context)
28-
}
29-
},
24+
for _, e := range envs {
25+
fmt.Printf("%-10s namespace=%s context=%s\n",
26+
e.Name, e.K8s.Namespace, e.K8s.Context)
27+
}
28+
return nil
29+
},
3030
}
3131

3232
func init() {
33-
envCmd.AddCommand(envListCmd)
33+
envCmd.AddCommand(envListCmd)
3434
}

cmd/errors.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
)
7+
8+
// ExitError prints an error message and exits with status code 1.
9+
func ExitError(format string, args ...interface{}) error {
10+
fmt.Fprintf(os.Stderr, "error: %s\n", fmt.Sprintf(format, args...))
11+
return fmt.Errorf(format, args...)
12+
}
13+
14+
// LogError prints an info/warning message but does not exit.
15+
func LogError(format string, args ...interface{}) error {
16+
msg := fmt.Sprintf(format, args...)
17+
fmt.Fprintf(os.Stderr, "info: %s\n", msg)
18+
return fmt.Errorf(msg)
19+
}
20+
21+
// LogInfo prints an info message to stderr.
22+
func LogInfo(format string, args ...interface{}) {
23+
fmt.Fprintf(os.Stderr, "info: %s\n", fmt.Sprintf(format, args...))
24+
}
25+
26+
// LogSuccess prints a success message to stdout.
27+
func LogSuccess(format string, args ...interface{}) {
28+
fmt.Printf("✅ %s\n", fmt.Sprintf(format, args...))
29+
}

cmd/k8s_apply.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"fmt"
54

65
"github.com/aryansharma9917/codewise-cli/pkg/k8s"
76
"github.com/spf13/cobra"
@@ -16,23 +15,16 @@ var (
1615
var k8sApplyCmd = &cobra.Command{
1716
Use: "apply",
1817
Short: "Apply Kubernetes manifests to the current cluster",
19-
Run: func(cmd *cobra.Command, args []string) {
20-
// If not a dry run, check cluster connectivity
18+
RunE: func(cmd *cobra.Command, args []string) error {
2119
if !k8sDryRun {
2220
if err := k8s.CheckKubectl(); err != nil {
23-
fmt.Println("info:", err.Error())
24-
return
21+
return LogError(err.Error())
2522
}
2623
if err := k8s.CheckCluster(); err != nil {
27-
fmt.Println("info:", err.Error())
28-
return
24+
return LogError(err.Error())
2925
}
3026
}
31-
32-
if err := k8s.ApplyManifests(k8sNamespace, k8sContext, k8sDryRun); err != nil {
33-
fmt.Println("info:", err.Error())
34-
return
35-
}
27+
return k8s.ApplyManifests(k8sNamespace, k8sContext, k8sDryRun)
3628
},
3729
}
3830

cmd/k8s_delete.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"fmt"
54

65
"github.com/aryansharma9917/codewise-cli/pkg/k8s"
76
"github.com/spf13/cobra"
@@ -16,22 +15,16 @@ var (
1615
var k8sDeleteCmd = &cobra.Command{
1716
Use: "delete",
1817
Short: "Delete Kubernetes resources from the current cluster",
19-
Run: func(cmd *cobra.Command, args []string) {
18+
RunE: func(cmd *cobra.Command, args []string) error {
2019
if !k8sDeleteDryRun {
2120
if err := k8s.CheckKubectl(); err != nil {
22-
fmt.Println("info:", err.Error())
23-
return
21+
return LogError(err.Error())
2422
}
2523
if err := k8s.CheckCluster(); err != nil {
26-
fmt.Println("info:", err.Error())
27-
return
24+
return LogError(err.Error())
2825
}
2926
}
30-
31-
if err := k8s.DeleteManifests(k8sDeleteNamespace, k8sDeleteContext, k8sDeleteDryRun); err != nil {
32-
fmt.Println("info:", err.Error())
33-
return
34-
}
27+
return k8s.DeleteManifests(k8sDeleteNamespace, k8sDeleteContext, k8sDeleteDryRun)
3528
},
3629
}
3730

cmd/k8s_init.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"fmt"
54

65
"github.com/aryansharma9917/codewise-cli/pkg/k8s"
76
"github.com/spf13/cobra"
@@ -15,18 +14,18 @@ var (
1514
var k8sInitCmd = &cobra.Command{
1615
Use: "init",
1716
Short: "Initialize Kubernetes deployment and service manifests",
18-
Run: func(cmd *cobra.Command, args []string) {
17+
RunE: func(cmd *cobra.Command, args []string) error {
1918
opts := k8s.Options{
2019
AppName: k8sInitAppName,
2120
Image: k8sInitImage,
2221
}
2322

2423
if err := k8s.InitK8sManifests(opts); err != nil {
25-
fmt.Println("info:", err.Error())
26-
return
24+
return LogError(err.Error())
2725
}
2826

29-
fmt.Println("k8s manifests created at k8s/app")
27+
LogSuccess("k8s manifests created at k8s/app")
28+
return nil
3029
},
3130
}
3231

0 commit comments

Comments
 (0)