Skip to content

Commit

Permalink
bake: add task map
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGamba committed Jun 16, 2024
1 parent 9bca868 commit d703937
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 31 deletions.
5 changes: 0 additions & 5 deletions bake/examples/website/bake/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"fmt"
"log"
"os"
"path/filepath"

Expand All @@ -14,10 +13,6 @@ import (
"github.com/DavidGamba/go-getoptions/dag"
)

var Logger = log.New(os.Stderr, "", log.LstdFlags)

var TM *dag.TaskMap

func Nothing(s string) error {
return nil
}
Expand Down
5 changes: 5 additions & 0 deletions bake/templates/main.go.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,20 @@ import (
"os"

"github.com/DavidGamba/go-getoptions"
"github.com/DavidGamba/go-getoptions/dag"
)

var Logger = log.New(os.Stderr, "", log.LstdFlags)

var TM *dag.TaskMap

func main() {
os.Exit(program(os.Args))
}

func program(args []string) int {
TM = dag.NewTaskMap()

opt := getoptions.New()
opt.SetUnknownMode(getoptions.Pass)
opt.Bool("quiet", false, opt.GetEnv("QUIET"))
Expand Down
59 changes: 33 additions & 26 deletions bake/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,31 +91,31 @@ func (ot *OptTree) AddCommand(name, descName, description string) (*getoptions.G
}

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{}{},
"break": {},
"default": {},
"func": {},
"interface": {},
"go": {},
"select": {},
"case": {},
"defer": {},
"goto": {},
"map": {},
"struct": {},
"chan": {},
"else": {},
"if": {},
"package": {},
"switch": {},
"const": {},
"fallthrough": {},
"import": {},
"range": {},
"type": {},
"continue": {},
"for": {},
"return": {},
"var": {},
}

func validateCmdName(name, descName string) error {
Expand All @@ -138,7 +138,14 @@ func (on *OptNode) String() string {
}
if on.Name != "" {
out += fmt.Sprintf("%s := %s.NewCommand(\"%s\", `%s`)\n", on.DescName, parent, on.DescName, on.Description)
out += fmt.Sprintf("%s.SetCommandFn(%s(%s))\n\n", on.DescName, on.Name, on.DescName)
out += fmt.Sprintf("%s.SetCommandFn(%s(%s))\n", on.DescName, on.Name, on.DescName)

// TODO: This is not considering more than two levels of commands
if parent == "opt" {
out += fmt.Sprintf("TM.Add(\"%s\", %s(%s))\n\n", on.DescName, on.Name, on.DescName)
} else {
out += fmt.Sprintf("TM.Add(\"%s:%s\", %s(%s))\n\n", parent, on.DescName, on.Name, on.DescName)
}
}
for _, child := range on.Children {
out += child.String()
Expand Down

0 comments on commit d703937

Please sign in to comment.