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-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
|
|
|
|
2021-01-15 16:31:59 +01:00
|
|
|
"github.com/docker/compose-cli/api/context/store"
|
2020-05-05 10:58:24 +02:00
|
|
|
)
|
|
|
|
|
2021-02-23 09:35:26 +01:00
|
|
|
// Warning is a global warning to be displayed to user on command failure
|
|
|
|
var Warning string
|
|
|
|
|
2021-01-20 11:02:46 +01:00
|
|
|
type projectOptions struct {
|
2020-12-29 16:22:57 -03:00
|
|
|
ProjectName string
|
2021-02-02 11:42:19 +01:00
|
|
|
Profiles []string
|
2020-08-18 09:40:57 +02:00
|
|
|
ConfigPaths []string
|
2021-02-25 17:37:11 -03:00
|
|
|
ProjectDir string
|
2021-01-19 11:01:52 +01:00
|
|
|
EnvFile string
|
2020-10-12 11:03:43 +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.")
|
2021-02-25 17:37:11 -03:00
|
|
|
f.StringVar(&o.ProjectDir, "project-directory", "", "Specify an alternate working directory\n(default: the path of the Compose file)")
|
2020-08-18 09:40:57 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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-02-02 11:42:19 +01:00
|
|
|
func (o *projectOptions) toProject(services []string) (*types.Project, error) {
|
2021-01-14 15:59:57 +01:00
|
|
|
options, err := o.toProjectOptions()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-20 16:42:37 +02:00
|
|
|
|
|
|
|
project, err := cli.ProjectFromOptions(options)
|
|
|
|
if err != nil {
|
2021-01-14 15:59:57 +01:00
|
|
|
return nil, err
|
2020-08-20 16:42:37 +02:00
|
|
|
}
|
2021-02-02 11:42:19 +01:00
|
|
|
|
|
|
|
if len(services) != 0 {
|
2021-02-09 10:22:04 -03:00
|
|
|
s, err := project.GetServices(services...)
|
2021-02-02 11:42:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
o.Profiles = append(o.Profiles, s.GetProfiles()...)
|
|
|
|
}
|
|
|
|
|
|
|
|
project.ApplyProfiles(o.Profiles)
|
|
|
|
|
|
|
|
err = project.ForServices(services)
|
|
|
|
return project, err
|
2020-08-20 16:42:37 +02:00
|
|
|
}
|
|
|
|
|
2021-01-20 11:02:46 +01:00
|
|
|
func (o *projectOptions) toProjectOptions() (*cli.ProjectOptions, error) {
|
2020-08-18 09:40:57 +02:00
|
|
|
return cli.NewProjectOptions(o.ConfigPaths,
|
2021-01-19 11:01:52 +01:00
|
|
|
cli.WithEnvFile(o.EnvFile),
|
2021-01-04 09:56:09 +01:00
|
|
|
cli.WithDotEnv,
|
2021-02-22 08:27:55 +01:00
|
|
|
cli.WithOsEnv,
|
2021-02-25 17:37:11 -03:00
|
|
|
cli.WithWorkingDirectory(o.ProjectDir),
|
2020-12-29 16:22:57 -03:00
|
|
|
cli.WithName(o.ProjectName))
|
2020-08-18 09:40:57 +02:00
|
|
|
}
|
|
|
|
|
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 {
|
2021-01-20 11:02:46 +01:00
|
|
|
opts := projectOptions{}
|
2020-05-05 10:58:24 +02:00
|
|
|
command := &cobra.Command{
|
2021-02-03 14:02:12 +01:00
|
|
|
Short: "Docker Compose",
|
|
|
|
Use: "compose",
|
|
|
|
TraverseChildren: true,
|
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 {
|
2021-02-23 09:35:26 +01:00
|
|
|
Warning = "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 16:29:53 +01:00
|
|
|
}
|
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(
|
2021-01-20 11:02:46 +01:00
|
|
|
upCommand(&opts, contextType),
|
2021-02-17 15:22:26 +01:00
|
|
|
downCommand(&opts, contextType),
|
2021-01-26 18:42:49 -03:00
|
|
|
startCommand(&opts),
|
|
|
|
stopCommand(&opts),
|
2021-01-20 11:02:46 +01:00
|
|
|
psCommand(&opts),
|
2021-02-24 12:06:17 +01:00
|
|
|
listCommand(contextType),
|
2021-01-26 16:46:15 +01:00
|
|
|
logsCommand(&opts, contextType),
|
2021-01-20 11:02:46 +01:00
|
|
|
convertCommand(&opts),
|
2021-01-31 14:42:13 -03:00
|
|
|
killCommand(&opts),
|
2021-01-20 11:02:46 +01:00
|
|
|
runCommand(&opts),
|
2021-02-15 09:13:41 +01:00
|
|
|
removeCommand(&opts),
|
2021-02-15 09:56:34 +01:00
|
|
|
execCommand(&opts),
|
2021-02-22 10:25:40 +01:00
|
|
|
pauseCommand(&opts),
|
|
|
|
unpauseCommand(&opts),
|
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(
|
2021-01-20 11:02:46 +01:00
|
|
|
buildCommand(&opts),
|
|
|
|
pushCommand(&opts),
|
|
|
|
pullCommand(&opts),
|
2021-01-28 11:09:35 +01:00
|
|
|
createCommand(&opts),
|
2020-12-03 12:22:01 +01:00
|
|
|
)
|
2020-11-30 12:03:13 +01:00
|
|
|
}
|
2020-12-03 09:24:15 +01:00
|
|
|
command.Flags().SetInterspersed(false)
|
2021-02-03 14:02:12 +01:00
|
|
|
opts.addProjectFlags(command.Flags())
|
2020-05-14 21:04:38 +02:00
|
|
|
return command
|
2020-05-05 15:37:12 +02:00
|
|
|
}
|