mirror of
https://github.com/passbolt/go-passbolt-cli.git
synced 2025-05-12 02:58:20 +00:00
Implement CEL filter in list group command
This commit is contained in:
parent
20e3a72c92
commit
6562aeab95
2 changed files with 72 additions and 0 deletions
63
group/filter.go
Normal file
63
group/filter.go
Normal file
|
@ -0,0 +1,63 @@
|
|||
package group
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/cel-go/cel"
|
||||
"github.com/passbolt/go-passbolt/api"
|
||||
)
|
||||
|
||||
// Environments for CEl
|
||||
var celEnvOptions = []cel.EnvOption{
|
||||
cel.Variable("ID", cel.StringType),
|
||||
cel.Variable("Name", cel.StringType),
|
||||
cel.Variable("CreatedTimestamp", cel.TimestampType),
|
||||
cel.Variable("ModifiedTimestamp", cel.TimestampType),
|
||||
}
|
||||
|
||||
// Filters the slice groups by invoke CEL program for each group
|
||||
func filterGroups(groups *[]api.Group, celCmd string, ctx context.Context) ([]api.Group, error) {
|
||||
if celCmd == "" {
|
||||
return *groups, nil
|
||||
}
|
||||
|
||||
env, err := cel.NewEnv(celEnvOptions...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ast, issue := env.Compile(celCmd)
|
||||
if issue.Err() != nil {
|
||||
return nil, issue.Err()
|
||||
}
|
||||
|
||||
program, err := env.Program(ast)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
filteredGroups := []api.Group{}
|
||||
for _, group := range *groups {
|
||||
val, _, err := program.ContextEval(ctx, map[string]any{
|
||||
"ID": group.ID,
|
||||
"Name": group.Name,
|
||||
"CreatedTimestamp": group.Created.Time,
|
||||
"ModifiedTimestamp": group.Modified.Time,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if val.Value() == true {
|
||||
filteredGroups = append(filteredGroups, group)
|
||||
}
|
||||
}
|
||||
|
||||
if len(filteredGroups) == 0 {
|
||||
return nil, fmt.Errorf("No such groups found with filter %v!", celCmd)
|
||||
}
|
||||
|
||||
return filteredGroups, nil
|
||||
}
|
|
@ -51,6 +51,10 @@ func GroupList(cmd *cobra.Command, args []string) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
celFilter, err := cmd.Flags().GetString("filter")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ctx := util.GetContext()
|
||||
|
||||
|
@ -69,6 +73,11 @@ func GroupList(cmd *cobra.Command, args []string) error {
|
|||
return fmt.Errorf("Listing Group: %w", err)
|
||||
}
|
||||
|
||||
groups, err = filterGroups(&groups, celFilter, ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if jsonOutput {
|
||||
outputGroups := []GroupJsonOutput{}
|
||||
for i := range groups {
|
||||
|
|
Loading…
Add table
Reference in a new issue