From da7498949eb07b38f69b4a7009ace642fcf6e8f3 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Tue, 16 Feb 2021 12:23:48 +0100 Subject: [PATCH] introduce config --quiet Signed-off-by: Nicolas De Loof --- cli/cmd/compose/convert.go | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/cli/cmd/compose/convert.go b/cli/cmd/compose/convert.go index e72bea816..e461179a5 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,14 @@ func runConvert(ctx context.Context, opts convertOptions, services []string) err if err != nil { return err } + 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 }