github.com/engineyard/workflow-cli@v2.21.6+incompatible/parser/apps.go (about)

     1  package parser
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  
     7  	"github.com/teamhephy/workflow-cli/cmd"
     8  	docopt "github.com/docopt/docopt-go"
     9  )
    10  
    11  // Apps routes app commands to their specific function.
    12  func Apps(argv []string, cmdr cmd.Commander) error {
    13  	usage := `
    14  Valid commands for apps:
    15  
    16  apps:create        create a new application
    17  apps:list          list accessible applications
    18  apps:info          view info about an application
    19  apps:open          open the application in a browser
    20  apps:logs          view aggregated application logs
    21  apps:run           run a command in an ephemeral app container
    22  apps:destroy       destroy an application
    23  apps:transfer      transfer app ownership to another user
    24  
    25  Use 'deis help [command]' to learn more.
    26  `
    27  
    28  	switch argv[0] {
    29  	case "apps:create":
    30  		return appCreate(argv, cmdr)
    31  	case "apps:list":
    32  		return appsList(argv, cmdr)
    33  	case "apps:info":
    34  		return appInfo(argv, cmdr)
    35  	case "apps:open":
    36  		return appOpen(argv, cmdr)
    37  	case "apps:logs":
    38  		return appLogs(argv, cmdr)
    39  	case "apps:run":
    40  		return appRun(argv, cmdr)
    41  	case "apps:destroy":
    42  		return appDestroy(argv, cmdr)
    43  	case "apps:transfer":
    44  		return appTransfer(argv, cmdr)
    45  	default:
    46  		if printHelp(argv, usage) {
    47  			return nil
    48  		}
    49  
    50  		if argv[0] == "apps" {
    51  			argv[0] = "apps:list"
    52  			return appsList(argv, cmdr)
    53  		}
    54  
    55  		PrintUsage(cmdr)
    56  		return nil
    57  	}
    58  }
    59  
    60  func appCreate(argv []string, cmdr cmd.Commander) error {
    61  	usage := `
    62  Creates a new application.
    63  
    64  - if no <id> is provided, one will be generated automatically.
    65  
    66  Usage: deis apps:create [<id>] [options]
    67  
    68  Arguments:
    69    <id>
    70      a uniquely identifiable name for the application. No other app can already
    71      exist with this name.
    72  
    73  Options:
    74    --no-remote
    75      do not create a 'deis' git remote.
    76    -b --buildpack BUILDPACK
    77      a buildpack url to use for this app
    78    -r --remote REMOTE
    79      name of remote to create. [default: deis]
    80  `
    81  
    82  	args, err := docopt.Parse(usage, argv, true, "", false, true)
    83  
    84  	if err != nil {
    85  		return err
    86  	}
    87  
    88  	id := safeGetValue(args, "<id>")
    89  	buildpack := safeGetValue(args, "--buildpack")
    90  	remote := safeGetValue(args, "--remote")
    91  	noRemote := args["--no-remote"].(bool)
    92  
    93  	return cmdr.AppCreate(id, buildpack, remote, noRemote)
    94  }
    95  
    96  func appsList(argv []string, cmdr cmd.Commander) error {
    97  	usage := `
    98  Lists applications visible to the current user.
    99  
   100  Usage: deis apps:list [options]
   101  
   102  Options:
   103    -l --limit=<num>
   104      the maximum number of results to display, defaults to config setting
   105  `
   106  
   107  	args, err := docopt.Parse(usage, argv, true, "", false, true)
   108  
   109  	if err != nil {
   110  		return err
   111  	}
   112  
   113  	results, err := responseLimit(safeGetValue(args, "--limit"))
   114  
   115  	if err != nil {
   116  		return err
   117  	}
   118  
   119  	return cmdr.AppsList(results)
   120  }
   121  
   122  func appInfo(argv []string, cmdr cmd.Commander) error {
   123  	usage := `
   124  Prints info about the current application.
   125  
   126  Usage: deis apps:info [options]
   127  
   128  Options:
   129    -a --app=<app>
   130      the uniquely identifiable name for the application.
   131  `
   132  
   133  	args, err := docopt.Parse(usage, argv, true, "", false, true)
   134  
   135  	if err != nil {
   136  		return err
   137  	}
   138  
   139  	app := safeGetValue(args, "--app")
   140  
   141  	return cmdr.AppInfo(app)
   142  }
   143  
   144  func appOpen(argv []string, cmdr cmd.Commander) error {
   145  	usage := `
   146  Opens a URL to the application in the default browser.
   147  
   148  Usage: deis apps:open [options]
   149  
   150  Options:
   151    -a --app=<app>
   152      the uniquely identifiable name for the application.
   153  `
   154  
   155  	args, err := docopt.Parse(usage, argv, true, "", false, true)
   156  
   157  	if err != nil {
   158  		return err
   159  	}
   160  
   161  	app := safeGetValue(args, "--app")
   162  
   163  	return cmdr.AppOpen(app)
   164  }
   165  
   166  func appLogs(argv []string, cmdr cmd.Commander) error {
   167  	usage := `
   168  Retrieves the most recent log events.
   169  
   170  Usage: deis apps:logs [options]
   171  
   172  Options:
   173    -a --app=<app>
   174      the uniquely identifiable name for the application.
   175    -n --lines=<lines>
   176      the number of lines to display
   177  `
   178  
   179  	args, err := docopt.Parse(usage, argv, true, "", false, true)
   180  
   181  	if err != nil {
   182  		return err
   183  	}
   184  
   185  	app := safeGetValue(args, "--app")
   186  
   187  	linesStr := safeGetValue(args, "--lines")
   188  	var lines int
   189  
   190  	if linesStr == "" {
   191  		lines = -1
   192  	} else {
   193  		lines, err = strconv.Atoi(linesStr)
   194  
   195  		if err != nil {
   196  			return err
   197  		}
   198  	}
   199  
   200  	return cmdr.AppLogs(app, lines)
   201  }
   202  
   203  func appRun(argv []string, cmdr cmd.Commander) error {
   204  	usage := `
   205  Runs a command inside an ephemeral app container. Default environment is
   206  /bin/bash.
   207  
   208  Usage: deis apps:run [options] [--] <command>...
   209  
   210  Arguments:
   211    <command>
   212      the shell command to run inside the container.
   213  
   214  Options:
   215    -a --app=<app>
   216      the uniquely identifiable name for the application.
   217  `
   218  
   219  	args, err := docopt.Parse(usage, argv, true, "", false, true)
   220  
   221  	if err != nil {
   222  		return err
   223  	}
   224  
   225  	app := safeGetValue(args, "--app")
   226  	command := strings.Join(args["<command>"].([]string), " ")
   227  
   228  	return cmdr.AppRun(app, command)
   229  }
   230  
   231  func appDestroy(argv []string, cmdr cmd.Commander) error {
   232  	usage := `
   233  Destroys an application.
   234  
   235  Usage: deis apps:destroy [options]
   236  
   237  Options:
   238    -a --app=<app>
   239      the uniquely identifiable name for the application.
   240    --confirm=<app>
   241      skips the prompt for the application name. <app> is the uniquely identifiable
   242      name for the application.
   243  `
   244  
   245  	args, err := docopt.Parse(usage, argv, true, "", false, true)
   246  
   247  	if err != nil {
   248  		return err
   249  	}
   250  
   251  	app := safeGetValue(args, "--app")
   252  	confirm := safeGetValue(args, "--confirm")
   253  
   254  	return cmdr.AppDestroy(app, confirm)
   255  }
   256  
   257  func appTransfer(argv []string, cmdr cmd.Commander) error {
   258  	usage := `
   259  Transfer app ownership to another user.
   260  
   261  Usage: deis apps:transfer <username> [options]
   262  
   263  Arguments:
   264    <username>
   265      the user that the app will be transferred to.
   266  
   267  Options:
   268    -a --app=<app>
   269      the uniquely identifiable name for the application.
   270  `
   271  
   272  	args, err := docopt.Parse(usage, argv, true, "", false, true)
   273  
   274  	if err != nil {
   275  		return err
   276  	}
   277  
   278  	app := safeGetValue(args, "--app")
   279  	user := safeGetValue(args, "<username>")
   280  
   281  	return cmdr.AppTransfer(app, user)
   282  }