github.com/dahs81/otto@v0.2.1-0.20160126165905-6400716cf085/command/infra.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/hashicorp/otto/helper/flag"
     8  )
     9  
    10  // InfraCommand is the command that sets up the infrastructure for an
    11  // Appfile.
    12  type InfraCommand struct {
    13  	Meta
    14  }
    15  
    16  func (c *InfraCommand) Run(args []string) int {
    17  	fs := c.FlagSet("infra", FlagSetNone)
    18  	fs.Usage = func() { c.Ui.Error(c.Help()) }
    19  	args, execArgs, posArgs := flag.FilterArgs(fs, args)
    20  	if err := fs.Parse(args); err != nil {
    21  		return 1
    22  	}
    23  
    24  	// Get the remaining args to determine if we have an action.
    25  	var action string
    26  	if len(posArgs) > 0 {
    27  		action = posArgs[0]
    28  		execArgs = append(execArgs, posArgs[1:]...)
    29  	}
    30  
    31  	// Load the appfile
    32  	app, err := c.Appfile()
    33  	if err != nil {
    34  		c.Ui.Error(err.Error())
    35  		return 1
    36  	}
    37  
    38  	// Get a core
    39  	core, err := c.Core(app)
    40  	if err != nil {
    41  		c.Ui.Error(fmt.Sprintf(
    42  			"Error loading core: %s", err))
    43  		return 1
    44  	}
    45  
    46  	// Destroy action gets an extra double-check
    47  	if action == "destroy" {
    48  		msg := "Otto will delete all your managed infrastructure."
    49  		if !c.confirmDestroy(msg, execArgs) {
    50  			return 1
    51  		}
    52  	}
    53  
    54  	// Execute the task
    55  	err = core.Infra(action, execArgs)
    56  	if err != nil {
    57  		c.Ui.Error(fmt.Sprintf(
    58  			"Error occurred: %s", err))
    59  		return 1
    60  	}
    61  
    62  	return 0
    63  }
    64  
    65  func (c *InfraCommand) Synopsis() string {
    66  	return "Builds the infrastructure for the Appfile"
    67  }
    68  
    69  func (c *InfraCommand) Help() string {
    70  	helpText := `
    71  Usage: otto infra [options]
    72  
    73    Builds the infrastructure for the Appfile.
    74  
    75    This will create real infrastructure resources as configured by the
    76    Appfile, such as launching real servers. This command is stateful. If
    77    the infrastructure has already been created, it won't create it again.
    78    If the infrastructure is created but needs to be modified, it will be
    79    modified.
    80  
    81    Note that not all infrastructure changes are non-destructive and this
    82    command may cause downtime.
    83  
    84  `
    85  
    86  	return strings.TrimSpace(helpText)
    87  }