-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Bug fix: invalid tool should error out #1776
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
2be83a0
bf9d382
8e269d4
fb51559
9bf817a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -101,6 +101,7 @@ func (b *Builder) WithToolsets(toolsetIDs []string) *Builder { | |
| // WithTools specifies additional tools that bypass toolset filtering. | ||
| // These tools are additive - they will be included even if their toolset is not enabled. | ||
| // Read-only filtering still applies to these tools. | ||
| // Input is cleaned (trimmed, deduplicated) during Build(). | ||
| // Deprecated tool aliases are automatically resolved to their canonical names during Build(). | ||
| // Returns self for chaining. | ||
| func (b *Builder) WithTools(toolNames []string) *Builder { | ||
|
|
@@ -127,6 +128,24 @@ func (b *Builder) WithFilter(filter ToolFilter) *Builder { | |
| return b | ||
| } | ||
|
|
||
| // cleanTools trims whitespace and removes duplicates from tool names. | ||
| // Empty strings after trimming are excluded. | ||
| func cleanTools(tools []string) []string { | ||
| seen := make(map[string]bool) | ||
| var cleaned []string | ||
| for _, name := range tools { | ||
| trimmed := strings.TrimSpace(name) | ||
| if trimmed == "" { | ||
| continue | ||
| } | ||
| if !seen[trimmed] { | ||
| seen[trimmed] = true | ||
| cleaned = append(cleaned, trimmed) | ||
| } | ||
| } | ||
| return cleaned | ||
| } | ||
|
|
||
| // Build creates the final Inventory with all configuration applied. | ||
| // This processes toolset filtering, tool name resolution, and sets up | ||
| // the inventory for use. The returned Inventory is ready for use with | ||
|
|
@@ -145,10 +164,19 @@ func (b *Builder) Build() *Inventory { | |
| // Process toolsets and pre-compute metadata in a single pass | ||
| r.enabledToolsets, r.unrecognizedToolsets, r.toolsetIDs, r.toolsetIDSet, r.defaultToolsetIDs, r.toolsetDescriptions = b.processToolsets() | ||
|
|
||
| // Process additional tools (resolve aliases) | ||
| // Build set of valid tool names for validation | ||
| validToolNames := make(map[string]bool, len(b.tools)) | ||
| for i := range b.tools { | ||
| validToolNames[b.tools[i].Tool.Name] = true | ||
| } | ||
|
|
||
| // Process additional tools (clean, resolve aliases, and track unrecognized) | ||
| if len(b.additionalTools) > 0 { | ||
| r.additionalTools = make(map[string]bool, len(b.additionalTools)) | ||
| for _, name := range b.additionalTools { | ||
| cleanedTools := cleanTools(b.additionalTools) | ||
|
|
||
| r.additionalTools = make(map[string]bool, len(cleanedTools)) | ||
| var unrecognizedTools []string | ||
| for _, name := range cleanedTools { | ||
| // Always include the original name - this handles the case where | ||
| // the tool exists but is controlled by a feature flag that's OFF. | ||
| r.additionalTools[name] = true | ||
|
|
@@ -157,8 +185,12 @@ func (b *Builder) Build() *Inventory { | |
| // the new consolidated tool is available. | ||
| if canonical, isAlias := b.deprecatedAliases[name]; isAlias { | ||
| r.additionalTools[canonical] = true | ||
| } else if !validToolNames[name] { | ||
| // Not a valid tool and not a deprecated alias - track as unrecognized | ||
| unrecognizedTools = append(unrecognizedTools, name) | ||
| } | ||
| } | ||
| r.unrecognizedTools = unrecognizedTools | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To me it makes sense if the builder can error, to simply return an error during building, and not allow an invalid inventory to build. The proactive checking is a riskier pattern as people need to remember to keep code checking it. No need to add unrecongnizedTools, it's reasonable to error during build. |
||
| } | ||
|
|
||
| return r | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kind of prefer input parsing to be done at the point of user input, rather than here, that said it's nice that it would work for remote server by having it in shared pipeline.