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

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/hashicorp/otto/helper/flag"
     8  	"github.com/hashicorp/otto/otto"
     9  )
    10  
    11  // DevCommand is the command that manages (starts, stops, etc.) the
    12  // development environment for an Appfile.
    13  type DevCommand struct {
    14  	Meta
    15  }
    16  
    17  func (c *DevCommand) Run(args []string) int {
    18  	fs := c.FlagSet("dev", FlagSetNone)
    19  	fs.Usage = func() { c.Ui.Error(c.Help()) }
    20  	args, execArgs, posArgs := flag.FilterArgs(fs, args)
    21  	if err := fs.Parse(args); err != nil {
    22  		return 1
    23  	}
    24  
    25  	// Get the remaining args to determine if we have an action.
    26  	var action string
    27  	if len(posArgs) > 0 {
    28  		action = posArgs[0]
    29  		execArgs = append(execArgs, posArgs[1:]...)
    30  	}
    31  
    32  	// Load the appfile
    33  	app, err := c.Appfile()
    34  	if err != nil {
    35  		c.Ui.Error(err.Error())
    36  		return 1
    37  	}
    38  
    39  	// Get a core
    40  	core, err := c.Core(app)
    41  	if err != nil {
    42  		c.Ui.Error(fmt.Sprintf(
    43  			"Error loading core: %s", err))
    44  		return 1
    45  	}
    46  
    47  	// If we have an action, then we use Execute(). Otherwise, we're
    48  	// building the dev environment with Dev().
    49  	if action == "" {
    50  		// Build the development environment
    51  		if err := core.Dev(); err != nil {
    52  			c.Ui.Error(fmt.Sprintf(
    53  				"Error building dev environment: %s", err))
    54  			return 1
    55  		}
    56  
    57  		return 0
    58  	}
    59  
    60  	// Execute the task
    61  	err = core.Execute(&otto.ExecuteOpts{
    62  		Task:   otto.ExecuteTaskDev,
    63  		Action: action,
    64  		Args:   execArgs,
    65  	})
    66  	if err != nil {
    67  		c.Ui.Error(err.Error())
    68  		return 1
    69  	}
    70  
    71  	return 0
    72  }
    73  
    74  func (c *DevCommand) Synopsis() string {
    75  	return "Start and manage a development environment"
    76  }
    77  
    78  func (c *DevCommand) Help() string {
    79  	helpText := `
    80  Usage: otto dev [subcommand] [options]
    81  
    82    Start and manage a development environment for your application.
    83  
    84    This will start a development environment for your application.
    85    Additional subcommands such as "destroy" can be given to tear down
    86    the development environment.
    87  
    88    The development environment will be local and will automatically include
    89    all upstream dependencies within the environment properly configured
    90    and started.
    91  
    92    The list of available subcommands depends on the type of application
    93    you're developing. To see the list, run "otto dev help".
    94  
    95  `
    96  
    97  	return strings.TrimSpace(helpText)
    98  }