save-load-export: clear cli-parsing default

...in order to silence Cobra's usually-helpful "(default xxx)"
message.

Initialization is now done in code, by testing for empty string
and setting that to /dev/std{in,out} as appropriate; make special
note of load.go where there's mild duplication between a local
variable and cliconfig.

Signed-off-by: Ed Santiago <santiago@redhat.com>
This commit is contained in:
Ed Santiago
2019-03-14 08:47:15 -06:00
parent 8f82edbcb3
commit 1e124306db
3 changed files with 13 additions and 9 deletions

View File

@ -36,7 +36,7 @@ func init() {
exportCommand.SetHelpTemplate(HelpTemplate()) exportCommand.SetHelpTemplate(HelpTemplate())
exportCommand.SetUsageTemplate(UsageTemplate()) exportCommand.SetUsageTemplate(UsageTemplate())
flags := exportCommand.Flags() flags := exportCommand.Flags()
flags.StringVarP(&exportCommand.Output, "output", "o", "/dev/stdout", "Write to a file instead of terminal") flags.StringVarP(&exportCommand.Output, "output", "o", "", "Write to a specified file (default: stdout, which must be redirected)")
} }
// exportCmd saves a container to a tarball on disk // exportCmd saves a container to a tarball on disk
@ -60,15 +60,16 @@ func exportCmd(c *cliconfig.ExportValues) error {
} }
output := c.Output output := c.Output
if runtime.Remote && (output == "/dev/stdout" || len(output) == 0) { if runtime.Remote && len(output) == 0 {
return errors.New("remote client usage must specify an output file (-o)") return errors.New("remote client usage must specify an output file (-o)")
} }
if output == "/dev/stdout" { if len(output) == 0 {
file := os.Stdout file := os.Stdout
if logrus.IsTerminal(file) { if logrus.IsTerminal(file) {
return errors.Errorf("refusing to export to terminal. Use -o flag or redirect") return errors.Errorf("refusing to export to terminal. Use -o flag or redirect")
} }
output = "/dev/stdout"
} }
if err := parse.ValidateFileName(output); err != nil { if err := parse.ValidateFileName(output); err != nil {

View File

@ -34,7 +34,7 @@ func init() {
loadCommand.SetHelpTemplate(HelpTemplate()) loadCommand.SetHelpTemplate(HelpTemplate())
loadCommand.SetUsageTemplate(UsageTemplate()) loadCommand.SetUsageTemplate(UsageTemplate())
flags := loadCommand.Flags() flags := loadCommand.Flags()
flags.StringVarP(&loadCommand.Input, "input", "i", "/dev/stdin", "Read from archive file instead of from terminal") flags.StringVarP(&loadCommand.Input, "input", "i", "", "Read from specified archive file (default: stdin)")
flags.BoolVarP(&loadCommand.Quiet, "quiet", "q", false, "Suppress the output") flags.BoolVarP(&loadCommand.Quiet, "quiet", "q", false, "Suppress the output")
flags.StringVar(&loadCommand.SignaturePolicy, "signature-policy", "", "Pathname of signature policy file (not usually used)") flags.StringVar(&loadCommand.SignaturePolicy, "signature-policy", "", "Pathname of signature policy file (not usually used)")
@ -64,7 +64,10 @@ func loadCmd(c *cliconfig.LoadValues) error {
if runtime.Remote && len(input) == 0 { if runtime.Remote && len(input) == 0 {
return errors.New("the remote client requires you to load via -i and a tarball") return errors.New("the remote client requires you to load via -i and a tarball")
} }
if input == "/dev/stdin" { if len(input) == 0 {
input = "/dev/stdin"
c.Input = input
fi, err := os.Stdin.Stat() fi, err := os.Stdin.Stat()
if err != nil { if err != nil {
return err return err

View File

@ -58,7 +58,7 @@ func init() {
flags := saveCommand.Flags() flags := saveCommand.Flags()
flags.BoolVar(&saveCommand.Compress, "compress", false, "Compress tarball image layers when saving to a directory using the 'dir' transport. (default is same compression type as source)") flags.BoolVar(&saveCommand.Compress, "compress", false, "Compress tarball image layers when saving to a directory using the 'dir' transport. (default is same compression type as source)")
flags.StringVar(&saveCommand.Format, "format", v2s2Archive, "Save image to oci-archive, oci-dir (directory with oci manifest type), docker-archive, docker-dir (directory with v2s2 manifest type)") flags.StringVar(&saveCommand.Format, "format", v2s2Archive, "Save image to oci-archive, oci-dir (directory with oci manifest type), docker-archive, docker-dir (directory with v2s2 manifest type)")
flags.StringVarP(&saveCommand.Output, "output", "o", "/dev/stdout", "Write to a file instead of terminal") flags.StringVarP(&saveCommand.Output, "output", "o", "", "Write to a specified file (default: stdout, which must be redirected)")
flags.BoolVarP(&saveCommand.Quiet, "quiet", "q", false, "Suppress the output") flags.BoolVarP(&saveCommand.Quiet, "quiet", "q", false, "Suppress the output")
} }
@ -79,14 +79,14 @@ func saveCmd(c *cliconfig.SaveValues) error {
return errors.Errorf("--compress can only be set when --format is either 'oci-dir' or 'docker-dir'") return errors.Errorf("--compress can only be set when --format is either 'oci-dir' or 'docker-dir'")
} }
output := c.Output if len(c.Output) == 0 {
if output == "/dev/stdout" {
fi := os.Stdout fi := os.Stdout
if logrus.IsTerminal(fi) { if logrus.IsTerminal(fi) {
return errors.Errorf("refusing to save to terminal. Use -o flag or redirect") return errors.Errorf("refusing to save to terminal. Use -o flag or redirect")
} }
c.Output = "/dev/stdout"
} }
if err := parse.ValidateFileName(output); err != nil { if err := parse.ValidateFileName(c.Output); err != nil {
return err return err
} }
return runtime.SaveImage(getContext(), c) return runtime.SaveImage(getContext(), c)