github.com/bdwilliams/libcompose@v0.3.1-0.20160826154243-d81a9bdacff0/cli/command/command.go (about) 1 package command 2 3 import ( 4 "os" 5 "strings" 6 7 "github.com/docker/libcompose/cli/app" 8 "github.com/docker/libcompose/project" 9 "github.com/urfave/cli" 10 ) 11 12 // Populate updates the specified project context based on command line arguments and subcommands. 13 func Populate(context *project.Context, c *cli.Context) { 14 // urfave/cli does not distinguish whether the first string in the slice comes from the envvar 15 // or is from a flag. Worse off, it appends the flag values to the envvar value instead of 16 // overriding it. To ensure the multifile envvar case is always handled, the first string 17 // must always be split. It gives a more consistent behavior, then, to split each string in 18 // the slice. 19 for _, v := range c.GlobalStringSlice("file") { 20 context.ComposeFiles = append(context.ComposeFiles, strings.Split(v, string(os.PathListSeparator))...) 21 } 22 23 if len(context.ComposeFiles) == 0 { 24 context.ComposeFiles = []string{"docker-compose.yml"} 25 if _, err := os.Stat("docker-compose.override.yml"); err == nil { 26 context.ComposeFiles = append(context.ComposeFiles, "docker-compose.override.yml") 27 } 28 } 29 30 context.ProjectName = c.GlobalString("project-name") 31 } 32 33 // CreateCommand defines the libcompose create subcommand. 34 func CreateCommand(factory app.ProjectFactory) cli.Command { 35 return cli.Command{ 36 Name: "create", 37 Usage: "Create all services but do not start", 38 Action: app.WithProject(factory, app.ProjectCreate), 39 Flags: []cli.Flag{ 40 cli.BoolFlag{ 41 Name: "no-recreate", 42 Usage: "If containers already exist, don't recreate them. Incompatible with --force-recreate.", 43 }, 44 cli.BoolFlag{ 45 Name: "force-recreate", 46 Usage: "Recreate containers even if their configuration and image haven't changed. Incompatible with --no-recreate.", 47 }, 48 cli.BoolFlag{ 49 Name: "no-build", 50 Usage: "Don't build an image, even if it's missing.", 51 }, 52 }, 53 } 54 } 55 56 // ConfigCommand defines the libcompose config subcommand 57 func ConfigCommand(factory app.ProjectFactory) cli.Command { 58 return cli.Command{ 59 Name: "config", 60 Usage: "Validate and view the compose file.", 61 Action: app.WithProject(factory, app.ProjectConfig), 62 } 63 } 64 65 // BuildCommand defines the libcompose build subcommand. 66 func BuildCommand(factory app.ProjectFactory) cli.Command { 67 return cli.Command{ 68 Name: "build", 69 Usage: "Build or rebuild services.", 70 Action: app.WithProject(factory, app.ProjectBuild), 71 Flags: []cli.Flag{ 72 cli.BoolFlag{ 73 Name: "no-cache", 74 Usage: "Do not use cache when building the image", 75 }, 76 cli.BoolFlag{ 77 Name: "force-rm", 78 Usage: "Always remove intermediate containers", 79 }, 80 cli.BoolFlag{ 81 Name: "pull", 82 Usage: "Always attempt to pull a newer version of the image", 83 }, 84 }, 85 } 86 } 87 88 // PsCommand defines the libcompose ps subcommand. 89 func PsCommand(factory app.ProjectFactory) cli.Command { 90 return cli.Command{ 91 Name: "ps", 92 Usage: "List containers", 93 Action: app.WithProject(factory, app.ProjectPs), 94 Flags: []cli.Flag{ 95 cli.BoolFlag{ 96 Name: "q", 97 Usage: "Only display IDs", 98 }, 99 }, 100 } 101 } 102 103 // PortCommand defines the libcompose port subcommand. 104 func PortCommand(factory app.ProjectFactory) cli.Command { 105 return cli.Command{ 106 Name: "port", 107 Usage: "Print the public port for a port binding", 108 Action: app.WithProject(factory, app.ProjectPort), 109 Flags: []cli.Flag{ 110 cli.StringFlag{ 111 Name: "protocol", 112 Usage: "tcp or udp ", 113 Value: "tcp", 114 }, 115 cli.IntFlag{ 116 Name: "index", 117 Usage: "index of the container if there are multiple instances of a service", 118 Value: 1, 119 }, 120 }, 121 } 122 } 123 124 // UpCommand defines the libcompose up subcommand. 125 func UpCommand(factory app.ProjectFactory) cli.Command { 126 return cli.Command{ 127 Name: "up", 128 Usage: "Bring all services up", 129 Action: app.WithProject(factory, app.ProjectUp), 130 Flags: []cli.Flag{ 131 cli.BoolFlag{ 132 Name: "d", 133 Usage: "Do not block and log", 134 }, 135 cli.BoolFlag{ 136 Name: "no-build", 137 Usage: "Don't build an image, even if it's missing.", 138 }, 139 cli.BoolFlag{ 140 Name: "no-recreate", 141 Usage: "If containers already exist, don't recreate them. Incompatible with --force-recreate.", 142 }, 143 cli.BoolFlag{ 144 Name: "force-recreate", 145 Usage: "Recreate containers even if their configuration and image haven't changed. Incompatible with --no-recreate.", 146 }, 147 cli.BoolFlag{ 148 Name: "build", 149 Usage: "Build images before starting containers.", 150 }, 151 }, 152 } 153 } 154 155 // StartCommand defines the libcompose start subcommand. 156 func StartCommand(factory app.ProjectFactory) cli.Command { 157 return cli.Command{ 158 Name: "start", 159 Usage: "Start services", 160 Action: app.WithProject(factory, app.ProjectStart), 161 Flags: []cli.Flag{ 162 cli.BoolTFlag{ 163 Name: "d", 164 Usage: "Do not block and log", 165 }, 166 }, 167 } 168 } 169 170 // RunCommand defines the libcompose run subcommand. 171 func RunCommand(factory app.ProjectFactory) cli.Command { 172 return cli.Command{ 173 Name: "run", 174 Usage: "Run a one-off command", 175 Action: app.WithProject(factory, app.ProjectRun), 176 Flags: []cli.Flag{ 177 cli.BoolFlag{ 178 Name: "d", 179 Usage: "Detached mode: Run container in the background, print new container name.", 180 }, 181 }, 182 } 183 } 184 185 // PullCommand defines the libcompose pull subcommand. 186 func PullCommand(factory app.ProjectFactory) cli.Command { 187 return cli.Command{ 188 Name: "pull", 189 Usage: "Pulls images for services", 190 Action: app.WithProject(factory, app.ProjectPull), 191 Flags: []cli.Flag{ 192 cli.BoolFlag{ 193 Name: "ignore-pull-failures", 194 Usage: "Pull what it can and ignores images with pull failures.", 195 }, 196 }, 197 } 198 } 199 200 // LogsCommand defines the libcompose logs subcommand. 201 func LogsCommand(factory app.ProjectFactory) cli.Command { 202 return cli.Command{ 203 Name: "logs", 204 Usage: "Get service logs", 205 Action: app.WithProject(factory, app.ProjectLog), 206 Flags: []cli.Flag{ 207 cli.BoolFlag{ 208 Name: "follow", 209 Usage: "Follow log output.", 210 }, 211 }, 212 } 213 } 214 215 // RestartCommand defines the libcompose restart subcommand. 216 func RestartCommand(factory app.ProjectFactory) cli.Command { 217 return cli.Command{ 218 Name: "restart", 219 Usage: "Restart services", 220 Action: app.WithProject(factory, app.ProjectRestart), 221 Flags: []cli.Flag{ 222 cli.IntFlag{ 223 Name: "timeout,t", 224 Usage: "Specify a shutdown timeout in seconds.", 225 Value: 10, 226 }, 227 }, 228 } 229 } 230 231 // StopCommand defines the libcompose stop subcommand. 232 func StopCommand(factory app.ProjectFactory) cli.Command { 233 return cli.Command{ 234 Name: "stop", 235 Usage: "Stop services", 236 Action: app.WithProject(factory, app.ProjectStop), 237 Flags: []cli.Flag{ 238 cli.IntFlag{ 239 Name: "timeout,t", 240 Usage: "Specify a shutdown timeout in seconds.", 241 Value: 10, 242 }, 243 }, 244 } 245 } 246 247 // DownCommand defines the libcompose stop subcommand. 248 func DownCommand(factory app.ProjectFactory) cli.Command { 249 return cli.Command{ 250 Name: "down", 251 Usage: "Stop and remove containers, networks, images, and volumes", 252 Action: app.WithProject(factory, app.ProjectDown), 253 Flags: []cli.Flag{ 254 cli.BoolFlag{ 255 Name: "volumes,v", 256 Usage: "Remove data volumes", 257 }, 258 cli.StringFlag{ 259 Name: "rmi", 260 Usage: "Remove images, type may be one of: 'all' to remove all images, or 'local' to remove only images that don't have an custom name set by the `image` field", 261 }, 262 cli.BoolFlag{ 263 Name: "remove-orphans", 264 Usage: "Remove containers for services not defined in the Compose file", 265 }, 266 }, 267 } 268 } 269 270 // ScaleCommand defines the libcompose scale subcommand. 271 func ScaleCommand(factory app.ProjectFactory) cli.Command { 272 return cli.Command{ 273 Name: "scale", 274 Usage: "Scale services", 275 Action: app.WithProject(factory, app.ProjectScale), 276 Flags: []cli.Flag{ 277 cli.IntFlag{ 278 Name: "timeout,t", 279 Usage: "Specify a shutdown timeout in seconds.", 280 Value: 10, 281 }, 282 }, 283 } 284 } 285 286 // RmCommand defines the libcompose rm subcommand. 287 func RmCommand(factory app.ProjectFactory) cli.Command { 288 return cli.Command{ 289 Name: "rm", 290 Usage: "Delete services", 291 Action: app.WithProject(factory, app.ProjectDelete), 292 Flags: []cli.Flag{ 293 cli.BoolFlag{ 294 Name: "force,f", 295 Usage: "Allow deletion of all services", 296 }, 297 cli.BoolFlag{ 298 Name: "v", 299 Usage: "Remove volumes associated with containers", 300 }, 301 }, 302 } 303 } 304 305 // KillCommand defines the libcompose kill subcommand. 306 func KillCommand(factory app.ProjectFactory) cli.Command { 307 return cli.Command{ 308 Name: "kill", 309 Usage: "Force stop service containers", 310 Action: app.WithProject(factory, app.ProjectKill), 311 Flags: []cli.Flag{ 312 cli.StringFlag{ 313 Name: "signal,s", 314 Usage: "SIGNAL to send to the container", 315 Value: "SIGKILL", 316 }, 317 }, 318 } 319 } 320 321 // PauseCommand defines the libcompose pause subcommand. 322 func PauseCommand(factory app.ProjectFactory) cli.Command { 323 return cli.Command{ 324 Name: "pause", 325 Usage: "Pause services.", 326 // ArgsUsage: "[SERVICE...]", 327 Action: app.WithProject(factory, app.ProjectPause), 328 } 329 } 330 331 // UnpauseCommand defines the libcompose unpause subcommand. 332 func UnpauseCommand(factory app.ProjectFactory) cli.Command { 333 return cli.Command{ 334 Name: "unpause", 335 Usage: "Unpause services.", 336 // ArgsUsage: "[SERVICE...]", 337 Action: app.WithProject(factory, app.ProjectUnpause), 338 } 339 } 340 341 // EventsCommand defines the libcompose events subcommand 342 func EventsCommand(factory app.ProjectFactory) cli.Command { 343 return cli.Command{ 344 Name: "events", 345 Usage: "Receive real time events from containers.", 346 Action: app.WithProject(factory, app.ProjectEvents), 347 Flags: []cli.Flag{ 348 cli.BoolFlag{ 349 Name: "json", 350 Usage: "Output events as a stream of json objects", 351 }, 352 }, 353 } 354 } 355 356 // VersionCommand defines the libcompose version subcommand. 357 func VersionCommand(factory app.ProjectFactory) cli.Command { 358 return cli.Command{ 359 Name: "version", 360 Usage: "Show version informations", 361 Action: app.Version, 362 Flags: []cli.Flag{ 363 cli.BoolFlag{ 364 Name: "short", 365 Usage: "Shows only Compose's version number.", 366 }, 367 }, 368 } 369 } 370 371 // CommonFlags defines the flags that are in common for all subcommands. 372 func CommonFlags() []cli.Flag { 373 return []cli.Flag{ 374 cli.BoolFlag{ 375 Name: "verbose,debug", 376 }, 377 cli.StringSliceFlag{ 378 Name: "file,f", 379 Usage: "Specify one or more alternate compose files (default: docker-compose.yml)", 380 Value: &cli.StringSlice{}, 381 EnvVar: "COMPOSE_FILE", 382 }, 383 cli.StringFlag{ 384 Name: "project-name,p", 385 Usage: "Specify an alternate project name (default: directory name)", 386 EnvVar: "COMPOSE_PROJECT_NAME", 387 }, 388 } 389 }