github.com/cpuid/libcompose@v0.4.0/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  		Flags: []cli.Flag{
    63  			cli.BoolFlag{
    64  				Name:  "quiet,q",
    65  				Usage: "Only validate the configuration, don't print anything.",
    66  			},
    67  		},
    68  	}
    69  }
    70  
    71  // BuildCommand defines the libcompose build subcommand.
    72  func BuildCommand(factory app.ProjectFactory) cli.Command {
    73  	return cli.Command{
    74  		Name:   "build",
    75  		Usage:  "Build or rebuild services.",
    76  		Action: app.WithProject(factory, app.ProjectBuild),
    77  		Flags: []cli.Flag{
    78  			cli.BoolFlag{
    79  				Name:  "no-cache",
    80  				Usage: "Do not use cache when building the image",
    81  			},
    82  			cli.BoolFlag{
    83  				Name:  "force-rm",
    84  				Usage: "Always remove intermediate containers",
    85  			},
    86  			cli.BoolFlag{
    87  				Name:  "pull",
    88  				Usage: "Always attempt to pull a newer version of the image",
    89  			},
    90  		},
    91  	}
    92  }
    93  
    94  // PsCommand defines the libcompose ps subcommand.
    95  func PsCommand(factory app.ProjectFactory) cli.Command {
    96  	return cli.Command{
    97  		Name:   "ps",
    98  		Usage:  "List containers",
    99  		Action: app.WithProject(factory, app.ProjectPs),
   100  		Flags: []cli.Flag{
   101  			cli.BoolFlag{
   102  				Name:  "q",
   103  				Usage: "Only display IDs",
   104  			},
   105  		},
   106  	}
   107  }
   108  
   109  // PortCommand defines the libcompose port subcommand.
   110  func PortCommand(factory app.ProjectFactory) cli.Command {
   111  	return cli.Command{
   112  		Name:   "port",
   113  		Usage:  "Print the public port for a port binding",
   114  		Action: app.WithProject(factory, app.ProjectPort),
   115  		Flags: []cli.Flag{
   116  			cli.StringFlag{
   117  				Name:  "protocol",
   118  				Usage: "tcp or udp ",
   119  				Value: "tcp",
   120  			},
   121  			cli.IntFlag{
   122  				Name:  "index",
   123  				Usage: "index of the container if there are multiple instances of a service",
   124  				Value: 1,
   125  			},
   126  		},
   127  	}
   128  }
   129  
   130  // UpCommand defines the libcompose up subcommand.
   131  func UpCommand(factory app.ProjectFactory) cli.Command {
   132  	return cli.Command{
   133  		Name:   "up",
   134  		Usage:  "Bring all services up",
   135  		Action: app.WithProject(factory, app.ProjectUp),
   136  		Flags: []cli.Flag{
   137  			cli.BoolFlag{
   138  				Name:  "d",
   139  				Usage: "Do not block and log",
   140  			},
   141  			cli.BoolFlag{
   142  				Name:  "no-build",
   143  				Usage: "Don't build an image, even if it's missing.",
   144  			},
   145  			cli.BoolFlag{
   146  				Name:  "no-recreate",
   147  				Usage: "If containers already exist, don't recreate them. Incompatible with --force-recreate.",
   148  			},
   149  			cli.BoolFlag{
   150  				Name:  "force-recreate",
   151  				Usage: "Recreate containers even if their configuration and image haven't changed. Incompatible with --no-recreate.",
   152  			},
   153  			cli.BoolFlag{
   154  				Name:  "build",
   155  				Usage: "Build images before starting containers.",
   156  			},
   157  		},
   158  	}
   159  }
   160  
   161  // StartCommand defines the libcompose start subcommand.
   162  func StartCommand(factory app.ProjectFactory) cli.Command {
   163  	return cli.Command{
   164  		Name:   "start",
   165  		Usage:  "Start services",
   166  		Action: app.WithProject(factory, app.ProjectStart),
   167  		Flags: []cli.Flag{
   168  			cli.BoolTFlag{
   169  				Name:  "d",
   170  				Usage: "Do not block and log",
   171  			},
   172  		},
   173  	}
   174  }
   175  
   176  // RunCommand defines the libcompose run subcommand.
   177  func RunCommand(factory app.ProjectFactory) cli.Command {
   178  	return cli.Command{
   179  		Name:   "run",
   180  		Usage:  "Run a one-off command",
   181  		Action: app.WithProject(factory, app.ProjectRun),
   182  		Flags: []cli.Flag{
   183  			cli.BoolFlag{
   184  				Name:  "d",
   185  				Usage: "Detached mode: Run container in the background, print new container name.",
   186  			},
   187  		},
   188  	}
   189  }
   190  
   191  // PullCommand defines the libcompose pull subcommand.
   192  func PullCommand(factory app.ProjectFactory) cli.Command {
   193  	return cli.Command{
   194  		Name:   "pull",
   195  		Usage:  "Pulls images for services",
   196  		Action: app.WithProject(factory, app.ProjectPull),
   197  		Flags: []cli.Flag{
   198  			cli.BoolFlag{
   199  				Name:  "ignore-pull-failures",
   200  				Usage: "Pull what it can and ignores images with pull failures.",
   201  			},
   202  		},
   203  	}
   204  }
   205  
   206  // LogsCommand defines the libcompose logs subcommand.
   207  func LogsCommand(factory app.ProjectFactory) cli.Command {
   208  	return cli.Command{
   209  		Name:   "logs",
   210  		Usage:  "Get service logs",
   211  		Action: app.WithProject(factory, app.ProjectLog),
   212  		Flags: []cli.Flag{
   213  			cli.BoolFlag{
   214  				Name:  "follow",
   215  				Usage: "Follow log output.",
   216  			},
   217  		},
   218  	}
   219  }
   220  
   221  // RestartCommand defines the libcompose restart subcommand.
   222  func RestartCommand(factory app.ProjectFactory) cli.Command {
   223  	return cli.Command{
   224  		Name:   "restart",
   225  		Usage:  "Restart services",
   226  		Action: app.WithProject(factory, app.ProjectRestart),
   227  		Flags: []cli.Flag{
   228  			cli.IntFlag{
   229  				Name:  "timeout,t",
   230  				Usage: "Specify a shutdown timeout in seconds.",
   231  				Value: 10,
   232  			},
   233  		},
   234  	}
   235  }
   236  
   237  // StopCommand defines the libcompose stop subcommand.
   238  func StopCommand(factory app.ProjectFactory) cli.Command {
   239  	return cli.Command{
   240  		Name:   "stop",
   241  		Usage:  "Stop services",
   242  		Action: app.WithProject(factory, app.ProjectStop),
   243  		Flags: []cli.Flag{
   244  			cli.IntFlag{
   245  				Name:  "timeout,t",
   246  				Usage: "Specify a shutdown timeout in seconds.",
   247  				Value: 10,
   248  			},
   249  		},
   250  	}
   251  }
   252  
   253  // DownCommand defines the libcompose stop subcommand.
   254  func DownCommand(factory app.ProjectFactory) cli.Command {
   255  	return cli.Command{
   256  		Name:   "down",
   257  		Usage:  "Stop and remove containers, networks, images, and volumes",
   258  		Action: app.WithProject(factory, app.ProjectDown),
   259  		Flags: []cli.Flag{
   260  			cli.BoolFlag{
   261  				Name:  "volumes,v",
   262  				Usage: "Remove data volumes",
   263  			},
   264  			cli.StringFlag{
   265  				Name:  "rmi",
   266  				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",
   267  			},
   268  			cli.BoolFlag{
   269  				Name:  "remove-orphans",
   270  				Usage: "Remove containers for services not defined in the Compose file",
   271  			},
   272  		},
   273  	}
   274  }
   275  
   276  // ScaleCommand defines the libcompose scale subcommand.
   277  func ScaleCommand(factory app.ProjectFactory) cli.Command {
   278  	return cli.Command{
   279  		Name:   "scale",
   280  		Usage:  "Scale services",
   281  		Action: app.WithProject(factory, app.ProjectScale),
   282  		Flags: []cli.Flag{
   283  			cli.IntFlag{
   284  				Name:  "timeout,t",
   285  				Usage: "Specify a shutdown timeout in seconds.",
   286  				Value: 10,
   287  			},
   288  		},
   289  	}
   290  }
   291  
   292  // RmCommand defines the libcompose rm subcommand.
   293  func RmCommand(factory app.ProjectFactory) cli.Command {
   294  	return cli.Command{
   295  		Name:   "rm",
   296  		Usage:  "Delete services",
   297  		Action: app.WithProject(factory, app.ProjectDelete),
   298  		Flags: []cli.Flag{
   299  			cli.BoolFlag{
   300  				Name:  "force,f",
   301  				Usage: "Allow deletion of all services",
   302  			},
   303  			cli.BoolFlag{
   304  				Name:  "v",
   305  				Usage: "Remove volumes associated with containers",
   306  			},
   307  		},
   308  	}
   309  }
   310  
   311  // KillCommand defines the libcompose kill subcommand.
   312  func KillCommand(factory app.ProjectFactory) cli.Command {
   313  	return cli.Command{
   314  		Name:   "kill",
   315  		Usage:  "Force stop service containers",
   316  		Action: app.WithProject(factory, app.ProjectKill),
   317  		Flags: []cli.Flag{
   318  			cli.StringFlag{
   319  				Name:  "signal,s",
   320  				Usage: "SIGNAL to send to the container",
   321  				Value: "SIGKILL",
   322  			},
   323  		},
   324  	}
   325  }
   326  
   327  // PauseCommand defines the libcompose pause subcommand.
   328  func PauseCommand(factory app.ProjectFactory) cli.Command {
   329  	return cli.Command{
   330  		Name:  "pause",
   331  		Usage: "Pause services.",
   332  		// ArgsUsage: "[SERVICE...]",
   333  		Action: app.WithProject(factory, app.ProjectPause),
   334  	}
   335  }
   336  
   337  // UnpauseCommand defines the libcompose unpause subcommand.
   338  func UnpauseCommand(factory app.ProjectFactory) cli.Command {
   339  	return cli.Command{
   340  		Name:  "unpause",
   341  		Usage: "Unpause services.",
   342  		// ArgsUsage: "[SERVICE...]",
   343  		Action: app.WithProject(factory, app.ProjectUnpause),
   344  	}
   345  }
   346  
   347  // EventsCommand defines the libcompose events subcommand
   348  func EventsCommand(factory app.ProjectFactory) cli.Command {
   349  	return cli.Command{
   350  		Name:   "events",
   351  		Usage:  "Receive real time events from containers.",
   352  		Action: app.WithProject(factory, app.ProjectEvents),
   353  		Flags: []cli.Flag{
   354  			cli.BoolFlag{
   355  				Name:  "json",
   356  				Usage: "Output events as a stream of json objects",
   357  			},
   358  		},
   359  	}
   360  }
   361  
   362  // VersionCommand defines the libcompose version subcommand.
   363  func VersionCommand(factory app.ProjectFactory) cli.Command {
   364  	return cli.Command{
   365  		Name:   "version",
   366  		Usage:  "Show version informations",
   367  		Action: app.Version,
   368  		Flags: []cli.Flag{
   369  			cli.BoolFlag{
   370  				Name:  "short",
   371  				Usage: "Shows only Compose's version number.",
   372  			},
   373  		},
   374  	}
   375  }
   376  
   377  // CommonFlags defines the flags that are in common for all subcommands.
   378  func CommonFlags() []cli.Flag {
   379  	return []cli.Flag{
   380  		cli.BoolFlag{
   381  			Name: "verbose,debug",
   382  		},
   383  		cli.StringSliceFlag{
   384  			Name:   "file,f",
   385  			Usage:  "Specify one or more alternate compose files (default: docker-compose.yml)",
   386  			Value:  &cli.StringSlice{},
   387  			EnvVar: "COMPOSE_FILE",
   388  		},
   389  		cli.StringFlag{
   390  			Name:   "project-name,p",
   391  			Usage:  "Specify an alternate project name (default: directory name)",
   392  			EnvVar: "COMPOSE_PROJECT_NAME",
   393  		},
   394  	}
   395  }