mirror of https://github.com/docker/compose.git
introduce config --quiet
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
parent
c74cec9ae3
commit
da7498949e
|
@ -17,8 +17,11 @@
|
||||||
package compose
|
package compose
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/docker/compose-cli/api/compose"
|
"github.com/docker/compose-cli/api/compose"
|
||||||
|
|
||||||
|
@ -31,6 +34,7 @@ type convertOptions struct {
|
||||||
*projectOptions
|
*projectOptions
|
||||||
Format string
|
Format string
|
||||||
Output string
|
Output string
|
||||||
|
quiet bool
|
||||||
}
|
}
|
||||||
|
|
||||||
var addFlagsFuncs []func(cmd *cobra.Command, opts *convertOptions)
|
var addFlagsFuncs []func(cmd *cobra.Command, opts *convertOptions)
|
||||||
|
@ -39,22 +43,31 @@ func convertCommand(p *projectOptions) *cobra.Command {
|
||||||
opts := convertOptions{
|
opts := convertOptions{
|
||||||
projectOptions: p,
|
projectOptions: p,
|
||||||
}
|
}
|
||||||
convertCmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Aliases: []string{"config"},
|
Aliases: []string{"config"},
|
||||||
Use: "convert SERVICES",
|
Use: "convert SERVICES",
|
||||||
Short: "Converts the compose file to platform's canonical format",
|
Short: "Converts the compose file to platform's canonical format",
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
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)
|
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.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
|
// add flags for hidden backends
|
||||||
for _, f := range addFlagsFuncs {
|
for _, f := range addFlagsFuncs {
|
||||||
f(convertCmd, &opts)
|
f(cmd, &opts)
|
||||||
}
|
}
|
||||||
return convertCmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func runConvert(ctx context.Context, opts convertOptions, services []string) error {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
var out io.Writer = os.Stdout
|
||||||
if opts.Output != "" {
|
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))
|
_, err = fmt.Fprint(out, string(json))
|
||||||
return nil
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue