Skip to content

Commit

Permalink
pull OCI remote resource
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
  • Loading branch information
ndeloof committed Aug 30, 2023
1 parent 914f78b commit c74d06c
Show file tree
Hide file tree
Showing 33 changed files with 399 additions and 240 deletions.
9 changes: 5 additions & 4 deletions cmd/compose/alpha.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
package compose

import (
"github.com/docker/cli/cli/command"
"github.com/docker/compose/v2/pkg/api"
"github.com/spf13/cobra"
)

// alphaCommand groups all experimental subcommands
func alphaCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
func alphaCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *cobra.Command {
cmd := &cobra.Command{
Short: "Experimental commands",
Use: "alpha [COMMAND]",
Expand All @@ -30,9 +31,9 @@ func alphaCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
},
}
cmd.AddCommand(
watchCommand(p, backend),
vizCommand(p, backend),
publishCommand(p, backend),
watchCommand(p, dockerCli, backend),
vizCommand(p, dockerCli, backend),
publishCommand(p, dockerCli, backend),
)
return cmd
}
11 changes: 6 additions & 5 deletions cmd/compose/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/compose-spec/compose-go/loader"
"github.com/compose-spec/compose-go/types"
buildx "github.com/docker/buildx/util/progress"
"github.com/docker/cli/cli/command"
cliopts "github.com/docker/cli/opts"
ui "github.com/docker/compose/v2/pkg/progress"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -73,7 +74,7 @@ func (opts buildOptions) toAPIBuildOptions(services []string) (api.BuildOptions,
}, nil
}

func buildCommand(p *ProjectOptions, progress *string, backend api.Service) *cobra.Command {
func buildCommand(p *ProjectOptions, dockerCli command.Cli, progress *string, backend api.Service) *cobra.Command {
opts := buildOptions{
ProjectOptions: p,
}
Expand All @@ -98,9 +99,9 @@ func buildCommand(p *ProjectOptions, progress *string, backend api.Service) *cob
if cmd.Flags().Changed("progress") && opts.ssh == "" {
fmt.Fprint(os.Stderr, "--progress is a global compose flag, better use `docker compose --progress xx build ...")
}
return runBuild(ctx, backend, opts, args)
return runBuild(ctx, dockerCli, backend, opts, args)
}),
ValidArgsFunction: completeServiceNames(p),
ValidArgsFunction: completeServiceNames(dockerCli, p),
}
cmd.Flags().BoolVar(&opts.push, "push", false, "Push service images.")
cmd.Flags().BoolVarP(&opts.quiet, "quiet", "q", false, "Don't print anything to STDOUT")
Expand All @@ -124,8 +125,8 @@ func buildCommand(p *ProjectOptions, progress *string, backend api.Service) *cob
return cmd
}

func runBuild(ctx context.Context, backend api.Service, opts buildOptions, services []string) error {
project, err := opts.ToProject(services, cli.WithResolvedPaths(true))
func runBuild(ctx context.Context, dockerCli command.Cli, backend api.Service, opts buildOptions, services []string) error {
project, err := opts.ToProject(dockerCli, services, cli.WithResolvedPaths(true))
if err != nil {
return err
}
Expand Down
9 changes: 5 additions & 4 deletions cmd/compose/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"sort"
"strings"

"github.com/docker/cli/cli/command"
"github.com/docker/compose/v2/pkg/api"
"github.com/spf13/cobra"
)
Expand All @@ -33,9 +34,9 @@ func noCompletion() validArgsFn {
}
}

func completeServiceNames(p *ProjectOptions) validArgsFn {
func completeServiceNames(dockerCli command.Cli, p *ProjectOptions) validArgsFn {
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
project, err := p.ToProject(nil)
project, err := p.ToProject(dockerCli, nil)

Check warning on line 39 in cmd/compose/completion.go

View check run for this annotation

Codecov / codecov/patch

cmd/compose/completion.go#L39

Added line #L39 was not covered by tests
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}
Expand Down Expand Up @@ -67,9 +68,9 @@ func completeProjectNames(backend api.Service) func(cmd *cobra.Command, args []s
}
}

func completeProfileNames(p *ProjectOptions) validArgsFn {
func completeProfileNames(dockerCli command.Cli, p *ProjectOptions) validArgsFn {
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
project, err := p.ToProject(nil)
project, err := p.ToProject(dockerCli, nil)

Check warning on line 73 in cmd/compose/completion.go

View check run for this annotation

Codecov / codecov/patch

cmd/compose/completion.go#L73

Added line #L73 was not covered by tests
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}
Expand Down
95 changes: 53 additions & 42 deletions cmd/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,16 @@ import (
"strings"
"syscall"

"github.com/compose-spec/compose-go/dotenv"
buildx "github.com/docker/buildx/util/progress"
"github.com/docker/cli/cli/command"
"github.com/docker/compose/v2/pkg/remote"

"github.com/compose-spec/compose-go/cli"
"github.com/compose-spec/compose-go/dotenv"
"github.com/compose-spec/compose-go/types"
composegoutils "github.com/compose-spec/compose-go/utils"
"github.com/docker/buildx/util/logutil"
buildx "github.com/docker/buildx/util/progress"
dockercli "github.com/docker/cli/cli"
"github.com/docker/cli/cli-plugins/manager"
"github.com/docker/cli/cli/command"
"github.com/docker/compose/v2/pkg/remote"
"github.com/morikuni/aec"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -126,34 +125,22 @@ type ProjectFunc func(ctx context.Context, project *types.Project) error
type ProjectServicesFunc func(ctx context.Context, project *types.Project, services []string) error

// WithProject creates a cobra run command from a ProjectFunc based on configured project options and selected services
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 {
func (o *ProjectOptions) WithProject(fn ProjectFunc, dockerCli command.Cli) func(cmd *cobra.Command, args []string) error {
return o.WithServices(dockerCli, func(ctx context.Context, project *types.Project, services []string) error {
return fn(ctx, project)
})
}

// WithServices creates a cobra run command from a ProjectFunc based on configured project options and selected services
func (o *ProjectOptions) WithServices(fn ProjectServicesFunc) func(cmd *cobra.Command, args []string) error {
func (o *ProjectOptions) WithServices(dockerCli command.Cli, fn ProjectServicesFunc) func(cmd *cobra.Command, args []string) error {
return Adapt(func(ctx context.Context, args []string) error {
options := []cli.ProjectOptionsFn{
cli.WithResolvedPaths(true),
cli.WithDiscardEnvFile,
cli.WithContext(ctx),
}

enabled, err := remote.GitRemoteLoaderEnabled()
if err != nil {
return err
}
if enabled {
git, err := remote.NewGitRemoteLoader()
if err != nil {
return err
}
options = append(options, cli.WithResourceLoader(git))
}

project, err := o.ToProject(args, options...)
project, err := o.ToProject(dockerCli, args, options...)
if err != nil {
return err
}
Expand All @@ -173,11 +160,11 @@ func (o *ProjectOptions) addProjectFlags(f *pflag.FlagSet) {
_ = f.MarkHidden("workdir")
}

func (o *ProjectOptions) projectOrName(services ...string) (*types.Project, string, error) {
func (o *ProjectOptions) projectOrName(dockerCli command.Cli, services ...string) (*types.Project, string, error) {
name := o.ProjectName
var project *types.Project
if len(o.ConfigPaths) > 0 || o.ProjectName == "" {
p, err := o.ToProject(services, cli.WithDiscardEnvFile)
p, err := o.ToProject(dockerCli, services, cli.WithDiscardEnvFile)
if err != nil {
envProjectName := os.Getenv(ComposeProjectName)
if envProjectName != "" {
Expand All @@ -191,7 +178,7 @@ func (o *ProjectOptions) projectOrName(services ...string) (*types.Project, stri
return project, name, nil
}

func (o *ProjectOptions) toProjectName() (string, error) {
func (o *ProjectOptions) toProjectName(dockerCli command.Cli) (string, error) {
if o.ProjectName != "" {
return o.ProjectName, nil
}
Expand All @@ -201,14 +188,38 @@ func (o *ProjectOptions) toProjectName() (string, error) {
return envProjectName, nil
}

project, err := o.ToProject(nil)
project, err := o.ToProject(dockerCli, nil)

Check warning on line 191 in cmd/compose/compose.go

View check run for this annotation

Codecov / codecov/patch

cmd/compose/compose.go#L191

Added line #L191 was not covered by tests
if err != nil {
return "", err
}
return project.Name, nil
}

func (o *ProjectOptions) ToProject(services []string, po ...cli.ProjectOptionsFn) (*types.Project, error) {
func (o *ProjectOptions) ToProject(dockerCli command.Cli, services []string, po ...cli.ProjectOptionsFn) (*types.Project, error) {
enabled, err := remote.GitRemoteLoaderEnabled()
if err != nil {
return nil, err
}

Check warning on line 202 in cmd/compose/compose.go

View check run for this annotation

Codecov / codecov/patch

cmd/compose/compose.go#L201-L202

Added lines #L201 - L202 were not covered by tests
if enabled {
git, err := remote.NewGitRemoteLoader()
if err != nil {
return nil, err
}
po = append(po, cli.WithResourceLoader(git))

Check warning on line 208 in cmd/compose/compose.go

View check run for this annotation

Codecov / codecov/patch

cmd/compose/compose.go#L204-L208

Added lines #L204 - L208 were not covered by tests
}

enabled, err = remote.OCIRemoteLoaderEnabled()
if err != nil {
return nil, err
}

Check warning on line 214 in cmd/compose/compose.go

View check run for this annotation

Codecov / codecov/patch

cmd/compose/compose.go#L213-L214

Added lines #L213 - L214 were not covered by tests
if enabled {
git, err := remote.NewOCIRemoteLoader(dockerCli)
if err != nil {
return nil, err
}
po = append(po, cli.WithResourceLoader(git))

Check warning on line 220 in cmd/compose/compose.go

View check run for this annotation

Codecov / codecov/patch

cmd/compose/compose.go#L216-L220

Added lines #L216 - L220 were not covered by tests
}

options, err := o.toProjectOptions(po...)
if err != nil {
return nil, compose.WrapComposeError(err)
Expand Down Expand Up @@ -427,32 +438,32 @@ func RootCommand(dockerCli command.Cli, backend api.Service) *cobra.Command { //

c.AddCommand(
upCommand(&opts, dockerCli, backend),
downCommand(&opts, backend),
startCommand(&opts, backend),
restartCommand(&opts, backend),
stopCommand(&opts, backend),
downCommand(&opts, dockerCli, backend),
startCommand(&opts, dockerCli, backend),
restartCommand(&opts, dockerCli, backend),
stopCommand(&opts, dockerCli, backend),
psCommand(&opts, dockerCli, backend),
listCommand(dockerCli, backend),
logsCommand(&opts, dockerCli, backend),
configCommand(&opts, dockerCli, backend),
killCommand(&opts, backend),
killCommand(&opts, dockerCli, backend),
runCommand(&opts, dockerCli, backend),
removeCommand(&opts, backend),
removeCommand(&opts, dockerCli, backend),
execCommand(&opts, dockerCli, backend),
pauseCommand(&opts, backend),
unpauseCommand(&opts, backend),
pauseCommand(&opts, dockerCli, backend),
unpauseCommand(&opts, dockerCli, backend),
topCommand(&opts, dockerCli, backend),
eventsCommand(&opts, dockerCli, backend),
portCommand(&opts, dockerCli, backend),
imagesCommand(&opts, dockerCli, backend),
versionCommand(dockerCli),
buildCommand(&opts, &progress, backend),
pushCommand(&opts, backend),
pullCommand(&opts, backend),
createCommand(&opts, backend),
copyCommand(&opts, backend),
waitCommand(&opts, backend),
alphaCommand(&opts, backend),
buildCommand(&opts, dockerCli, &progress, backend),
pushCommand(&opts, dockerCli, backend),
pullCommand(&opts, dockerCli, backend),
createCommand(&opts, dockerCli, backend),
copyCommand(&opts, dockerCli, backend),
waitCommand(&opts, dockerCli, backend),
alphaCommand(&opts, dockerCli, backend),
)

c.Flags().SetInterspersed(false)
Expand All @@ -475,7 +486,7 @@ func RootCommand(dockerCli command.Cli, backend api.Service) *cobra.Command { //
)
c.RegisterFlagCompletionFunc( //nolint:errcheck
"profile",
completeProfileNames(&opts),
completeProfileNames(dockerCli, &opts),
)

c.Flags().StringVar(&progress, "progress", buildx.PrinterModeAuto, fmt.Sprintf(`Set type of progress output (%s)`, strings.Join(printerModes, ", ")))
Expand Down

0 comments on commit c74d06c

Please sign in to comment.