Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 080c986

Browse files
authored
Merge pull request #53 from mnottale/cmd-use-rune
cmd: Use cobra's RunE to simplify code.
2 parents 70cde29 + e2c91b4 commit 080c986

File tree

12 files changed

+26
-95
lines changed

12 files changed

+26
-95
lines changed

cmd/helm.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var helmCmd = &cobra.Command{
1414
Use: "helm <app-name> [-c <compose-files>...] [-e key=value...] [-f settings-file...]",
1515
Short: "Render the Compose file for this app as an Helm package",
1616
Args: cobra.MaximumNArgs(1),
17-
Run: func(cmd *cobra.Command, args []string) {
17+
RunE: func(cmd *cobra.Command, args []string) error {
1818
d := make(map[string]string)
1919
for _, v := range helmEnv {
2020
kv := strings.SplitN(v, "=", 2)
@@ -32,11 +32,7 @@ var helmCmd = &cobra.Command{
3232
if len(args) > 0 {
3333
app = args[0]
3434
}
35-
err := renderer.Helm(app, helmComposeFiles, helmSettingsFile, d)
36-
if err != nil {
37-
fmt.Printf("%v\n", err)
38-
os.Exit(1)
39-
}
35+
return renderer.Helm(app, helmComposeFiles, helmSettingsFile, d)
4036
},
4137
}
4238

cmd/image-add.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var imageAddCmd = &cobra.Command{
1414
Use: "image-add <app-name> [services...]",
1515
Short: "Add images for given services (default: all) to the app package",
1616
Args: cobra.MinimumNArgs(1),
17-
Run: func(cmd *cobra.Command, args []string) {
17+
RunE: func(cmd *cobra.Command, args []string) error {
1818
d := make(map[string]string)
1919
for _, v := range imageAddEnv {
2020
kv := strings.SplitN(v, "=", 2)
@@ -24,11 +24,7 @@ var imageAddCmd = &cobra.Command{
2424
}
2525
d[kv[0]] = kv[1]
2626
}
27-
err := image.Add(args[0], args[1:], imageAddComposeFiles, imageAddSettingsFile, d)
28-
if err != nil {
29-
fmt.Printf("%v\n", err)
30-
os.Exit(1)
31-
}
27+
return image.Add(args[0], args[1:], imageAddComposeFiles, imageAddSettingsFile, d)
3228
},
3329
}
3430
var imageAddComposeFiles []string

cmd/image-load.go

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

33
import (
4-
"fmt"
5-
"os"
6-
74
"github.com/docker/lunchbox/image"
85
"github.com/docker/lunchbox/internal"
96
"github.com/spf13/cobra"
@@ -13,12 +10,8 @@ var imageLoadCmd = &cobra.Command{
1310
Use: "image-load <app-name> [services...]",
1411
Short: "Load stored images for given services (default: all) to the local docker daemon",
1512
Args: cobra.MinimumNArgs(1),
16-
Run: func(cmd *cobra.Command, args []string) {
17-
err := image.Load(args[0], args[1:])
18-
if err != nil {
19-
fmt.Printf("%v\n", err)
20-
os.Exit(1)
21-
}
13+
RunE: func(cmd *cobra.Command, args []string) error {
14+
return image.Load(args[0], args[1:])
2215
},
2316
}
2417

cmd/init.go

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

33
import (
44
"fmt"
5-
"os"
65

76
"github.com/docker/lunchbox/packager"
87
"github.com/spf13/cobra"
@@ -13,12 +12,9 @@ var initCmd = &cobra.Command{
1312
Use: "init <app-name> [-c <compose-file>]",
1413
Short: "Initialize an app package in the current working directory",
1514
Args: cobra.ExactArgs(1),
16-
Run: func(cmd *cobra.Command, args []string) {
15+
RunE: func(cmd *cobra.Command, args []string) error {
1716
fmt.Println("init called")
18-
if err := packager.Init(args[0], composeFile); err != nil {
19-
fmt.Printf("%v\n", err)
20-
os.Exit(1)
21-
}
17+
return packager.Init(args[0], composeFile)
2218
},
2319
}
2420

cmd/inspect.go

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

33
import (
4-
"fmt"
5-
"os"
6-
74
"github.com/docker/lunchbox/packager"
85
"github.com/spf13/cobra"
96
)
@@ -13,16 +10,12 @@ var inspectCmd = &cobra.Command{
1310
Use: "inspect <app-name>",
1411
Short: "Retrieve metadata for a given app package",
1512
Args: cobra.MaximumNArgs(1),
16-
Run: func(cmd *cobra.Command, args []string) {
13+
RunE: func(cmd *cobra.Command, args []string) error {
1714
app := ""
1815
if len(args) > 0 {
1916
app = args[0]
2017
}
21-
err := packager.Inspect(app)
22-
if err != nil {
23-
fmt.Printf("%v\n", err)
24-
os.Exit(1)
25-
}
18+
return packager.Inspect(app)
2619
},
2720
}
2821

cmd/load.go

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

33
import (
4-
"fmt"
5-
"os"
6-
74
"github.com/docker/lunchbox/internal"
85
"github.com/docker/lunchbox/packager"
96
"github.com/spf13/cobra"
@@ -13,16 +10,12 @@ var loadCmd = &cobra.Command{
1310
Use: "load <repotag>",
1411
Short: "Load an app from docker",
1512
Args: cobra.MaximumNArgs(1),
16-
Run: func(cmd *cobra.Command, args []string) {
13+
RunE: func(cmd *cobra.Command, args []string) error {
1714
app := ""
1815
if len(args) > 0 {
1916
app = args[0]
2017
}
21-
err := packager.Load(app)
22-
if err != nil {
23-
fmt.Printf("%v\n", err)
24-
os.Exit(1)
25-
}
18+
return packager.Load(app)
2619
},
2720
}
2821

cmd/pack.go

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

33
import (
4-
"fmt"
5-
"os"
6-
74
"github.com/docker/lunchbox/internal"
85
"github.com/docker/lunchbox/packager"
96
"github.com/spf13/cobra"
@@ -13,16 +10,12 @@ var packCmd = &cobra.Command{
1310
Use: "pack <app-name> [-o output_file]",
1411
Short: "Pack this app as a single file",
1512
Args: cobra.MaximumNArgs(1),
16-
Run: func(cmd *cobra.Command, args []string) {
13+
RunE: func(cmd *cobra.Command, args []string) error {
1714
app := ""
1815
if len(args) > 0 {
1916
app = args[0]
2017
}
21-
err := packager.Pack(app, packOutputFile)
22-
if err != nil {
23-
fmt.Printf("%v\n", err)
24-
os.Exit(1)
25-
}
18+
return packager.Pack(app, packOutputFile)
2619
},
2720
}
2821

cmd/pull.go

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

33
import (
4-
"fmt"
5-
"os"
6-
74
"github.com/docker/lunchbox/internal"
85
"github.com/docker/lunchbox/packager"
96
"github.com/spf13/cobra"
@@ -13,12 +10,8 @@ var pullCmd = &cobra.Command{
1310
Use: "pull <repotag>",
1411
Short: "Pull an app from a registry",
1512
Args: cobra.ExactArgs(1),
16-
Run: func(cmd *cobra.Command, args []string) {
17-
err := packager.Pull(args[0])
18-
if err != nil {
19-
fmt.Printf("%v\n", err)
20-
os.Exit(1)
21-
}
13+
RunE: func(cmd *cobra.Command, args []string) error {
14+
return packager.Pull(args[0])
2215
},
2316
}
2417

cmd/push.go

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

33
import (
4-
"fmt"
5-
"os"
6-
74
"github.com/docker/lunchbox/internal"
85
"github.com/docker/lunchbox/packager"
96
"github.com/spf13/cobra"
@@ -13,19 +10,15 @@ var pushCmd = &cobra.Command{
1310
Use: "push <app-name>",
1411
Short: "Push the application to a registry",
1512
Args: cobra.MaximumNArgs(1),
16-
Run: func(cmd *cobra.Command, args []string) {
13+
RunE: func(cmd *cobra.Command, args []string) error {
1714
if pushTag == "" {
1815
pushTag = "latest"
1916
}
2017
app := ""
2118
if len(args) > 0 {
2219
app = args[0]
2320
}
24-
err := packager.Push(app, pushPrefix, pushTag)
25-
if err != nil {
26-
fmt.Printf("%v\n", err)
27-
os.Exit(1)
28-
}
21+
return packager.Push(app, pushPrefix, pushTag)
2922
},
3023
}
3124

cmd/render.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Override is provided in different ways:
2222
-s flag. These value takes precedence over all settings files.
2323
`,
2424
Args: cobra.MaximumNArgs(1),
25-
Run: func(cmd *cobra.Command, args []string) {
25+
RunE: func(cmd *cobra.Command, args []string) error {
2626
d := make(map[string]string)
2727
for _, v := range renderEnv {
2828
kv := strings.SplitN(v, "=", 2)
@@ -42,15 +42,14 @@ Override is provided in different ways:
4242
}
4343
rendered, err := renderer.Render(app, renderComposeFiles, renderSettingsFile, d)
4444
if err != nil {
45-
fmt.Printf("%v\n", err)
46-
os.Exit(1)
45+
return err
4746
}
4847
res, err := yaml.Marshal(rendered)
4948
if err != nil {
50-
fmt.Printf("%v\n", err)
51-
os.Exit(1)
49+
return err
5250
}
5351
fmt.Printf("%s", string(res))
52+
return nil
5453
},
5554
}
5655

0 commit comments

Comments
 (0)