Skip to content

Commit

Permalink
bake: validate yield error and bad command names
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGamba committed Jun 14, 2024
1 parent 779c886 commit 7717b0a
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
18 changes: 16 additions & 2 deletions bake/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ func parsedFiles(dir string) iter.Seq2[ParsedFile, error] {
f, err := parser.ParseFile(fset, file, nil, parser.ParseComments)
if err != nil {
yield(p, fmt.Errorf("failed to parse file: %w", err))
return
}
p.f = f
yield(p, nil)
if !yield(p, nil) {
return
}
}
}
}
Expand All @@ -71,8 +74,12 @@ func AstFns(dir string) iter.Seq2[FnDecl, error] {
}
fnDecl := FnDecl{ParsedFile: p}

doneYield := false
// Iterate through every node in the file
ast.Inspect(p.f, func(n ast.Node) bool {
if doneYield {
return false
}
fnDecl.Node = n
switch x := n.(type) {
// Check function declarations for exported functions
Expand All @@ -83,11 +90,18 @@ func AstFns(dir string) iter.Seq2[FnDecl, error] {
var buf bytes.Buffer
printer.Fprint(&buf, p.fset, x.Type)
fnDecl.Type = buf.String()
yield(fnDecl, nil)
if !yield(fnDecl, nil) {
doneYield = true
return false
}
}
}
return true
})

if doneYield {
return
}
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions bake/ast_getoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ func LoadAst(ctx context.Context, opt *getoptions.GetOpt, dir string) (*OptTree,
return ot, err
}

cmd := ot.AddCommand(getOptFn.Name, getOptFn.DescName, getOptFn.Description)
cmd, err := ot.AddCommand(getOptFn.Name, getOptFn.DescName, getOptFn.Description)
if err != nil {
return ot, err
}
err = addOptionsToCMD(getOptFn, cmd)
if err != nil {
return ot, err
Expand Down Expand Up @@ -116,7 +119,9 @@ func AstGetoptionFns(ctx context.Context, dir string) iter.Seq2[GetOptFn, error]
} else {
getOptFn.DescName = camelToKebab(getOptFn.Name)
}
yield(getOptFn, nil)
if !yield(getOptFn, nil) {
return
}
}
}
}
44 changes: 42 additions & 2 deletions bake/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func NewOptTree(opt *getoptions.GetOpt) *OptTree {
// Regex for description: fn-name - description
var descriptionRe = regexp.MustCompile(`^\w\S+ -`)

func (ot *OptTree) AddCommand(name, descName, description string) *getoptions.GetOpt {
func (ot *OptTree) AddCommand(name, descName, description string) (*getoptions.GetOpt, error) {
keys := strings.Split(descName, ":")
// Logger.Printf("keys: %v\n", keys)
node := ot.Root
Expand All @@ -63,6 +63,10 @@ func (ot *OptTree) AddCommand(name, descName, description string) *getoptions.Ge
if len(keys) == i+1 {
desc = description
}
err := validateCmdName(key)
if err != nil {
return nil, err
}
cmd = node.Opt.NewCommand(key, desc)
node.Children[key] = &OptNode{
Name: name,
Expand All @@ -83,7 +87,43 @@ func (ot *OptTree) AddCommand(name, descName, description string) *getoptions.Ge
})
}
}
return cmd
return cmd, nil
}

var golangKeywords = map[string]struct{}{
"break": struct{}{},
"default": struct{}{},
"func": struct{}{},
"interface": struct{}{},
"go": struct{}{},
"select": struct{}{},
"case": struct{}{},
"defer": struct{}{},
"goto": struct{}{},
"map": struct{}{},
"struct": struct{}{},
"chan": struct{}{},
"else": struct{}{},
"if": struct{}{},
"package": struct{}{},
"switch": struct{}{},
"const": struct{}{},
"fallthrough": struct{}{},
"import": struct{}{},
"range": struct{}{},
"type": struct{}{},
"continue": struct{}{},
"for": struct{}{},
"return": struct{}{},
"var": struct{}{},
}

func validateCmdName(name string) error {
// if command name matches a golang keyword, return an error
if _, ok := golangKeywords[name]; ok {
return fmt.Errorf("command name '%s' is a golang keyword", name)
}
return nil
}

func (ot *OptTree) String() string {
Expand Down

0 comments on commit 7717b0a

Please sign in to comment.