2020-05-14 21:13:07 +02:00
/ *
2020-09-22 12:13:00 +02:00
Copyright 2020 Docker Compose CLI authors
2020-06-18 16:13:24 +02:00
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
2020-05-14 21:13:07 +02:00
* /
2020-05-05 10:58:24 +02:00
package compose
import (
2021-04-15 12:43:18 +02:00
"context"
2021-03-01 12:43:00 -03:00
"fmt"
2021-03-19 13:37:26 +01:00
"os"
2021-04-22 22:05:01 +02:00
"os/signal"
2021-06-07 14:21:55 +02:00
"path/filepath"
2021-04-06 11:20:58 +02:00
"strings"
2021-04-22 22:05:01 +02:00
"syscall"
2021-03-01 12:43:00 -03:00
2020-08-18 16:56:42 +02:00
"github.com/compose-spec/compose-go/cli"
2020-11-27 14:47:53 +01:00
"github.com/compose-spec/compose-go/types"
2022-05-30 17:30:30 +02:00
composegoutils "github.com/compose-spec/compose-go/utils"
2022-08-12 23:49:02 +02:00
"github.com/docker/buildx/util/logutil"
2021-04-15 12:43:18 +02:00
dockercli "github.com/docker/cli/cli"
2021-09-29 23:45:11 +02:00
"github.com/docker/cli/cli-plugins/manager"
2022-03-15 15:46:27 +01:00
"github.com/docker/cli/cli/command"
2021-03-01 12:43:00 -03:00
"github.com/morikuni/aec"
"github.com/pkg/errors"
2021-09-29 23:45:11 +02:00
"github.com/sirupsen/logrus"
2020-07-29 14:43:41 +02:00
"github.com/spf13/cobra"
2020-11-27 14:47:53 +01:00
"github.com/spf13/pflag"
2020-07-29 14:43:41 +02:00
2022-03-08 12:15:59 +01:00
"github.com/docker/compose/v2/cmd/formatter"
2021-08-31 18:53:24 +02:00
"github.com/docker/compose/v2/pkg/api"
"github.com/docker/compose/v2/pkg/compose"
2022-03-08 12:15:59 +01:00
"github.com/docker/compose/v2/pkg/progress"
"github.com/docker/compose/v2/pkg/utils"
2020-05-05 10:58:24 +02:00
)
2021-02-25 19:38:11 -03:00
// Command defines a compose CLI command as a func with args
2021-04-15 12:43:18 +02:00
type Command func ( context . Context , [ ] string ) error
2021-09-20 12:55:16 +02:00
// CobraCommand defines a cobra command function
2021-09-08 09:07:18 +02:00
type CobraCommand func ( context . Context , * cobra . Command , [ ] string ) error
// AdaptCmd adapt a CobraCommand func to cobra library
func AdaptCmd ( fn CobraCommand ) func ( cmd * cobra . Command , args [ ] string ) error {
2021-04-15 12:43:18 +02:00
return func ( cmd * cobra . Command , args [ ] string ) error {
2021-04-22 22:05:01 +02:00
ctx := cmd . Context ( )
contextString := fmt . Sprintf ( "%s" , ctx )
if ! strings . HasSuffix ( contextString , ".WithCancel" ) { // need to handle cancel
cancellableCtx , cancel := context . WithCancel ( cmd . Context ( ) )
ctx = cancellableCtx
s := make ( chan os . Signal , 1 )
signal . Notify ( s , syscall . SIGTERM , syscall . SIGINT )
go func ( ) {
<- s
cancel ( )
} ( )
}
2021-09-08 09:07:18 +02:00
err := fn ( ctx , cmd , args )
2021-06-15 09:57:38 +02:00
var composeErr compose . Error
2021-06-14 16:26:14 +02:00
if api . IsErrCanceled ( err ) || errors . Is ( ctx . Err ( ) , context . Canceled ) {
2021-04-22 22:05:01 +02:00
err = dockercli . StatusError {
StatusCode : 130 ,
2021-06-15 09:57:38 +02:00
Status : compose . CanceledStatus ,
2021-04-22 22:05:01 +02:00
}
}
2021-04-15 12:43:18 +02:00
if errors . As ( err , & composeErr ) {
err = dockercli . StatusError {
StatusCode : composeErr . GetMetricsFailureCategory ( ) . ExitCode ,
Status : err . Error ( ) ,
}
}
return err
}
}
2021-09-08 09:07:18 +02:00
// Adapt a Command func to cobra library
func Adapt ( fn Command ) func ( cmd * cobra . Command , args [ ] string ) error {
return AdaptCmd ( func ( ctx context . Context , cmd * cobra . Command , args [ ] string ) error {
return fn ( ctx , args )
} )
}
2021-01-20 11:02:46 +01:00
type projectOptions struct {
2021-09-22 09:57:31 +02:00
ProjectName string
Profiles [ ] string
ConfigPaths [ ] string
WorkDir string
ProjectDir string
EnvFile string
Compatibility bool
2020-10-12 11:03:43 +02:00
}
2021-06-03 15:30:05 +02:00
// ProjectFunc does stuff within a types.Project
type ProjectFunc func ( ctx context . Context , project * types . Project ) error
2021-06-07 14:21:55 +02:00
// ProjectServicesFunc does stuff within a types.Project and a selection of services
type ProjectServicesFunc func ( ctx context . Context , project * types . Project , services [ ] string ) error
2021-09-29 23:45:11 +02:00
// WithProject creates a cobra run command from a ProjectFunc based on configured project options and selected services
2021-06-07 14:21:55 +02:00
func ( o * projectOptions ) WithProject ( fn ProjectFunc ) func ( cmd * cobra . Command , args [ ] string ) error {
return o . WithServices ( func ( ctx context . Context , project * types . Project , services [ ] string ) error {
return fn ( ctx , project )
} )
}
2021-06-03 15:30:05 +02:00
// WithServices creates a cobra run command from a ProjectFunc based on configured project options and selected services
2021-06-07 14:21:55 +02:00
func ( o * projectOptions ) WithServices ( fn ProjectServicesFunc ) func ( cmd * cobra . Command , args [ ] string ) error {
return Adapt ( func ( ctx context . Context , args [ ] string ) error {
2021-09-03 10:13:13 +02:00
project , err := o . toProject ( args , cli . WithResolvedPaths ( true ) )
2021-06-03 15:30:05 +02:00
if err != nil {
return err
}
2021-06-07 14:21:55 +02:00
return fn ( ctx , project , args )
2021-06-03 15:30:05 +02:00
} )
}
2021-01-20 11:02:46 +01:00
func ( o * projectOptions ) addProjectFlags ( f * pflag . FlagSet ) {
2021-02-02 11:42:19 +01:00
f . StringArrayVar ( & o . Profiles , "profile" , [ ] string { } , "Specify a profile to enable" )
2021-01-20 11:02:46 +01:00
f . StringVarP ( & o . ProjectName , "project-name" , "p" , "" , "Project name" )
f . StringArrayVarP ( & o . ConfigPaths , "file" , "f" , [ ] string { } , "Compose configuration files" )
f . StringVar ( & o . EnvFile , "env-file" , "" , "Specify an alternate environment file." )
2022-04-28 17:51:35 +02:00
f . StringVar ( & o . ProjectDir , "project-directory" , "" , "Specify an alternate working directory\n(default: the path of the, first specified, Compose file)" )
f . StringVar ( & o . WorkDir , "workdir" , "" , "DEPRECATED! USE --project-directory INSTEAD.\nSpecify an alternate working directory\n(default: the path of the, first specified, Compose file)" )
2021-09-22 09:57:31 +02:00
f . BoolVar ( & o . Compatibility , "compatibility" , false , "Run compose in backward compatibility mode" )
2021-03-01 12:43:00 -03:00
_ = f . MarkHidden ( "workdir" )
2020-08-18 09:40:57 +02:00
}
2022-11-28 22:07:36 +01:00
func ( o * projectOptions ) projectOrName ( services ... string ) ( * types . Project , string , error ) {
2022-04-11 10:58:20 +02:00
name := o . ProjectName
var project * types . Project
2022-12-14 23:22:01 +01:00
if len ( o . ConfigPaths ) > 0 || o . ProjectName == "" {
2022-11-28 22:07:36 +01:00
p , err := o . toProject ( services )
2022-04-11 10:58:20 +02:00
if err != nil {
2022-08-03 14:50:33 +02:00
envProjectName := os . Getenv ( "COMPOSE_PROJECT_NAME" )
if envProjectName != "" {
return nil , envProjectName , nil
}
2022-04-11 10:58:20 +02:00
return nil , "" , err
}
project = p
name = p . Name
}
return project , name , nil
}
2021-01-20 11:02:46 +01:00
func ( o * projectOptions ) toProjectName ( ) ( string , error ) {
2020-12-29 16:22:57 -03:00
if o . ProjectName != "" {
return o . ProjectName , nil
2020-08-20 16:42:37 +02:00
}
2022-03-31 14:26:16 +02:00
envProjectName := os . Getenv ( "COMPOSE_PROJECT_NAME" )
if envProjectName != "" {
return envProjectName , nil
}
2021-02-02 11:42:19 +01:00
project , err := o . toProject ( nil )
2020-08-20 16:42:37 +02:00
if err != nil {
return "" , err
}
2021-01-14 15:59:57 +01:00
return project . Name , nil
}
2021-03-22 08:56:46 +01:00
func ( o * projectOptions ) toProject ( services [ ] string , po ... cli . ProjectOptionsFn ) ( * types . Project , error ) {
options , err := o . toProjectOptions ( po ... )
2021-01-14 15:59:57 +01:00
if err != nil {
2021-06-15 09:57:38 +02:00
return nil , compose . WrapComposeError ( err )
2021-01-14 15:59:57 +01:00
}
2020-08-20 16:42:37 +02:00
2022-07-29 18:55:22 +02:00
if o . Compatibility || utils . StringToBool ( options . Environment [ "COMPOSE_COMPATIBILITY" ] ) {
api . Separator = "_"
}
2020-08-20 16:42:37 +02:00
project , err := cli . ProjectFromOptions ( options )
if err != nil {
2021-06-15 09:57:38 +02:00
return nil , compose . WrapComposeError ( err )
2020-08-20 16:42:37 +02:00
}
2021-02-02 11:42:19 +01:00
2021-11-24 09:11:27 +01:00
for i , s := range project . Services {
s . CustomLabels = map [ string ] string {
api . ProjectLabel : project . Name ,
api . ServiceLabel : s . Name ,
api . VersionLabel : api . ComposeVersion ,
api . WorkingDirLabel : project . WorkingDir ,
api . ConfigFilesLabel : strings . Join ( project . ComposeFiles , "," ) ,
api . OneoffLabel : "False" , // default, will be overridden by `run` command
}
2022-12-13 14:01:51 +01:00
if o . EnvFile != "" {
s . CustomLabels [ api . EnvironmentFileLabel ] = o . EnvFile
2021-11-24 09:11:27 +01:00
}
project . Services [ i ] = s
}
2022-10-10 13:53:39 +02:00
if profiles , ok := options . Environment [ "COMPOSE_PROFILES" ] ; ok && len ( o . Profiles ) == 0 {
o . Profiles = append ( o . Profiles , strings . Split ( profiles , "," ) ... )
}
2021-05-05 10:12:04 +02:00
if len ( services ) > 0 {
s , err := project . GetServices ( services ... )
if err != nil {
return nil , err
}
o . Profiles = append ( o . Profiles , s . GetProfiles ( ) ... )
2021-02-02 11:42:19 +01:00
}
project . ApplyProfiles ( o . Profiles )
2021-06-23 10:49:50 +02:00
project . WithoutUnnecessaryResources ( )
2021-02-02 11:42:19 +01:00
err = project . ForServices ( services )
return project , err
2020-08-20 16:42:37 +02:00
}
2021-03-22 08:56:46 +01:00
func ( o * projectOptions ) toProjectOptions ( po ... cli . ProjectOptionsFn ) ( * cli . ProjectOptions , error ) {
2020-08-18 09:40:57 +02:00
return cli . NewProjectOptions ( o . ConfigPaths ,
2021-03-22 08:56:46 +01:00
append ( po ,
2021-10-22 14:59:25 +02:00
cli . WithWorkingDirectory ( o . ProjectDir ) ,
2022-05-17 17:06:01 +02:00
cli . WithOsEnv ,
2021-03-22 08:56:46 +01:00
cli . WithEnvFile ( o . EnvFile ) ,
cli . WithDotEnv ,
2021-05-20 14:58:57 +02:00
cli . WithConfigFileEnv ,
2021-04-26 11:53:34 +02:00
cli . WithDefaultConfigPath ,
2021-03-22 08:56:46 +01:00
cli . WithName ( o . ProjectName ) ) ... )
2020-08-18 09:40:57 +02:00
}
2021-12-08 18:41:25 +09:00
// PluginName is the name of the plugin
2021-12-02 11:03:38 +01:00
const PluginName = "compose"
2021-09-29 23:45:11 +02:00
// RunningAsStandalone detects when running as a standalone program
func RunningAsStandalone ( ) bool {
2021-12-02 11:03:38 +01:00
return len ( os . Args ) < 2 || os . Args [ 1 ] != manager . MetadataSubcommandName && os . Args [ 1 ] != PluginName
2021-09-29 23:45:11 +02:00
}
2021-04-15 12:43:18 +02:00
// RootCommand returns the compose command with its child commands
2022-12-13 14:01:51 +01:00
func RootCommand ( dockerCli command . Cli , backend api . Service ) * cobra . Command { //nolint:gocyclo
2022-08-12 23:49:02 +02:00
// filter out useless commandConn.CloseWrite warning message that can occur
// when using a remote context that is unreachable: "commandConn.CloseWrite: commandconn: failed to wait: signal: killed"
// https://github.com/docker/cli/blob/e1f24d3c93df6752d3c27c8d61d18260f141310c/cli/connhelper/commandconn/commandconn.go#L203-L215
logrus . AddHook ( logutil . NewFilter ( [ ] logrus . Level {
logrus . WarnLevel ,
} ,
"commandConn.CloseWrite:" ,
"commandConn.CloseRead:" ,
) )
2021-01-20 11:02:46 +01:00
opts := projectOptions { }
2021-06-22 11:44:56 +02:00
var (
2022-11-30 12:08:26 +01:00
ansi string
noAnsi bool
verbose bool
version bool
parallel int
2021-06-22 11:44:56 +02:00
)
2022-07-13 02:53:08 +02:00
c := & cobra . Command {
2021-02-03 14:02:12 +01:00
Short : "Docker Compose" ,
2021-12-02 11:03:38 +01:00
Use : PluginName ,
2021-02-03 14:02:12 +01:00
TraverseChildren : true ,
2022-07-13 02:53:08 +02:00
// By default (no Run/RunE in parent c) for typos in subcommands, cobra displays the help of parent c but exit(0) !
2021-04-06 23:19:55 +02:00
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
if len ( args ) == 0 {
return cmd . Help ( )
}
2021-10-03 11:11:36 +02:00
if version {
return versionCommand ( ) . Execute ( )
}
2021-04-06 23:19:55 +02:00
_ = cmd . Help ( )
2021-04-15 12:43:18 +02:00
return dockercli . StatusError {
2021-06-15 09:57:38 +02:00
StatusCode : compose . CommandSyntaxFailure . ExitCode ,
2021-04-15 12:43:18 +02:00
Status : fmt . Sprintf ( "unknown docker command: %q" , "compose " + args [ 0 ] ) ,
}
2021-04-06 23:19:55 +02:00
} ,
2020-12-07 15:11:22 +01:00
PersistentPreRunE : func ( cmd * cobra . Command , args [ ] string ) error {
2022-05-30 17:30:30 +02:00
err := setEnvWithDotEnv ( & opts )
if err != nil {
return err
}
2021-03-04 19:21:42 +01:00
parent := cmd . Root ( )
2021-09-30 09:07:19 +02:00
if parent != nil {
2021-09-29 23:45:11 +02:00
parentPrerun := parent . PersistentPreRunE
if parentPrerun != nil {
err := parentPrerun ( cmd , args )
if err != nil {
return err
}
2021-03-04 19:21:42 +01:00
}
}
2021-03-19 17:24:56 +01:00
if noAnsi {
if ansi != "auto" {
2021-03-19 17:48:06 +01:00
return errors . New ( ` cannot specify DEPRECATED "--no-ansi" and "--ansi". Please use only "--ansi" ` )
2021-03-19 17:24:56 +01:00
}
ansi = "never"
2022-03-24 15:56:06 +01:00
fmt . Fprint ( os . Stderr , "option '--no-ansi' is DEPRECATED ! Please use '--ansi' instead.\n" )
2021-03-19 17:24:56 +01:00
}
2021-06-22 11:44:56 +02:00
if verbose {
logrus . SetLevel ( logrus . TraceLevel )
}
2021-03-08 10:22:24 +01:00
formatter . SetANSIMode ( ansi )
2022-03-08 12:15:59 +01:00
switch ansi {
case "never" :
progress . Mode = progress . ModePlain
case "tty" :
progress . Mode = progress . ModeTTY
}
2021-03-01 12:43:00 -03:00
if opts . WorkDir != "" {
if opts . ProjectDir != "" {
2021-03-19 17:48:06 +01:00
return errors . New ( ` cannot specify DEPRECATED "--workdir" and "--project-directory". Please use only "--project-directory" instead ` )
2021-03-01 12:43:00 -03:00
}
opts . ProjectDir = opts . WorkDir
2021-03-19 17:24:56 +01:00
fmt . Fprint ( os . Stderr , aec . Apply ( "option '--workdir' is DEPRECATED at root level! Please use '--project-directory' instead.\n" , aec . RedF ) )
2021-03-01 12:43:00 -03:00
}
2022-12-13 14:01:51 +01:00
if opts . EnvFile != "" && ! filepath . IsAbs ( opts . EnvFile ) {
opts . EnvFile , err = filepath . Abs ( opts . EnvFile )
if err != nil {
return err
}
}
2022-11-30 12:08:26 +01:00
if parallel > 0 {
backend . MaxConcurrency ( parallel )
}
2020-12-07 15:11:22 +01:00
return nil
} ,
2020-05-05 10:58:24 +02:00
}
2020-05-14 21:04:38 +02:00
2022-07-13 02:53:08 +02:00
c . AddCommand (
2021-06-07 14:21:55 +02:00
upCommand ( & opts , backend ) ,
2021-06-07 11:21:57 +02:00
downCommand ( & opts , backend ) ,
2021-04-14 11:47:02 +02:00
startCommand ( & opts , backend ) ,
restartCommand ( & opts , backend ) ,
stopCommand ( & opts , backend ) ,
psCommand ( & opts , backend ) ,
2021-08-31 15:41:20 +02:00
listCommand ( backend ) ,
logsCommand ( & opts , backend ) ,
2021-04-14 11:47:02 +02:00
convertCommand ( & opts , backend ) ,
killCommand ( & opts , backend ) ,
2022-03-15 15:46:27 +01:00
runCommand ( & opts , dockerCli , backend ) ,
2021-04-14 11:47:02 +02:00
removeCommand ( & opts , backend ) ,
2022-03-15 15:46:27 +01:00
execCommand ( & opts , dockerCli , backend ) ,
2021-04-14 11:47:02 +02:00
pauseCommand ( & opts , backend ) ,
unpauseCommand ( & opts , backend ) ,
topCommand ( & opts , backend ) ,
eventsCommand ( & opts , backend ) ,
portCommand ( & opts , backend ) ,
imagesCommand ( & opts , backend ) ,
2021-04-02 13:18:51 +02:00
versionCommand ( ) ,
2021-08-31 15:41:20 +02:00
buildCommand ( & opts , backend ) ,
pushCommand ( & opts , backend ) ,
pullCommand ( & opts , backend ) ,
createCommand ( & opts , backend ) ,
copyCommand ( & opts , backend ) ,
2020-05-05 10:58:24 +02:00
)
2022-07-13 02:53:08 +02:00
c . Flags ( ) . SetInterspersed ( false )
opts . addProjectFlags ( c . Flags ( ) )
2022-09-26 19:21:45 +02:00
c . RegisterFlagCompletionFunc ( //nolint:errcheck
"project-name" ,
completeProjectNames ( backend ) ,
)
c . RegisterFlagCompletionFunc ( //nolint:errcheck
"file" ,
func ( cmd * cobra . Command , args [ ] string , toComplete string ) ( [ ] string , cobra . ShellCompDirective ) {
return [ ] string { "yaml" , "yml" } , cobra . ShellCompDirectiveFilterFileExt
} ,
)
2022-07-13 02:53:08 +02:00
c . Flags ( ) . StringVar ( & ansi , "ansi" , "auto" , ` Control when to print ANSI control characters ("never"|"always"|"auto") ` )
2022-11-30 12:08:26 +01:00
c . Flags ( ) . IntVar ( & parallel , "parallel" , - 1 , ` Control max parallelism, -1 for unlimited ` )
2022-07-13 02:53:08 +02:00
c . Flags ( ) . BoolVarP ( & version , "version" , "v" , false , "Show the Docker Compose version information" )
c . Flags ( ) . MarkHidden ( "version" ) //nolint:errcheck
c . Flags ( ) . BoolVar ( & noAnsi , "no-ansi" , false , ` Do not print ANSI control characters (DEPRECATED) ` )
c . Flags ( ) . MarkHidden ( "no-ansi" ) //nolint:errcheck
c . Flags ( ) . BoolVar ( & verbose , "verbose" , false , "Show more output" )
c . Flags ( ) . MarkHidden ( "verbose" ) //nolint:errcheck
return c
2020-05-05 15:37:12 +02:00
}
2022-05-30 17:30:30 +02:00
func setEnvWithDotEnv ( prjOpts * projectOptions ) error {
options , err := prjOpts . toProjectOptions ( )
if err != nil {
return compose . WrapComposeError ( err )
}
workingDir , err := options . GetWorkingDir ( )
if err != nil {
return err
}
envFromFile , err := cli . GetEnvFromFile ( composegoutils . GetAsEqualsMap ( os . Environ ( ) ) , workingDir , options . EnvFile )
if err != nil {
return err
}
for k , v := range envFromFile {
2022-08-17 22:36:25 +02:00
if _ , ok := os . LookupEnv ( k ) ; ! ok { // Precedence to OS Env
if err := os . Setenv ( k , v ) ; err != nil {
return err
}
2022-05-30 17:30:30 +02:00
}
}
return nil
}