github.com/docker/libcompose@v0.4.1-0.20210616120443-2a046c0bdbf2/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  			},
   232  		},
   233  	}
   234  }
   235  
   236  // StopCommand defines the libcompose stop subcommand.
   237  func StopCommand(factory app.ProjectFactory) cli.Command {
   238  	return cli.Command{
   239  		Name:   "stop",
   240  		Usage:  "Stop services",
   241  		Action: app.WithProject(factory, app.ProjectStop),
   242  		Flags: []cli.Flag{
   243  			cli.IntFlag{
   244  				Name:  "timeout,t",
   245  				Usage: "Specify a shutdown timeout in seconds.",
   246  			},
   247  		},
   248  	}
   249  }
   250  
   251  // DownCommand defines the libcompose stop subcommand.
   252  func DownCommand(factory app.ProjectFactory) cli.Command {
   253  	return cli.Command{
   254  		Name:   "down",
   255  		Usage:  "Stop and remove containers, networks, images, and volumes",
   256  		Action: app.WithProject(factory, app.ProjectDown),
   257  		Flags: []cli.Flag{
   258  			cli.BoolFlag{
   259  				Name:  "volumes,v",
   260  				Usage: "Remove data volumes",
   261  			},
   262  			cli.StringFlag{
   263  				Name:  "rmi",
   264  				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",
   265  			},
   266  			cli.BoolFlag{
   267  				Name:  "remove-orphans",
   268  				Usage: "Remove containers for services not defined in the Compose file",
   269  			},
   270  		},
   271  	}
   272  }
   273  
   274  // ScaleCommand defines the libcompose scale subcommand.
   275  func ScaleCommand(factory app.ProjectFactory) cli.Command {
   276  	return cli.Command{
   277  		Name:   "scale",
   278  		Usage:  "Scale services",
   279  		Action: app.WithProject(factory, app.ProjectScale),
   280  		Flags: []cli.Flag{
   281  			cli.IntFlag{
   282  				Name:  "timeout,t",
   283  				Usage: "Specify a shutdown timeout in seconds.",
   284  			},
   285  		},
   286  	}
   287  }
   288  
   289  // RmCommand defines the libcompose rm subcommand.
   290  func RmCommand(factory app.ProjectFactory) cli.Command {
   291  	return cli.Command{
   292  		Name:   "rm",
   293  		Usage:  "Delete services",
   294  		Action: app.WithProject(factory, app.ProjectDelete),
   295  		Flags: []cli.Flag{
   296  			cli.BoolFlag{
   297  				Name:  "force,f",
   298  				Usage: "Allow deletion of all services",
   299  			},
   300  			cli.BoolFlag{
   301  				Name:  "v",
   302  				Usage: "Remove volumes associated with containers",
   303  			},
   304  		},
   305  	}
   306  }
   307  
   308  // KillCommand defines the libcompose kill subcommand.
   309  func KillCommand(factory app.ProjectFactory) cli.Command {
   310  	return cli.Command{
   311  		Name:   "kill",
   312  		Usage:  "Force stop service containers",
   313  		Action: app.WithProject(factory, app.ProjectKill),
   314  		Flags: []cli.Flag{
   315  			cli.StringFlag{
   316  				Name:  "signal,s",
   317  				Usage: "SIGNAL to send to the container",
   318  				Value: "SIGKILL",
   319  			},
   320  		},
   321  	}
   322  }
   323  
   324  // PauseCommand defines the libcompose pause subcommand.
   325  func PauseCommand(factory app.ProjectFactory) cli.Command {
   326  	return cli.Command{
   327  		Name:  "pause",
   328  		Usage: "Pause services.",
   329  		// ArgsUsage: "[SERVICE...]",
   330  		Action: app.WithProject(factory, app.ProjectPause),
   331  	}
   332  }
   333  
   334  // UnpauseCommand defines the libcompose unpause subcommand.
   335  func UnpauseCommand(factory app.ProjectFactory) cli.Command {
   336  	return cli.Command{
   337  		Name:  "unpause",
   338  		Usage: "Unpause services.",
   339  		// ArgsUsage: "[SERVICE...]",
   340  		Action: app.WithProject(factory, app.ProjectUnpause),
   341  	}
   342  }
   343  
   344  // EventsCommand defines the libcompose events subcommand
   345  func EventsCommand(factory app.ProjectFactory) cli.Command {
   346  	return cli.Command{
   347  		Name:   "events",
   348  		Usage:  "Receive real time events from containers.",
   349  		Action: app.WithProject(factory, app.ProjectEvents),
   350  		Flags: []cli.Flag{
   351  			cli.BoolFlag{
   352  				Name:  "json",
   353  				Usage: "Output events as a stream of json objects",
   354  			},
   355  		},
   356  	}
   357  }
   358  
   359  // VersionCommand defines the libcompose version subcommand.
   360  func VersionCommand(factory app.ProjectFactory) cli.Command {
   361  	return cli.Command{
   362  		Name:   "version",
   363  		Usage:  "Show version informations",
   364  		Action: app.Version,
   365  		Flags: []cli.Flag{
   366  			cli.BoolFlag{
   367  				Name:  "short",
   368  				Usage: "Shows only Compose's version number.",
   369  			},
   370  		},
   371  	}
   372  }
   373  
   374  // CommonFlags defines the flags that are in common for all subcommands.
   375  func CommonFlags() []cli.Flag {
   376  	return []cli.Flag{
   377  		cli.BoolFlag{
   378  			Name: "verbose,debug",
   379  		},
   380  		cli.StringSliceFlag{
   381  			Name:   "file,f",
   382  			Usage:  "Specify one or more alternate compose files (default: docker-compose.yml)",
   383  			Value:  &cli.StringSlice{},
   384  			EnvVar: "COMPOSE_FILE",
   385  		},
   386  		cli.StringFlag{
   387  			Name:   "project-name,p",
   388  			Usage:  "Specify an alternate project name (default: directory name)",
   389  			EnvVar: "COMPOSE_PROJECT_NAME",
   390  		},
   391  	}
   392  }