From d5dc76ec3ef64632d9f7ea71f06f33b5f8aef9fe Mon Sep 17 00:00:00 2001 From: dwelch-spike <53876192+dwelch-spike@users.noreply.github.com> Date: Tue, 12 Dec 2023 16:12:30 -0800 Subject: [PATCH] fix: properly read stdin in convert prerun (#28) --- cmd/convert.go | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/cmd/convert.go b/cmd/convert.go index 266aa22..2c4b375 100644 --- a/cmd/convert.go +++ b/cmd/convert.go @@ -35,6 +35,7 @@ func init() { var convertCmd = newConvertCmd() func newConvertCmd() *cobra.Command { + var cfgData []byte res := &cobra.Command{ Use: "convert [flags] ", Short: "Convert between yaml and Aerospike config format.", @@ -91,11 +92,6 @@ func newConvertCmd() *cobra.Command { logger.Debugf("Processing flag format value=%v", srcFormat) - fdata, err := os.ReadFile(srcPath) - if err != nil { - return err - } - var outFmt asconf.Format switch srcFormat { case asconf.AsConfig: @@ -109,14 +105,14 @@ func newConvertCmd() *cobra.Command { // if the version option is empty, // try populating from the metadata if version == "" { - version, err = getMetaDataItem(fdata, metaKeyAerospikeVersion) + version, err = getMetaDataItem(cfgData, metaKeyAerospikeVersion) if err != nil && !force { return errors.Join(errMissingAerospikeVersion, err) } } conf, err := asconf.NewAsconf( - fdata, + cfgData, srcFormat, outFmt, version, @@ -142,7 +138,7 @@ func newConvertCmd() *cobra.Command { // prepend metadata to the config output mtext, err := genMetaDataText(metaDataArgs{ - src: fdata, + src: cfgData, aerospikeVersion: version, asconfigVersion: VERSION, }) @@ -215,7 +211,15 @@ func newConvertCmd() *cobra.Command { return err } - cfgData, err := os.ReadFile(args[0]) + // read stdin by default + var srcPath string + if len(args) == 0 { + srcPath = os.Stdin.Name() + } else { + srcPath = args[0] + } + + cfgData, err = os.ReadFile(srcPath) if err != nil { return err }