Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(kvstoreentry): support JSON output for bulk processing #940

Merged
merged 3 commits into from
May 17, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 48 additions & 6 deletions pkg/commands/kvstoreentry/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,34 @@ 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
}
defer func() {
_ = f.Close()
}()
return c.CallBatchEndpoint(f, out)

var in io.Reader
Integralist marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down Expand Up @@ -201,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)
Integralist marked this conversation as resolved.
Show resolved Hide resolved
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
Expand All @@ -222,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) + "...")
}
}()

Expand All @@ -243,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)
Expand Down Expand Up @@ -300,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
Expand Down Expand Up @@ -336,11 +363,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
}

Expand Down