Skip to content

Commit

Permalink
bake: get stringvar working
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGamba committed Jun 16, 2024
1 parent f41ce20 commit 453dc20
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 23 deletions.
96 changes: 74 additions & 22 deletions bake/ast_handle_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
func addOptionsToCMD(getOptFn GetOptFn, cmd *getoptions.GetOpt, name string) error {
Logger.Printf("Adding options to %s\n", name)

var outerErr error
// Check for Expressions of opt type
ast.Inspect(getOptFn.Node, func(n ast.Node) bool {
switch x := n.(type) {
Expand Down Expand Up @@ -51,8 +52,64 @@ func addOptionsToCMD(getOptFn GetOptFn, cmd *getoptions.GetOpt, name string) err
// Logger.Printf("handling %s.%s\n", xIdent.Name, fun.Sel.Name)

switch fun.Sel.Name {
case "String":
handleString(cmd, getOptFn.OptFieldName, n)
case "String", "StringOptional", "Int", "IntOptional", "Increment", "Float64", "Float64Optional":
mfns := []getoptions.ModifyFn{}
if len(x.Args) > 2 {
mfns = handleOptionModifiers(cmd, getOptFn.OptFieldName, x.Args[2:])
}
name, defaultValue, err := extractNameAndDefault(n, 0)
if err != nil {
outerErr = err
return false
}
switch fun.Sel.Name {
case "String":
cmd.String(name, defaultValue, mfns...)
case "StringOptional":
cmd.StringOptional(name, defaultValue, mfns...)
case "Int":
x, err := strconv.Atoi(defaultValue)
if err != nil {
x = 0
}
cmd.Int(name, x, mfns...)
case "IntOptional":
x, err := strconv.Atoi(defaultValue)
if err != nil {
x = 0
}
cmd.IntOptional(name, x, mfns...)
case "Increment":
x, err := strconv.Atoi(defaultValue)
if err != nil {
x = 0
}
cmd.Increment(name, x, mfns...)
case "Float64":
x, err := strconv.ParseFloat(defaultValue, 64)
if err != nil {
x = 0.0
}
cmd.Float64(name, x, mfns...)
case "Float64Optional":
x, err := strconv.ParseFloat(defaultValue, 64)
if err != nil {
x = 0.0
}
cmd.Float64Optional(name, x, mfns...)
}

case "StringVar":
mfns := []getoptions.ModifyFn{}
if len(x.Args) > 3 {
mfns = handleOptionModifiers(cmd, getOptFn.OptFieldName, x.Args[3:])
}
name, defaultValue, err := extractNameAndDefault(n, 1)
if err != nil {
outerErr = err
return false
}
cmd.String(name, defaultValue, mfns...)
}

return false
Expand All @@ -63,47 +120,42 @@ func addOptionsToCMD(getOptFn GetOptFn, cmd *getoptions.GetOpt, name string) err
}
return true
})
return nil
return outerErr
}

func handleString(cmd *getoptions.GetOpt, optFieldName string, n ast.Node) error {
func extractNameAndDefault(n ast.Node, offset int) (string, string, error) {
x := n.(*ast.CallExpr)
name, err := extractName(x.Args)
name, err := extractName(x.Args, offset)
if err != nil {
return err
return "", "", err
}
defaultValue, err := extractDefault(x.Args)
defaultValue, err := extractDefault(x.Args, offset)
if err != nil {
return err
}
mfns := []getoptions.ModifyFn{}
if len(x.Args) > 2 {
mfns = handleOptionModifiers(cmd, optFieldName, x.Args[2:])
return "", "", err
}
cmd.String(name, defaultValue, mfns...)
return nil
return name, defaultValue, nil
}

func extractName(args []ast.Expr) (string, error) {
func extractName(args []ast.Expr, offset int) (string, error) {
// First argument is the Name
if len(args) < 1 {
if len(args) < 1+offset {
return "", fmt.Errorf("missing name argument")
}
name, err := strconv.Unquote(args[0].(*ast.BasicLit).Value)
name, err := strconv.Unquote(args[0+offset].(*ast.BasicLit).Value)
if err != nil {
name = args[0].(*ast.BasicLit).Value
name = args[0+offset].(*ast.BasicLit).Value
}
return name, nil
}

func extractDefault(args []ast.Expr) (string, error) {
func extractDefault(args []ast.Expr, offset int) (string, error) {
// Second argument is the Default
if len(args) < 2 {
if len(args) < 2+offset {
return "", fmt.Errorf("missing default argument")
}
defaultValue, err := strconv.Unquote(args[1].(*ast.BasicLit).Value)
defaultValue, err := strconv.Unquote(args[1+offset].(*ast.BasicLit).Value)
if err != nil {
defaultValue = args[1].(*ast.BasicLit).Value
defaultValue = args[1+offset].(*ast.BasicLit).Value
}
return defaultValue, nil
}
Expand Down
1 change: 0 additions & 1 deletion bake/examples/website/bake/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ func NotUsed() error {
// say:hello - This is a greeting
func Hello(opt *getoptions.GetOpt) getoptions.CommandFn {
var lang string
// TODO: Check if the closure pointer receiver works
opt.StringVar(&lang, "lang", "en", opt.ValidValues("en", "es"))
return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
Logger.Println("Running Hello")
Expand Down

0 comments on commit 453dc20

Please sign in to comment.