Skip to content

Commit

Permalink
Merge pull request #1673 from ripienaar/1665.5
Browse files Browse the repository at this point in the history
(#1665) allow filter and std filter to be used together
  • Loading branch information
ripienaar authored May 15, 2022
2 parents 268032f + d472a6e commit 3ae0dae
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
29 changes: 29 additions & 0 deletions client/discovery/cli_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,35 @@ func NewStandardOptions() *StandardOptions {
}
}

// Merge merges opt with the settings here, when a basic setting is
// set in opt it will overwrite the one here, when its a filter like
// a list or map it will extend ours with its values.
func (o *StandardOptions) Merge(opt *StandardOptions) {
if opt.Collective != "" {
o.Collective = opt.Collective
}
o.FactFilter = append(o.FactFilter, opt.FactFilter...)
o.AgentFilter = append(o.AgentFilter, opt.AgentFilter...)
o.ClassFilter = append(o.ClassFilter, opt.ClassFilter...)
o.IdentityFilter = append(o.IdentityFilter, opt.IdentityFilter...)
o.CombinedFilter = append(o.CombinedFilter, opt.CombinedFilter...)
if opt.CompoundFilter != "" {
o.CompoundFilter = opt.CompoundFilter
}
if opt.DiscoveryMethod != "" {
o.DiscoveryMethod = opt.DiscoveryMethod
}
if opt.DiscoveryTimeout > 0 {
o.DiscoveryTimeout = opt.DiscoveryTimeout
}
if opt.NodesFile != "" {
o.NodesFile = opt.NodesFile
}
for k, v := range opt.DiscoveryOptions {
o.DiscoveryOptions[k] = v
}
}

// AddSelectionFlags adds the --dm and --discovery-timeout options
func (o *StandardOptions) AddSelectionFlags(app inter.FlagApp) {
app.Flag("dm", "Sets a discovery method (mc, choria, file, external, inventory)").EnumVar(&o.DiscoveryMethod, "broadcast", "choria", "mc", "file", "flatfile", "external", "inventory")
Expand Down
4 changes: 2 additions & 2 deletions internal/fs/schemas/builder.json
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,12 @@
}
},
"filter": {
"description": "Optional filters to apply to the request, can not be combined with std_filters",
"description": "Optional filters to apply to the request, will be merged with standard options if set",
"$ref": "#/definitions/rpc_filter"
},
"std_filters": {
"type": "boolean",
"description": "Enables standard RPC filters like -C, -I etc, can not be combined with filters",
"description": "Enables standard RPC filters like -C, -I etc",
"default": false
},
"output_format_flags": {
Expand Down
13 changes: 5 additions & 8 deletions providers/appbuilder/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,11 @@ func (r *RPC) CreateCommand(app inter.FlagApp) (*kingpin.CmdClause, error) {
return nil, fmt.Errorf("invalid output format %q, valid formats are senders, json and table", r.def.OutputFormat)
}

switch {
case r.def.StandardFilter && r.def.Filter != nil:
return nil, fmt.Errorf("only one of std_filters and filter may be supplied in command %s", r.def.Name)

case r.def.StandardFilter:
if r.def.StandardFilter {
r.fo = discovery.NewStandardOptions()
r.fo.AddFilterFlags(r.cmd)
r.fo.AddFlatFileFlags(r.cmd)
r.fo.AddSelectionFlags(r.cmd)

case r.def.Filter != nil:
r.fo = r.def.Filter
}

switch {
Expand Down Expand Up @@ -278,6 +271,10 @@ func (r *RPC) runCommand(_ *kingpin.ParseContext) error {
r.fo = discovery.NewStandardOptions()
}

if r.def.Filter != nil {
r.fo.Merge(r.def.Filter)
}

r.fo.SetDefaultsFromChoria(fw)

if r.def.AllNodesConfirmPrompt != "" && r.fo.NodesFile == "" {
Expand Down

0 comments on commit 3ae0dae

Please sign in to comment.