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 (
2020-12-07 15:11:22 +01:00
"fmt"
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"
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
2020-11-30 12:03:13 +01:00
"github.com/docker/compose-cli/context/store"
2020-05-05 10:58:24 +02:00
)
2020-08-18 09:40:57 +02:00
type composeOptions struct {
Name string
2020-09-21 17:05:49 +02:00
DomainName string
2020-08-18 09:40:57 +02:00
WorkingDir string
ConfigPaths [ ] string
Environment [ ] string
2020-09-28 17:08:27 +02:00
Format string
2020-10-06 10:56:00 +02:00
Detach bool
2020-12-10 10:22:18 +01:00
Build bool
2020-10-12 11:03:43 +02:00
Quiet bool
}
func addComposeCommonFlags ( f * pflag . FlagSet , opts * composeOptions ) {
f . StringVarP ( & opts . Name , "project-name" , "p" , "" , "Project name" )
f . StringVar ( & opts . Format , "format" , "" , "Format the output. Values: [pretty | json]. (Default: pretty)" )
f . BoolVarP ( & opts . Quiet , "quiet" , "q" , false , "Only display IDs" )
2020-08-18 09:40:57 +02:00
}
2020-08-20 16:42:37 +02:00
func ( o * composeOptions ) toProjectName ( ) ( string , error ) {
if o . Name != "" {
return o . Name , nil
}
options , err := o . toProjectOptions ( )
if err != nil {
return "" , err
}
project , err := cli . ProjectFromOptions ( options )
if err != nil {
return "" , err
}
return project . Name , nil
}
2020-08-18 09:40:57 +02:00
func ( o * composeOptions ) toProjectOptions ( ) ( * cli . ProjectOptions , error ) {
return cli . NewProjectOptions ( o . ConfigPaths ,
cli . WithOsEnv ,
2021-01-04 09:56:09 +01:00
cli . WithDotEnv ,
2020-08-18 09:40:57 +02:00
cli . WithEnv ( o . Environment ) ,
cli . WithWorkingDirectory ( o . WorkingDir ) ,
cli . WithName ( o . Name ) )
}
2020-05-05 16:27:22 +02:00
// Command returns the compose command with its child commands
2020-09-21 17:05:49 +02:00
func Command ( contextType string ) * cobra . Command {
2020-05-05 10:58:24 +02:00
command := & cobra . Command {
Short : "Docker Compose" ,
Use : "compose" ,
2020-12-07 15:11:22 +01:00
PersistentPreRunE : func ( cmd * cobra . Command , args [ ] string ) error {
2020-12-07 16:29:53 +01:00
if contextType == store . DefaultContextType || contextType == store . LocalContextType {
fmt . Println ( "The new 'docker compose' command is currently experimental. To provide feedback or request new features please open issues at https://github.com/docker/compose-cli" )
}
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
2020-05-05 10:58:24 +02:00
command . AddCommand (
2020-09-21 17:05:49 +02:00
upCommand ( contextType ) ,
2020-05-05 10:58:24 +02:00
downCommand ( ) ,
2020-08-05 16:32:51 +02:00
psCommand ( ) ,
2020-09-04 13:20:11 +02:00
listCommand ( ) ,
2020-08-05 16:32:51 +02:00
logsCommand ( ) ,
2020-08-13 16:47:04 +02:00
convertCommand ( ) ,
2020-12-03 09:24:15 +01:00
runCommand ( ) ,
2020-05-05 10:58:24 +02:00
)
2020-12-04 18:09:12 +01:00
if contextType == store . LocalContextType || contextType == store . DefaultContextType {
2020-12-01 11:34:48 +01:00
command . AddCommand (
buildCommand ( ) ,
2020-12-03 12:22:01 +01:00
pushCommand ( ) ,
pullCommand ( ) ,
)
2020-11-30 12:03:13 +01:00
}
2020-12-03 09:24:15 +01:00
command . Flags ( ) . SetInterspersed ( false )
2020-05-14 21:04:38 +02:00
return command
2020-05-05 15:37:12 +02:00
}
2020-08-03 17:59:20 +02:00
2020-11-27 14:47:53 +01:00
//
func filter ( project * types . Project , services [ ] string ) error {
if len ( services ) == 0 {
// All services
return nil
}
names := map [ string ] bool { }
err := addServiceNames ( project , services , names )
if err != nil {
return err
}
var filtered types . Services
for _ , s := range project . Services {
if _ , ok := names [ s . Name ] ; ok {
filtered = append ( filtered , s )
}
}
project . Services = filtered
return nil
}
func addServiceNames ( project * types . Project , services [ ] string , names map [ string ] bool ) error {
for _ , name := range services {
names [ name ] = true
service , err := project . GetService ( name )
if err != nil {
return err
}
err = addServiceNames ( project , service . GetDependencies ( ) , names )
if err != nil {
return err
}
}
return nil
}