@@ -8,76 +8,42 @@ 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 ,
20+ internal .Namespace + "render" : renderAction ,
3021 }
31- return cnabAction (action ), nil
32- }
22+ )
3323
34- func getCnabOperation () (cnabOperation , error ) {
24+ func getCnabAction () (cnabAction , string , error ) {
3525 // CNAB_ACTION should always be set. but in future we want to have
3626 // 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
27+ actionName , ok := os .LookupEnv ("CNAB_ACTION" )
28+ if ! ok {
29+ return nil , "" , errors .New ("no CNAB action specified" )
30+ }
31+ action , ok := cnabActions [actionName ]
32+ if ! ok {
33+ return nil , "" , fmt .Errorf ("action %q not supported" , actionName )
4034 }
41- return cnabOperation {
42- action : action ,
43- installation : os .Getenv ("CNAB_INSTALLATION_NAME" ),
44- }, nil
35+ return action , actionName , nil
4536}
4637
4738func main () {
48- op , err := getCnabOperation ()
39+ action , actionName , err := getCnabAction ()
4940 if err != nil {
5041 fmt .Fprintf (os .Stderr , "Error while parsing cnab operation: %s" , err )
5142 os .Exit (1 )
5243 }
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 )
44+ instanceName := os .Getenv ("CNAB_INSTALLATION_NAME" )
45+ if err := action (instanceName ); err != nil {
46+ fmt .Fprintf (os .Stderr , "Action %q failed: %s" , actionName , err )
8147 os .Exit (1 )
8248 }
8349}
0 commit comments