Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions cmd/compose/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (
"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/cli/cli/command"
cliopts "github.com/docker/cli/opts"
"github.com/docker/compose/v2/cmd/display"
"github.com/docker/compose/v2/pkg/compose"
ui "github.com/docker/compose/v2/pkg/progress"
"github.com/spf13/cobra"

"github.com/docker/compose/v2/pkg/api"
Expand Down Expand Up @@ -67,8 +67,8 @@ func (opts buildOptions) toAPIBuildOptions(services []string) (api.BuildOptions,
builderName = os.Getenv("BUILDX_BUILDER")
}

uiMode := ui.Mode
if uiMode == ui.ModeJSON {
uiMode := display.Mode
if uiMode == display.ModeJSON {
uiMode = "rawjson"
}

Expand Down Expand Up @@ -100,7 +100,7 @@ func buildCommand(p *ProjectOptions, dockerCli command.Cli, backendOptions *Back
Short: "Build or rebuild services",
PreRunE: Adapt(func(ctx context.Context, args []string) error {
if opts.quiet {
ui.Mode = ui.ModeQuiet
display.Mode = display.ModeQuiet
devnull, err := os.Open(os.DevNull)
if err != nil {
return err
Expand Down Expand Up @@ -151,7 +151,7 @@ func buildCommand(p *ProjectOptions, dockerCli command.Cli, backendOptions *Back

func runBuild(ctx context.Context, dockerCli command.Cli, backendOptions *BackendOptions, opts buildOptions, services []string) error {
if opts.print {
backendOptions.Add(compose.WithEventProcessor(ui.NewQuietWriter()))
backendOptions.Add(compose.WithEventProcessor(display.Quiet()))
}
backend, err := compose.NewComposeService(dockerCli, backendOptions.Options...)
if err != nil {
Expand Down
69 changes: 41 additions & 28 deletions cmd/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ import (
"github.com/docker/cli/cli-plugins/metadata"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/pkg/kvfile"
"github.com/docker/compose/v2/cmd/display"
"github.com/docker/compose/v2/cmd/formatter"
"github.com/docker/compose/v2/internal/tracing"
"github.com/docker/compose/v2/pkg/api"
"github.com/docker/compose/v2/pkg/compose"
ui "github.com/docker/compose/v2/pkg/progress"
"github.com/docker/compose/v2/pkg/remote"
"github.com/docker/compose/v2/pkg/utils"
"github.com/morikuni/aec"
Expand Down Expand Up @@ -84,10 +84,16 @@ func rawEnv(r io.Reader, filename string, vars map[string]string, lookup func(ke
return nil
}

var stdioToStdout bool

func init() {
// compose evaluates env file values for interpolation
// `raw` format allows to load env_file with the same parser used by docker run --env-file
dotenv.RegisterFormat("raw", rawEnv)

if v, ok := os.LookupEnv("COMPOSE_STATUS_STDOUT"); ok {
stdioToStdout, _ = strconv.ParseBool(v)
}
}

// Command defines a compose CLI command as a func with args
Expand Down Expand Up @@ -116,7 +122,7 @@ func AdaptCmd(fn CobraCommand) func(cmd *cobra.Command, args []string) error {
StatusCode: 130,
}
}
if ui.Mode == ui.ModeJSON {
if display.Mode == display.ModeJSON {
err = makeJSONError(err)
}
return err
Expand Down Expand Up @@ -486,49 +492,49 @@ func RootCommand(dockerCli command.Cli, backendOptions *BackendOptions) *cobra.C
formatter.SetANSIMode(dockerCli, ansi)

if noColor, ok := os.LookupEnv("NO_COLOR"); ok && noColor != "" {
ui.NoColor()
display.NoColor()
formatter.SetANSIMode(dockerCli, formatter.Never)
}

switch ansi {
case "never":
ui.Mode = ui.ModePlain
display.Mode = display.ModePlain
case "always":
ui.Mode = ui.ModeTTY
display.Mode = display.ModeTTY
}

var ep ui.EventProcessor
var ep api.EventProcessor
switch opts.Progress {
case "", ui.ModeAuto:
case "", display.ModeAuto:
switch {
case ansi == "never":
ui.Mode = ui.ModePlain
ep = ui.NewPlainWriter(dockerCli.Err())
display.Mode = display.ModePlain
ep = display.Plain(dockerCli.Err())
case dockerCli.Out().IsTerminal():
ep = ui.NewTTYWriter(dockerCli.Err())
ep = display.Full(dockerCli.Err(), stdinfo(dockerCli))
default:
ep = ui.NewPlainWriter(dockerCli.Err())
ep = display.Plain(dockerCli.Err())
}
case ui.ModeTTY:
case display.ModeTTY:
if ansi == "never" {
return fmt.Errorf("can't use --progress tty while ANSI support is disabled")
}
ui.Mode = ui.ModeTTY
ep = ui.NewTTYWriter(dockerCli.Err())
display.Mode = display.ModeTTY
ep = display.Full(dockerCli.Err(), stdinfo(dockerCli))

case ui.ModePlain:
case display.ModePlain:
if ansi == "always" {
return fmt.Errorf("can't use --progress plain while ANSI support is forced")
}
ui.Mode = ui.ModePlain
ep = ui.NewPlainWriter(dockerCli.Err())
case ui.ModeQuiet, "none":
ui.Mode = ui.ModeQuiet
ep = ui.NewQuietWriter()
case ui.ModeJSON:
ui.Mode = ui.ModeJSON
display.Mode = display.ModePlain
ep = display.Plain(dockerCli.Err())
case display.ModeQuiet, "none":
display.Mode = display.ModeQuiet
ep = display.Quiet()
case display.ModeJSON:
display.Mode = display.ModeJSON
logrus.SetFormatter(&logrus.JSONFormatter{})
ep = ui.NewJSONWriter(dockerCli.Err())
ep = display.JSON(dockerCli.Err())
default:
return fmt.Errorf("unsupported --progress value %q", opts.Progress)
}
Expand Down Expand Up @@ -658,6 +664,13 @@ func RootCommand(dockerCli command.Cli, backendOptions *BackendOptions) *cobra.C
return c
}

func stdinfo(dockerCli command.Cli) io.Writer {
if stdioToStdout {
return dockerCli.Out()
}
return dockerCli.Err()
}

func setEnvWithDotEnv(opts ProjectOptions) error {
options, err := cli.NewProjectOptions(opts.ConfigPaths,
cli.WithWorkingDirectory(opts.ProjectDir),
Expand All @@ -683,9 +696,9 @@ func setEnvWithDotEnv(opts ProjectOptions) error {
}

var printerModes = []string{
ui.ModeAuto,
ui.ModeTTY,
ui.ModePlain,
ui.ModeJSON,
ui.ModeQuiet,
display.ModeAuto,
display.ModeTTY,
display.ModePlain,
display.ModeJSON,
display.ModeQuiet,
}
9 changes: 8 additions & 1 deletion cmd/compose/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package compose

import (
"context"
"errors"
"fmt"
"os"

"github.com/docker/cli/cli/command"
Expand Down Expand Up @@ -65,10 +67,15 @@ func runKill(ctx context.Context, dockerCli command.Cli, backendOptions *Backend
if err != nil {
return err
}
return backend.Kill(ctx, name, api.KillOptions{
err = backend.Kill(ctx, name, api.KillOptions{
RemoveOrphans: opts.removeOrphans,
Project: project,
Services: services,
Signal: opts.signal,
})
if errors.Is(err, api.ErrNoResources) {
_, _ = fmt.Fprintln(stdinfo(dockerCli), "No container to kill")
return nil
}
return err
}
13 changes: 6 additions & 7 deletions cmd/compose/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (

"github.com/docker/cli/cli/command"
"github.com/docker/compose/v2/pkg/compose"
"github.com/docker/compose/v2/pkg/progress"
"github.com/spf13/cobra"

"github.com/docker/compose/v2/cmd/formatter"
Expand Down Expand Up @@ -107,28 +106,28 @@ func runLogs(ctx context.Context, dockerCli command.Cli, backendOptions *Backend
var _ api.LogConsumer = &logConsumer{}

type logConsumer struct {
events progress.EventProcessor
events api.EventProcessor
}

func (l logConsumer) Log(containerName, message string) {
l.events.On(progress.Event{
l.events.On(api.Resource{
ID: containerName,
Text: message,
})
}

func (l logConsumer) Err(containerName, message string) {
l.events.On(progress.Event{
l.events.On(api.Resource{
ID: containerName,
Status: progress.Error,
Status: api.Error,
Text: message,
})
}

func (l logConsumer) Status(containerName, message string) {
l.events.On(progress.Event{
l.events.On(api.Resource{
ID: containerName,
Status: progress.Error,
Status: api.Error,
Text: message,
})
}
4 changes: 2 additions & 2 deletions cmd/compose/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ import (
"github.com/compose-spec/compose-go/v2/template"
"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/cli/cli/command"
"github.com/docker/compose/v2/cmd/display"
"github.com/docker/compose/v2/cmd/prompt"
"github.com/docker/compose/v2/internal/tracing"
ui "github.com/docker/compose/v2/pkg/progress"
)

func applyPlatforms(project *types.Project, buildForSinglePlatform bool) error {
Expand Down Expand Up @@ -247,7 +247,7 @@ func displayInterpolationVariables(writer io.Writer, varsInfo []varInfo) {

func displayLocationRemoteStack(dockerCli command.Cli, project *types.Project, options buildOptions) {
mainComposeFile := options.ProjectOptions.ConfigPaths[0] //nolint:staticcheck
if ui.Mode != ui.ModeQuiet && ui.Mode != ui.ModeJSON {
if display.Mode != display.ModeQuiet && display.Mode != display.ModeJSON {
_, _ = fmt.Fprintf(dockerCli.Out(), "Your compose stack %q is stored in %q\n", mainComposeFile, project.WorkingDir)
}
}
Expand Down
9 changes: 8 additions & 1 deletion cmd/compose/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package compose

import (
"context"
"errors"
"fmt"

"github.com/docker/cli/cli/command"
"github.com/docker/compose/v2/pkg/api"
Expand Down Expand Up @@ -70,11 +72,16 @@ func runRemove(ctx context.Context, dockerCli command.Cli, backendOptions *Backe
if err != nil {
return err
}
return backend.Remove(ctx, name, api.RemoveOptions{
err = backend.Remove(ctx, name, api.RemoveOptions{
Services: services,
Force: opts.force,
Volumes: opts.volumes,
Project: project,
Stop: opts.stop,
})
if errors.Is(err, api.ErrNoResources) {
_, _ = fmt.Fprintln(stdinfo(dockerCli), "No stopped containers")
return nil
}
return err
}
4 changes: 2 additions & 2 deletions cmd/compose/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
composecli "github.com/compose-spec/compose-go/v2/cli"
"github.com/compose-spec/compose-go/v2/dotenv"
"github.com/compose-spec/compose-go/v2/format"
"github.com/docker/compose/v2/cmd/display"
"github.com/docker/compose/v2/pkg/compose"
"github.com/docker/compose/v2/pkg/progress"
xprogress "github.com/moby/buildkit/util/progress/progressui"
"github.com/sirupsen/logrus"

Expand Down Expand Up @@ -193,7 +193,7 @@ func runCommand(p *ProjectOptions, dockerCli command.Cli, backendOptions *Backen
}

if options.quiet {
progress.Mode = progress.ModeQuiet
display.Mode = display.ModeQuiet
devnull, err := os.Open(os.DevNull)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions cmd/compose/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (

"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/cli/cli/command"
"github.com/docker/compose/v2/cmd/display"
"github.com/docker/compose/v2/pkg/compose"
ui "github.com/docker/compose/v2/pkg/progress"
xprogress "github.com/moby/buildkit/util/progress/progressui"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -341,7 +341,7 @@ func runUp(
WaitTimeout: timeout,
Watch: upOptions.watch,
Services: services,
NavigationMenu: upOptions.navigationMenu && ui.Mode != "plain" && dockerCli.In().IsTerminal(),
NavigationMenu: upOptions.navigationMenu && display.Mode != "plain" && dockerCli.In().IsTerminal(),
},
})
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/progress/colors.go → cmd/display/colors.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
limitations under the License.
*/

package progress
package display

import (
"github.com/morikuni/aec"
Expand Down
21 changes: 21 additions & 0 deletions cmd/display/dryrun.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
Copyright 2020 Docker Compose CLI authors
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.
*/

package display

const (
DRYRUN_PREFIX = " DRY-RUN MODE - "
)
Loading