Skip to content

Commit

Permalink
feat(cli): be able to change global LW_LQL_OPERATOR
Browse files Browse the repository at this point in the history
The `eq` operator allows you to specify a value that the field values
of the result must be equal to. The `ne` operator means not equal to.

The `in` operator allows you to specify multiple values in the values
field of the filters. The field values of the result must match one of
the values. The `not_in` operator is the opposite of `in`.

The `like` operator allows you to specify a pattern that the field
values of the result must match. The `not_like` operator is the
opposite of `like`.

The `ilike` operator works similar to like but it makes the match case
insensitive. The `not_ilike` operator is the opposite of `ilike`.

(default) The `rlike` operator matches the specified pattern represented
by regular expressions (more info on `RLIKE` see https://docs.snowflake.com/en/sql-reference/functions/rlike.html).
The `not_rlike` operator is the opposite of `rlike`.

The `gt` operator allows you to specify a value that the field values of
the result must be greater than. The `lt` (less-than) operator is the
opposite of `gt`.

The `ge` operator allows you to specify a value that the field values of
the result must be greater than or equal to. The `le` (less-than-or-equal-to)
operator is the opposite of `ge`.

Signed-off-by: Salim Afiune Maya <[email protected]>
  • Loading branch information
afiune committed Feb 10, 2022
1 parent 8e200a0 commit a4c7746
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
9 changes: 6 additions & 3 deletions cli/cmd/agent_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ func listAgents(_ *cobra.Command, _ []string) error {
)

if len(agentListCmdState.Filters) != 0 {
progressMsg = fmt.Sprintf("%s with filters (%s)", progressMsg, strings.Join(agentListCmdState.Filters, ", "))
progressMsg = fmt.Sprintf(
"%s with filters (%s)",
progressMsg, strings.Join(agentListCmdState.Filters, ", "),
)
filters.Filters = []api.Filter{}
for _, tag := range agentListCmdState.Filters {

Expand All @@ -91,8 +94,8 @@ func listAgents(_ *cobra.Command, _ []string) error {

cli.Log.Infow("adding filter", "key", kv[0], "value", kv[1])
filters.Filters = append(filters.Filters, api.Filter{
Field: fmt.Sprintf("tags.%s", kv[0]),
Expression: "rlike", // @afiune we use rlike to allow user to pass regex
Field: kv[0],
Expression: cli.lqlOperator, // @afiune we use rlike to allow user to pass regex
Value: kv[1],
})
}
Expand Down
47 changes: 43 additions & 4 deletions cli/cmd/cli_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,19 @@ type cliState struct {
csvOutput bool
nonInteractive bool
noCache bool
lqlOperator string
profileDetails map[string]interface{}
tokenCache api.TokenData
}

// NewDefaultState creates a new cliState with some defaults
func NewDefaultState() *cliState {
c := &cliState{
id: newID(),
Profile: "default",
CfgVersion: 2,
Log: lwlogger.New("").Sugar(),
id: newID(),
Profile: "default",
lqlOperator: "rlike", // @afiune we use rlike to allow user to pass regex
CfgVersion: 2,
Log: lwlogger.New("").Sugar(),
JsonF: &prettyjson.Formatter{
KeyColor: color.New(color.FgCyan, color.Bold),
StringColor: color.New(color.FgGreen, color.Bold),
Expand Down Expand Up @@ -248,6 +250,8 @@ func (c *cliState) NewClient() error {
apiOpts = append(apiOpts, api.WithURL(os.Getenv("LW_API_SERVER_URL")))
}

c.LoadLQLOperator()

client, err := api.NewClient(c.Account, apiOpts...)
if err != nil {
return errors.Wrap(err, "unable to generate api client")
Expand All @@ -259,6 +263,41 @@ func (c *cliState) NewClient() error {
return c.WriteCachedToken()
}

// LoadLQLOperator reads the reserverd environment variable to change the
// default LQL operator in the CLI for filter flags. Available operators;
//
// The `eq` operator allows you to specify a value that the field values
// of the result must be equal to. The `ne` operator means not equal to.
//
// The `in` operator allows you to specify multiple values in the values
// field of the filters. The field values of the result must match one of
// the values. The `not_in` operator is the opposite of `in`.
//
// The `like` operator allows you to specify a pattern that the field
// values of the result must match. The `not_like` operator is the
// opposite of `like`.
//
// The `ilike` operator works similar to like but it makes the match case
// insensitive. The `not_ilike` operator is the opposite of `ilike`.
//
// (default) The `rlike` operator matches the specified pattern represented
// by regular expressions. The `not_rlike` operator is the opposite of `rlike`.
// (more info on `RLIKE` see https://docs.snowflake.com/en/sql-reference/functions/rlike.html).
//
// The `gt` operator allows you to specify a value that the field values of
// the result must be greater than. The `lt` (less-than) operator is the
// opposite of `gt`.
//
// The `ge` operator allows you to specify a value that the field values of
// the result must be greater than or equal to. The `le` (less-than-or-equal-to)
// operator is the opposite of `ge`.

func (c *cliState) LoadLQLOperator() {
if os.Getenv("LW_LQL_OPERATOR") != "" {
c.lqlOperator = os.Getenv("LW_LQL_OPERATOR")
}
}

// InteractiveMode returns true if the cli is running in interactive mode
func (c *cliState) InteractiveMode() bool {
return !c.nonInteractive && !c.csvOutput
Expand Down
1 change: 0 additions & 1 deletion cli/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ import (
// )}
// ```
type CmdFilters struct {
// list of available filters from a command
Filters []string
}

Expand Down

0 comments on commit a4c7746

Please sign in to comment.