-
Notifications
You must be signed in to change notification settings - Fork 113
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
RSDK-9755 - CLI lint enforce createCommandWithT usage #4803
Merged
stuqdog
merged 7 commits into
viamrobotics:main
from
stuqdog:RSDK-9755-lint-enforce-create-command-with-t
Feb 25, 2025
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c39cd5e
add useclihelper
stuqdog facc1e6
add lint rule to verify use of createCommandWithT
stuqdog dd7a66f
make SDK codeowners for CLI internals
stuqdog adecad4
a bit of cleanup
stuqdog fca2a24
make lint rule private
stuqdog edb2668
lint fixes
stuqdog ad03297
fail fast with linting
stuqdog File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
cli_lint |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Package main is the CLI-specific linter itself. | ||
package main | ||
|
||
import ( | ||
"go/ast" | ||
"go/types" | ||
"strings" | ||
|
||
"golang.org/x/tools/go/analysis" | ||
"golang.org/x/tools/go/analysis/singlechecker" | ||
) | ||
|
||
var enforceCreateCommandWithT = &analysis.Analyzer{ | ||
Name: "createcommandwitht", | ||
Doc: "Use CreateCommandWithT", | ||
Run: enforceCreateCommandWithTRun, | ||
} | ||
|
||
func enforceCreateCommandWithTRun(pass *analysis.Pass) (interface{}, error) { | ||
var commandType types.Type | ||
|
||
for _, pkg := range pass.Pkg.Imports() { | ||
// we want to differentiate the upstream `cli` package and our own `cli` package | ||
if pkg.Name() == "cli" && !strings.Contains(pkg.Path(), "viam") { | ||
commandType = pkg.Scope().Lookup("Command").Type() | ||
commandType = types.NewPointer(commandType) // actual `Commands` in the app are pointers | ||
} | ||
} | ||
if commandType == nil { // no CLI imports so we can skip | ||
return nil, nil | ||
} | ||
|
||
for _, file := range pass.Files { | ||
ast.Inspect(file, func(node ast.Node) bool { | ||
composite, isComposite := node.(*ast.CompositeLit) | ||
|
||
// we aren't looking at a struct, so no need to evaluate further | ||
if !isComposite { | ||
return true | ||
} | ||
compositeType := pass.TypesInfo.TypeOf(composite) | ||
|
||
// we aren't looking at a CLI command, so no need to evaluate further | ||
if compositeType.String() != commandType.String() { | ||
return true | ||
} | ||
|
||
for _, elt := range composite.Elts { | ||
keyValue, isKeyValue := elt.(*ast.KeyValueExpr) | ||
|
||
// we aren't looking at assignment of a struct param, so no need to evaluate further | ||
if !isKeyValue { | ||
return true | ||
} | ||
key := keyValue.Key.(*ast.Ident) | ||
|
||
// "Action", "Before", and "After" are the three types of CLI actions for which | ||
// `createCommandWithT` was designed | ||
if key.Name == "Action" || key.Name == "Before" || | ||
key.Name == "After" { | ||
callExpr, isCallExpr := keyValue.Value.(*ast.CallExpr) | ||
|
||
// `Action` was assigned a literal value, rather than a function call. | ||
if !isCallExpr { | ||
pass.Report(analysis.Diagnostic{ | ||
Pos: keyValue.Pos(), | ||
End: keyValue.End(), | ||
Message: "must use createCommandWithT when constructing a CLI action", | ||
}) | ||
return true | ||
} | ||
|
||
callExprFunc, isCallExprFunc := callExpr.Fun.(*ast.IndexExpr) | ||
if !isCallExprFunc { // this should never happen, but just for explicitness | ||
return true | ||
} | ||
funcIdent, isFuncIdent := callExprFunc.X.(*ast.Ident) | ||
if !isFuncIdent { // as above, this should never happen | ||
return true | ||
} | ||
|
||
if funcIdent.Name != "createCommandWithT" { | ||
// some other func was used to generate the action | ||
pass.Report(analysis.Diagnostic{ | ||
Pos: funcIdent.Pos(), | ||
End: funcIdent.End(), | ||
Message: "must use createCommandWithT when constructing a CLI action", | ||
}) | ||
} | ||
} | ||
} | ||
return true | ||
}) | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
func main() { | ||
singlechecker.Main(enforceCreateCommandWithT) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Maybe lint before running unit tests?
Unit tests take forever, and linting should be fast