github.com/docker/app@v0.9.1-beta3.0.20210611140623-a48f773ab002/cmd/cnab-run/main.go (about)

     1  package main
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"math/rand"
     7  	"os"
     8  	"time"
     9  
    10  	"github.com/docker/app/internal"
    11  )
    12  
    13  type cnabAction func(string) error
    14  
    15  var (
    16  	cnabActions = map[string]cnabAction{
    17  		"install":                           installAction,
    18  		"upgrade":                           installAction, // upgrade is implemented as reinstall.
    19  		"uninstall":                         uninstallAction,
    20  		internal.ActionStatusNameDeprecated: statusAction,
    21  		internal.ActionStatusName:           statusAction,
    22  		internal.ActionStatusJSONName:       statusJSONAction,
    23  		internal.ActionInspectName:          inspectAction,
    24  		internal.ActionRenderName:           renderAction,
    25  	}
    26  )
    27  
    28  func getCnabAction() (cnabAction, string, error) {
    29  	// CNAB_ACTION should always be set. but in future we want to have
    30  	// claim-less actions. So we don't fail if no installation is set
    31  	actionName, ok := os.LookupEnv("CNAB_ACTION")
    32  	if !ok {
    33  		return nil, "", errors.New("no CNAB action specified")
    34  	}
    35  	action, ok := cnabActions[actionName]
    36  	if !ok {
    37  		return nil, "", fmt.Errorf("action %q not supported", actionName)
    38  	}
    39  	return action, actionName, nil
    40  }
    41  
    42  func main() {
    43  	rand.Seed(time.Now().UnixNano())
    44  	action, actionName, err := getCnabAction()
    45  	if err != nil {
    46  		fmt.Fprintf(os.Stderr, "Error while parsing CNAB operation: %s", err)
    47  		os.Exit(1)
    48  	}
    49  	instanceName := os.Getenv("CNAB_INSTALLATION_NAME")
    50  	if err := action(instanceName); err != nil {
    51  		fmt.Fprintf(os.Stderr, "Action %q failed: %s", actionName, err)
    52  		os.Exit(1)
    53  	}
    54  }