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