Skip to content

Commit

Permalink
Allow file for backenaction data payload. (#53)
Browse files Browse the repository at this point in the history
Co-authored-by: Elblinator <[email protected]>
  • Loading branch information
rrenkert and Elblinator authored Jan 8, 2025
1 parent 1ceed7d commit a75e691
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
46 changes: 31 additions & 15 deletions backendaction/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,40 @@ func (o Options) Run(ctx context.Context, cfg client.Config) error {
o.Content = string(stdinContent)
}

actions := make([]string, o.Amount)
for i := 0; i < len(actions); i++ {
c := o.Content
c = strings.ReplaceAll(c, `\i`, strconv.Itoa(i+1))
c = strings.ReplaceAll(c, `\u`, uuid.New().String())
body := ""
if o.BodyFile != nil {
bodyFileContent, err := io.ReadAll(o.BodyFile)
if err != nil {
return fmt.Errorf("reading body file: %w", err)
}

actions[i] = c
}
body = fmt.Sprintf(
`[{
"action": "%s",
"data": %s
}]`,
o.Action,
bodyFileContent,
)
} else {
actions := make([]string, o.Amount)
for i := 0; i < len(actions); i++ {
c := o.Content
c = strings.ReplaceAll(c, `\i`, strconv.Itoa(i+1))
c = strings.ReplaceAll(c, `\u`, uuid.New().String())

body := fmt.Sprintf(
`[{
"action": "%s",
"data": [%s]
}]`,
o.Action,
strings.Join(actions, ","),
)
actions[i] = c
}

body = fmt.Sprintf(
`[{
"action": "%s",
"data": [%s]
}]`,
o.Action,
strings.Join(actions, ","),
)
}
req, err := http.NewRequestWithContext(
ctx,
"POST",
Expand Down
11 changes: 7 additions & 4 deletions backendaction/options.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package backendaction

import "os"

// Options is the meta information for the cli.
type Options struct {
Action string `arg:"" help:"Name of the action."`
Amount int `help:"Amount of action to be called." short:"n" default:"10"`
Content string `arg:"" help:"content of the action."`
Action string `arg:"" help:"Name of the action."`
Amount int `help:"Amount of action to be called." short:"n" default:"10"`
Content string `help:"content of the action." short:"c" default:""`
BodyFile *os.File `help:"File containing the 'data' array of action payload sent to the backend" short:"b"`
}

// Help returns the help message
Expand All @@ -19,5 +22,5 @@ If content is "-", then the content is read from stdin.
Example:
openslides-performance backend-action motion.create '{"meeting_id":1,"text":"hello world","title":"motion\u"}'`
openslides-performance backend-action motion.create -c '{"meeting_id":1,"text":"hello world","title":"motion\u"}'`
}

0 comments on commit a75e691

Please sign in to comment.