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

Commit 0010dc8

Browse files
Refactor and simplify cnab-run main.
Signed-off-by: Silvin Lubecki <silvin.lubecki@docker.com>
1 parent 867378b commit 0010dc8

5 files changed

Lines changed: 27 additions & 62 deletions

File tree

cmd/cnab-run/inspect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/docker/app/internal/packager"
88
)
99

10-
func inspect() error {
10+
func inspectAction(instanceName string) error {
1111
app, err := packager.Extract("")
1212
// todo: merge additional compose file
1313
if err != nil {

cmd/cnab-run/install.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const (
2222
imageMapFilePath = "/cnab/app/image-map.json"
2323
)
2424

25-
func install(instanceName string) error {
25+
func installAction(instanceName string) error {
2626
cli, err := setupDockerContext()
2727
if err != nil {
2828
return errors.Wrap(err, "unable to restore docker context")

cmd/cnab-run/main.go

Lines changed: 23 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -8,76 +8,41 @@ import (
88
"github.com/docker/app/internal"
99
)
1010

11-
type cnabAction string
12-
13-
const (
14-
cnabActionInstall = cnabAction("install")
15-
cnabActionUninstall = cnabAction("uninstall")
16-
cnabActionUpgrade = cnabAction("upgrade")
17-
cnabActionStatus = cnabAction(internal.Namespace + "status")
18-
cnabActionInspect = cnabAction(internal.Namespace + "inspect")
19-
)
20-
21-
type cnabOperation struct {
22-
action cnabAction
23-
installation string
24-
}
25-
26-
func getCnabAction() (cnabAction, error) {
27-
action, ok := os.LookupEnv("CNAB_ACTION")
28-
if !ok {
29-
return "", errors.New("no CNAB action specified")
11+
type cnabAction func(string) error
12+
13+
var (
14+
cnabActions = map[string]cnabAction{
15+
"install": installAction,
16+
"upgrade": installAction,
17+
"uninstall": uninstallAction,
18+
internal.Namespace + "status": statusAction,
19+
internal.Namespace + "inspect": inspectAction,
3020
}
31-
return cnabAction(action), nil
32-
}
21+
)
3322

34-
func getCnabOperation() (cnabOperation, error) {
23+
func getCnabAction() (cnabAction, string, error) {
3524
// CNAB_ACTION should always be set. but in future we want to have
3625
// claim-less actions. So we don't fail if no installation is set
37-
action, err := getCnabAction()
38-
if err != nil {
39-
return cnabOperation{}, err
26+
actionName, ok := os.LookupEnv("CNAB_ACTION")
27+
if !ok {
28+
return nil, "", errors.New("no CNAB action specified")
29+
}
30+
action, ok := cnabActions[actionName]
31+
if !ok {
32+
return nil, "", fmt.Errorf("action %q not supported", actionName)
4033
}
41-
return cnabOperation{
42-
action: action,
43-
installation: os.Getenv("CNAB_INSTALLATION_NAME"),
44-
}, nil
34+
return action, actionName, nil
4535
}
4636

4737
func main() {
48-
op, err := getCnabOperation()
38+
action, actionName, err := getCnabAction()
4939
if err != nil {
5040
fmt.Fprintf(os.Stderr, "Error while parsing cnab operation: %s", err)
5141
os.Exit(1)
5242
}
53-
switch op.action {
54-
case cnabActionInstall:
55-
if err := install(op.installation); err != nil {
56-
fmt.Fprintf(os.Stderr, "Install failed: %s", err)
57-
os.Exit(1)
58-
}
59-
case cnabActionUpgrade:
60-
if err := install(op.installation); err != nil {
61-
fmt.Fprintf(os.Stderr, "Upgrade failed: %s", err)
62-
os.Exit(1)
63-
}
64-
case cnabActionUninstall:
65-
if err := uninstall(op.installation); err != nil {
66-
fmt.Fprintf(os.Stderr, "Uninstall failed: %s", err)
67-
os.Exit(1)
68-
}
69-
case cnabActionStatus:
70-
if err := status(op.installation); err != nil {
71-
fmt.Fprintf(os.Stderr, "Status failed: %s", err)
72-
os.Exit(1)
73-
}
74-
case cnabActionInspect:
75-
if err := inspect(); err != nil {
76-
fmt.Fprintf(os.Stderr, "Inspect failed: %s", err)
77-
os.Exit(1)
78-
}
79-
default:
80-
fmt.Fprintf(os.Stderr, "Action %q is not supported", op.action)
43+
instanceName := os.Getenv("CNAB_INSTALLATION_NAME")
44+
if err := action(instanceName); err != nil {
45+
fmt.Fprintf(os.Stderr, "Action %q failed: %s", actionName, err)
8146
os.Exit(1)
8247
}
8348
}

cmd/cnab-run/status.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/pkg/errors"
1010
)
1111

12-
func status(instanceName string) error {
12+
func statusAction(instanceName string) error {
1313
cli, err := setupDockerContext()
1414
if err != nil {
1515
return errors.Wrap(err, "unable to restore docker context")

cmd/cnab-run/uninstall.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/pkg/errors"
99
)
1010

11-
func uninstall(instanceName string) error {
11+
func uninstallAction(instanceName string) error {
1212
cli, err := setupDockerContext()
1313
if err != nil {
1414
return errors.Wrap(err, "unable to restore docker context")

0 commit comments

Comments
 (0)