github.com/greenboxal/deis@v1.12.1/client/parser/apps.go (about)

     1  package parser
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  
     7  	"github.com/deis/deis/client/cmd"
     8  	docopt "github.com/docopt/docopt-go"
     9  )
    10  
    11  // Apps routes app commands to their specific function.
    12  func Apps(argv []string) 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)
    31  	case "apps:list":
    32  		return appsList(argv)
    33  	case "apps:info":
    34  		return appInfo(argv)
    35  	case "apps:open":
    36  		return appOpen(argv)
    37  	case "apps:logs":
    38  		return appLogs(argv)
    39  	case "apps:run":
    40  		return appRun(argv)
    41  	case "apps:destroy":
    42  		return appDestroy(argv)
    43  	case "apps:transfer":
    44  		return appTransfer(argv)
    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)
    53  		}
    54  
    55  		PrintUsage()
    56  		return nil
    57  	}
    58  }
    59  
    60  func appCreate(argv []string) 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  	args, err := docopt.Parse(usage, argv, true, "", false, true)
    82  
    83  	if err != nil {
    84  		return err
    85  	}
    86  
    87  	id := safeGetValue(args, "<id>")
    88  	buildpack := safeGetValue(args, "--buildpack")
    89  	remote := safeGetValue(args, "--remote")
    90  	noRemote := args["--no-remote"].(bool)
    91  
    92  	return cmd.AppCreate(id, buildpack, remote, noRemote)
    93  }
    94  
    95  func appsList(argv []string) error {
    96  	usage := `
    97  Lists applications visible to the current user.
    98  
    99  Usage: deis apps:list [options]
   100  
   101  Options:
   102    -l --limit=<num>
   103      the maximum number of results to display, defaults to config setting
   104  `
   105  	args, err := docopt.Parse(usage, argv, true, "", false, true)
   106  
   107  	if err != nil {
   108  		return err
   109  	}
   110  
   111  	results, err := responseLimit(safeGetValue(args, "--limit"))
   112  
   113  	if err != nil {
   114  		return err
   115  	}
   116  
   117  	return cmd.AppsList(results)
   118  }
   119  
   120  func appInfo(argv []string) error {
   121  	usage := `
   122  Prints info about the current application.
   123  
   124  Usage: deis apps:info [options]
   125  
   126  Options:
   127    -a --app=<app>
   128      the uniquely identifiable name for the application.
   129  `
   130  	args, err := docopt.Parse(usage, argv, true, "", false, true)
   131  
   132  	if err != nil {
   133  		return err
   134  	}
   135  
   136  	app := safeGetValue(args, "--app")
   137  
   138  	return cmd.AppInfo(app)
   139  }
   140  
   141  func appOpen(argv []string) error {
   142  	usage := `
   143  Opens a URL to the application in the default browser.
   144  
   145  Usage: deis apps:open [options]
   146  
   147  Options:
   148    -a --app=<app>
   149      the uniquely identifiable name for the application.
   150  `
   151  	args, err := docopt.Parse(usage, argv, true, "", false, true)
   152  
   153  	if err != nil {
   154  		return err
   155  	}
   156  
   157  	app := safeGetValue(args, "--app")
   158  
   159  	return cmd.AppOpen(app)
   160  }
   161  
   162  func appLogs(argv []string) error {
   163  	usage := `
   164  Retrieves the most recent log events.
   165  
   166  Usage: deis apps:logs [options]
   167  
   168  Options:
   169    -a --app=<app>
   170      the uniquely identifiable name for the application.
   171    -n --lines=<lines>
   172      the number of lines to display
   173  `
   174  	args, err := docopt.Parse(usage, argv, true, "", false, true)
   175  
   176  	if err != nil {
   177  		return err
   178  	}
   179  
   180  	app := safeGetValue(args, "--app")
   181  
   182  	linesStr := safeGetValue(args, "--lines")
   183  	var lines int
   184  
   185  	if linesStr == "" {
   186  		lines = -1
   187  	} else {
   188  		lines, err = strconv.Atoi(linesStr)
   189  
   190  		if err != nil {
   191  			return err
   192  		}
   193  	}
   194  
   195  	return cmd.AppLogs(app, lines)
   196  }
   197  
   198  func appRun(argv []string) error {
   199  	usage := `
   200  Runs a command inside an ephemeral app container. Default environment is
   201  /bin/bash.
   202  
   203  Usage: deis apps:run [options] [--] <command>...
   204  
   205  Arguments:
   206    <command>
   207      the shell command to run inside the container.
   208  
   209  Options:
   210    -a --app=<app>
   211      the uniquely identifiable name for the application.
   212  `
   213  	args, err := docopt.Parse(usage, argv, true, "", false, true)
   214  
   215  	if err != nil {
   216  		return err
   217  	}
   218  
   219  	app := safeGetValue(args, "--app")
   220  	command := strings.Join(args["<command>"].([]string), " ")
   221  
   222  	return cmd.AppRun(app, command)
   223  }
   224  
   225  func appDestroy(argv []string) error {
   226  	usage := `
   227  Destroys an application.
   228  
   229  Usage: deis apps:destroy [options]
   230  
   231  Options:
   232    -a --app=<app>
   233      the uniquely identifiable name for the application.
   234    --confirm=<app>
   235      skips the prompt for the application name. <app> is the uniquely identifiable
   236      name for the application.
   237  
   238  `
   239  	args, err := docopt.Parse(usage, argv, true, "", false, true)
   240  
   241  	if err != nil {
   242  		return err
   243  	}
   244  
   245  	app := safeGetValue(args, "--app")
   246  	confirm := safeGetValue(args, "--confirm")
   247  
   248  	return cmd.AppDestroy(app, confirm)
   249  }
   250  
   251  func appTransfer(argv []string) error {
   252  	usage := `
   253  Transfer app ownership to another user.
   254  
   255  Usage: deis apps:transfer <username> [options]
   256  
   257  Arguments:
   258    <username>
   259      the user that the app will be transfered to.
   260  
   261  Options:
   262    -a --app=<app>
   263      the uniquely identifiable name for the application.
   264  `
   265  	args, err := docopt.Parse(usage, argv, true, "", false, true)
   266  
   267  	if err != nil {
   268  		return err
   269  	}
   270  
   271  	return cmd.AppTransfer(safeGetValue(args, "--app"), safeGetValue(args, "<username>"))
   272  }