From 7e87c1902674dfa1516e94c426fb9984c27fcf09 Mon Sep 17 00:00:00 2001 From: Integralist Date: Wed, 17 May 2023 12:21:03 +0100 Subject: [PATCH 1/3] fix(kvstoreentry): support JSON output for bulk processing --- pkg/commands/kvstoreentry/create.go | 33 +++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/pkg/commands/kvstoreentry/create.go b/pkg/commands/kvstoreentry/create.go index 3d28a3a84..37c111eb8 100644 --- a/pkg/commands/kvstoreentry/create.go +++ b/pkg/commands/kvstoreentry/create.go @@ -136,12 +136,20 @@ func (c *CreateCommand) ProcessStdin(in io.Reader, out io.Writer) error { if in == nil || text.IsTTY(in) { return fsterr.ErrNoSTDINData } + if c.Globals.Verbose() { + in = io.TeeReader(in, out) + } return c.CallBatchEndpoint(in, out) } // ProcessFile streams a JSON file content to the batch API endpoint. func (c *CreateCommand) ProcessFile(out io.Writer) error { - f, err := os.Open(c.filePath) + var ( + err error + f io.ReadCloser + ) + + f, err = os.Open(c.filePath) if err != nil { c.Globals.ErrLog.Add(err) return err @@ -149,7 +157,13 @@ func (c *CreateCommand) ProcessFile(out io.Writer) error { defer func() { _ = f.Close() }() - return c.CallBatchEndpoint(f, out) + + var in io.Reader + in = f + if c.Globals.Verbose() { + in = io.TeeReader(f, out) + } + return c.CallBatchEndpoint(in, out) } // ProcessDir concurrently reads files from the given directory structure and @@ -336,11 +350,26 @@ func (c *CreateCommand) PromptWindowsUser(in io.Reader, out io.Writer) (bool, er // CallBatchEndpoint calls the batch API endpoint. func (c *CreateCommand) CallBatchEndpoint(in io.Reader, out io.Writer) error { + type result struct { + Success bool `json:"success"` + } + if err := c.Globals.APIClient.BatchModifyKVStoreKey(&fastly.BatchModifyKVStoreKeyInput{ ID: c.Input.ID, Body: in, }); err != nil { c.Globals.ErrLog.Add(err) + + if c.JSONOutput.Enabled { + _, err := c.WriteJSON(out, result{Success: false}) + return err + } + + return err + } + + if c.JSONOutput.Enabled { + _, err := c.WriteJSON(out, result{Success: true}) return err } From 35a8c3e2286daa66ef54b3fbf40e78aad469ed80 Mon Sep 17 00:00:00 2001 From: Integralist Date: Wed, 17 May 2023 12:48:34 +0100 Subject: [PATCH 2/3] fix(kvstoreentry): support verbose output for --dir feature --- pkg/commands/kvstoreentry/create.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/pkg/commands/kvstoreentry/create.go b/pkg/commands/kvstoreentry/create.go index 37c111eb8..432afc755 100644 --- a/pkg/commands/kvstoreentry/create.go +++ b/pkg/commands/kvstoreentry/create.go @@ -215,13 +215,14 @@ func (c *CreateCommand) ProcessDir(in io.Reader, out io.Writer) error { if err != nil { return err } - fileLength := len(filteredFiles) + filesTotal := len(filteredFiles) msg := "%s %d of %d files" - spinner.Message(fmt.Sprintf(msg, "Processing", 0, fileLength) + "...") + spinner.Message(fmt.Sprintf(msg, "Processing", 0, filesTotal) + "...") base := filepath.Base(path) processed := make(chan struct{}, c.dirConcurrency) sem := make(chan struct{}, c.dirConcurrency) + filesVerboseOutput := make(chan string, filesTotal) var ( processingErrors []ProcessErr @@ -236,7 +237,7 @@ func (c *CreateCommand) ProcessDir(in io.Reader, out io.Writer) error { go func() { for range processed { atomic.AddUint64(&filesProcessed, 1) - spinner.Message(fmt.Sprintf(msg, "Processing", filesProcessed, fileLength) + "...") + spinner.Message(fmt.Sprintf(msg, "Processing", filesProcessed, filesTotal) + "...") } }() @@ -257,6 +258,10 @@ func (c *CreateCommand) ProcessDir(in io.Reader, out io.Writer) error { index := strings.Index(dir, base) filename = filepath.Join(dir[index:], filename) + if c.Globals.Verbose() { + filesVerboseOutput <- filename + } + // G304 (CWE-22): Potential file inclusion via variable // #nosec f, err := os.Open(filePath) @@ -314,12 +319,20 @@ func (c *CreateCommand) ProcessDir(in io.Reader, out io.Writer) error { wg.Wait() - spinner.StopMessage(fmt.Sprintf(msg, "Processed", atomic.LoadUint64(&filesProcessed)-uint64(len(processingErrors)), fileLength)) + spinner.StopMessage(fmt.Sprintf(msg, "Processed", atomic.LoadUint64(&filesProcessed)-uint64(len(processingErrors)), filesTotal)) err = spinner.Stop() if err != nil { return err } + if c.Globals.Verbose() { + close(filesVerboseOutput) + text.Break(out) + for filename := range filesVerboseOutput { + fmt.Println(filename) + } + } + if len(processingErrors) == 0 { text.Success(out, "Inserted %d keys into KV Store", len(filteredFiles)) return nil From f72ed4a28a59dc50a271c3c2f96296bcd59bb885 Mon Sep 17 00:00:00 2001 From: Integralist Date: Wed, 17 May 2023 15:42:42 +0100 Subject: [PATCH 3/3] refactor(kvstoreentry/create): avoid unnecessary io.ReadCloser --- pkg/commands/kvstoreentry/create.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/pkg/commands/kvstoreentry/create.go b/pkg/commands/kvstoreentry/create.go index 432afc755..7482fcce2 100644 --- a/pkg/commands/kvstoreentry/create.go +++ b/pkg/commands/kvstoreentry/create.go @@ -144,12 +144,7 @@ func (c *CreateCommand) ProcessStdin(in io.Reader, out io.Writer) error { // ProcessFile streams a JSON file content to the batch API endpoint. func (c *CreateCommand) ProcessFile(out io.Writer) error { - var ( - err error - f io.ReadCloser - ) - - f, err = os.Open(c.filePath) + f, err := os.Open(c.filePath) if err != nil { c.Globals.ErrLog.Add(err) return err @@ -158,8 +153,7 @@ func (c *CreateCommand) ProcessFile(out io.Writer) error { _ = f.Close() }() - var in io.Reader - in = f + var in io.Reader = f if c.Globals.Verbose() { in = io.TeeReader(f, out) }