diff --git a/api/progress/plain.go b/api/progress/plain.go index 3280bee1f..75d4168d3 100644 --- a/api/progress/plain.go +++ b/api/progress/plain.go @@ -37,7 +37,7 @@ func (p *plainWriter) Start(ctx context.Context) error { } func (p *plainWriter) Event(e Event) { - fmt.Println(e.ID, e.Text, e.StatusText) + fmt.Fprintln(p.out, e.ID, e.Text, e.StatusText) } func (p *plainWriter) Stop() { diff --git a/cli/cmd/compose/build.go b/cli/cmd/compose/build.go index 29bc29f62..622b753fa 100644 --- a/cli/cmd/compose/build.go +++ b/cli/cmd/compose/build.go @@ -18,6 +18,7 @@ package compose import ( "context" + "os" "github.com/spf13/cobra" @@ -28,20 +29,29 @@ import ( type buildOptions struct { *projectOptions composeOptions + quiet bool } func buildCommand(p *projectOptions) *cobra.Command { opts := buildOptions{ projectOptions: p, } - buildCmd := &cobra.Command{ + cmd := &cobra.Command{ Use: "build [SERVICE...]", Short: "Build or rebuild services", RunE: func(cmd *cobra.Command, args []string) error { + if opts.quiet { + devnull, err := os.Open(os.DevNull) + if err != nil { + return err + } + os.Stdout = devnull + } return runBuild(cmd.Context(), opts, args) }, } - return buildCmd + cmd.Flags().BoolVarP(&opts.quiet, "quiet", "q", false, "Don't print anything to STDOUT") + return cmd } func runBuild(ctx context.Context, opts buildOptions, services []string) error { diff --git a/cli/cmd/compose/convert.go b/cli/cmd/compose/convert.go index e72bea816..17aef2403 100644 --- a/cli/cmd/compose/convert.go +++ b/cli/cmd/compose/convert.go @@ -17,8 +17,11 @@ package compose import ( + "bufio" "context" "fmt" + "io" + "os" "github.com/docker/compose-cli/api/compose" @@ -31,6 +34,7 @@ type convertOptions struct { *projectOptions Format string Output string + quiet bool } var addFlagsFuncs []func(cmd *cobra.Command, opts *convertOptions) @@ -39,22 +43,31 @@ func convertCommand(p *projectOptions) *cobra.Command { opts := convertOptions{ projectOptions: p, } - convertCmd := &cobra.Command{ + cmd := &cobra.Command{ Aliases: []string{"config"}, Use: "convert SERVICES", Short: "Converts the compose file to platform's canonical format", RunE: func(cmd *cobra.Command, args []string) error { + if opts.quiet { + devnull, err := os.Open(os.DevNull) + if err != nil { + return err + } + os.Stdout = devnull + } + opts.Output = os.DevNull return runConvert(cmd.Context(), opts, args) }, } - flags := convertCmd.Flags() + flags := cmd.Flags() flags.StringVar(&opts.Format, "format", "yaml", "Format the output. Values: [yaml | json]") + flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only validate the configuration, don't print anything.") // add flags for hidden backends for _, f := range addFlagsFuncs { - f(convertCmd, &opts) + f(cmd, &opts) } - return convertCmd + return cmd } func runConvert(ctx context.Context, opts convertOptions, services []string) error { @@ -76,9 +89,19 @@ func runConvert(ctx context.Context, opts convertOptions, services []string) err if err != nil { return err } + + if opts.quiet { + return nil + } + + var out io.Writer = os.Stdout if opts.Output != "" { - fmt.Print("model saved to ") + file, err := os.Create(opts.Output) + if err != nil { + return err + } + out = bufio.NewWriter(file) } - fmt.Println(string(json)) - return nil + _, err = fmt.Fprint(out, string(json)) + return err } diff --git a/cli/cmd/compose/pull.go b/cli/cmd/compose/pull.go index 3617058e3..3dca9eb73 100644 --- a/cli/cmd/compose/pull.go +++ b/cli/cmd/compose/pull.go @@ -18,7 +18,6 @@ package compose import ( "context" - "github.com/spf13/cobra" "github.com/docker/compose-cli/api/client" @@ -28,20 +27,22 @@ import ( type pullOptions struct { *projectOptions composeOptions + quiet bool } func pullCommand(p *projectOptions) *cobra.Command { opts := pullOptions{ projectOptions: p, } - pullCmd := &cobra.Command{ + cmd := &cobra.Command{ Use: "pull [SERVICE...]", Short: "Pull service images", RunE: func(cmd *cobra.Command, args []string) error { return runPull(cmd.Context(), opts, args) }, } - return pullCmd + cmd.Flags().BoolVarP(&opts.quiet, "quiet", "q", false, "Pull without printing progress information") + return cmd } func runPull(ctx context.Context, opts pullOptions, services []string) error { @@ -55,6 +56,10 @@ func runPull(ctx context.Context, opts pullOptions, services []string) error { return err } + if opts.quiet { + return c.ComposeService().Pull(ctx, project) + } + _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { return "", c.ComposeService().Pull(ctx, project) })