Skip to content

Commit

Permalink
feat(lql): add option to generate empty lql file (#1335)
Browse files Browse the repository at this point in the history
* feat(lql.go): add option to generate empty lql file

adds a flag when running `lacework query run` to create an empty file in `$EDITOR`, rather than
using the default template

* style(lql.go): simplify boolean logic to not need true/false explicitly

pass the linter checks for Go

* style(lql.go): update formatting via gofmt

this adjusts the formatting to pass the linter

* test(query_run): add the new flag to expectations

includes new text for help
  • Loading branch information
emmaisadev authored Oct 13, 2023
1 parent 93923cd commit f0ad17a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
32 changes: 23 additions & 9 deletions cli/cmd/lql.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ import (

var (
queryCmdState = struct {
End string
File string
Limit int
Range string
Start string
URL string
ValidateOnly bool
FailOnCount string
End string
File string
Limit int
Range string
Start string
URL string
ValidateOnly bool
FailOnCount string
EmptyTemplate bool
// create, update validate from library
CURVFromLibrary string
}{}
Expand Down Expand Up @@ -191,6 +192,12 @@ func init() {
"fail_on_count", "",
"fail if the results from a query match the provided expression (e.g. '>0')",
)
// empty template flag
queryRunCmd.Flags().BoolVar(
&queryCmdState.EmptyTemplate,
"empty", false,
"start $EDITOR with empty file",
)
}

func setQuerySourceFlags(cmds ...*cobra.Command) {
Expand Down Expand Up @@ -295,7 +302,7 @@ func inputQueryFromEditor(action string) (query string, err error) {
FileName: "query*.yaml",
}

if action == "create" || action == "run" {
if (action == "create" || action == "run") && !queryCmdState.EmptyTemplate {
prompt.Default = `queryId: YourQueryID
queryText: |-
{
Expand All @@ -311,6 +318,10 @@ queryText: |-
}`
prompt.HideDefault = true
prompt.AppendDefault = true
} else if (action == "create" || action == "run") && queryCmdState.EmptyTemplate {
prompt.Default = ``
prompt.HideDefault = true
prompt.AppendDefault = true
}

err = survey.AskOne(prompt, &query)
Expand Down Expand Up @@ -402,6 +413,9 @@ func runQuery(cmd *cobra.Command, args []string) error {
if queryCmdState.ValidateOnly {
naFlag = "validate_only"
}
if queryCmdState.EmptyTemplate {
naFlag = "empty"
}
if naFlag != "" {
return errors.New(
fmt.Sprintf(
Expand Down
1 change: 1 addition & 0 deletions integration/test_resources/help/query_run
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Aliases:
run, execute

Flags:
--empty start $EDITOR with empty file
--end string end time for query (default "now")
--fail_on_count string fail if the results from a query match the provided expression (e.g. '>0')
-f, --file string path to a query to run
Expand Down

0 comments on commit f0ad17a

Please sign in to comment.