github.com/bhameyie/otto@v0.2.1-0.20160406174117-16052efa52ec/command/deploy.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/hashicorp/otto/helper/flag"
     8  )
     9  
    10  // DeployCommand is the command that deploys the app once it is built.
    11  type DeployCommand struct {
    12  	Meta
    13  }
    14  
    15  func (c *DeployCommand) Run(args []string) int {
    16  	fs := c.FlagSet("deploy", FlagSetNone)
    17  	fs.Usage = func() { c.Ui.Error(c.Help()) }
    18  	args, execArgs, posArgs := flag.FilterArgs(fs, args)
    19  	if err := fs.Parse(args); err != nil {
    20  		return 1
    21  	}
    22  
    23  	// Get the remaining args to determine if we have an action.
    24  	var action string
    25  	if len(posArgs) > 0 {
    26  		action = posArgs[0]
    27  		execArgs = append(execArgs, posArgs[1:]...)
    28  	}
    29  
    30  	// Load the appfile
    31  	app, err := c.Appfile()
    32  	if err != nil {
    33  		c.Ui.Error(err.Error())
    34  		return 1
    35  	}
    36  
    37  	// Get a core
    38  	core, err := c.Core(app)
    39  	if err != nil {
    40  		c.Ui.Error(fmt.Sprintf(
    41  			"Error loading core: %s", err))
    42  		return 1
    43  	}
    44  
    45  	// Destroy action gets an extra double-check
    46  	if action == "destroy" {
    47  		msg := "Otto will delete all resources associated with the deploy."
    48  		if !c.confirmDestroy(msg, execArgs) {
    49  			return 1
    50  		}
    51  	}
    52  
    53  	// Deploy the artifact
    54  	if err := core.Deploy(action, execArgs); err != nil {
    55  		// Display errors without prefix, we expect them to be formatted in a way
    56  		// that's suitable for UI.
    57  		c.Ui.Error(err.Error())
    58  		return 1
    59  	}
    60  
    61  	return 0
    62  }
    63  
    64  func (c *DeployCommand) Synopsis() string {
    65  	return "Deploy the application"
    66  }
    67  
    68  func (c *DeployCommand) Help() string {
    69  	helpText := `
    70  Usage: otto deploy [options]
    71  
    72    Deploy the application to the current environment.
    73  
    74    This command may modify real infrastructure, including adding
    75    and removing resources.
    76  
    77    The "build" command should be called prior to this to create the
    78    build artifact. Deploy can be called multiple times with the same
    79    artifact to redeploy an application.
    80  
    81  `
    82  
    83  	return strings.TrimSpace(helpText)
    84  }