Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1313 from docker/quiet
Browse files Browse the repository at this point in the history
  • Loading branch information
ndeloof authored Feb 17, 2021
2 parents 5e7203d + 057f23e commit 65fc4ff
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 13 deletions.
2 changes: 1 addition & 1 deletion api/progress/plain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
14 changes: 12 additions & 2 deletions cli/cmd/compose/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package compose

import (
"context"
"os"

"github.com/spf13/cobra"

Expand All @@ -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 {
Expand Down
37 changes: 30 additions & 7 deletions cli/cmd/compose/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
package compose

import (
"bufio"
"context"
"fmt"
"io"
"os"

"github.com/docker/compose-cli/api/compose"

Expand All @@ -31,6 +34,7 @@ type convertOptions struct {
*projectOptions
Format string
Output string
quiet bool
}

var addFlagsFuncs []func(cmd *cobra.Command, opts *convertOptions)
Expand All @@ -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 {
Expand All @@ -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
}
11 changes: 8 additions & 3 deletions cli/cmd/compose/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package compose

import (
"context"

"github.com/spf13/cobra"

"github.com/docker/compose-cli/api/client"
Expand All @@ -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 {
Expand All @@ -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)
})
Expand Down

0 comments on commit 65fc4ff

Please sign in to comment.