2020-05-14 21:13:07 +02:00
|
|
|
/*
|
2020-06-18 16:13:24 +02:00
|
|
|
Copyright 2020 Docker, Inc.
|
|
|
|
|
|
|
|
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-03 17:59:20 +02:00
|
|
|
"context"
|
|
|
|
|
2020-08-18 16:56:42 +02:00
|
|
|
"github.com/compose-spec/compose-go/cli"
|
2020-08-13 16:47:04 +02:00
|
|
|
|
2020-07-29 14:43:41 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2020-09-07 13:23:06 +02:00
|
|
|
"github.com/docker/compose-cli/api/client"
|
2020-08-21 17:24:53 +02:00
|
|
|
"github.com/docker/compose-cli/errdefs"
|
2020-05-05 10:58:24 +02:00
|
|
|
)
|
|
|
|
|
2020-08-18 09:40:57 +02:00
|
|
|
type composeOptions struct {
|
|
|
|
Name string
|
|
|
|
WorkingDir string
|
|
|
|
ConfigPaths []string
|
|
|
|
Environment []string
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
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-05-05 10:58:24 +02:00
|
|
|
func Command() *cobra.Command {
|
|
|
|
command := &cobra.Command{
|
|
|
|
Short: "Docker Compose",
|
|
|
|
Use: "compose",
|
2020-07-29 11:58:09 +02:00
|
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
2020-08-06 12:02:11 +02:00
|
|
|
return checkComposeSupport(cmd.Context())
|
2020-07-29 11:58:09 +02:00
|
|
|
},
|
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(
|
|
|
|
upCommand(),
|
|
|
|
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-05-05 10:58:24 +02:00
|
|
|
)
|
|
|
|
|
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-08-06 12:02:11 +02:00
|
|
|
func checkComposeSupport(ctx context.Context) error {
|
2020-08-20 09:06:17 +02:00
|
|
|
_, err := client.New(ctx)
|
2020-08-13 16:47:04 +02:00
|
|
|
if errdefs.IsNotFoundError(err) {
|
2020-08-20 09:06:17 +02:00
|
|
|
return errdefs.ErrNotImplemented
|
2020-08-03 17:59:20 +02:00
|
|
|
}
|
2020-08-20 09:06:17 +02:00
|
|
|
|
2020-08-13 16:47:04 +02:00
|
|
|
return err
|
2020-08-03 17:59:20 +02:00
|
|
|
}
|