Skip to content

Commit

Permalink
bake: handle extra edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGamba committed Jun 16, 2024
1 parent b0bb340 commit 6e79e56
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
10 changes: 8 additions & 2 deletions bake/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func buildBinary(dir string) error {
Logger.Printf("Found modifications on %v, rebuilding binary...\n", files)
err = run.CMD("go", "build").Dir(dir).Log().Run()
if err != nil {
os.Remove(filepath.Join(dir, generatedMainFilename))
os.Remove(filepath.Join(dir, "bake"))
return fmt.Errorf("failed to build binary: %w", err)
}
}
Expand Down Expand Up @@ -79,7 +79,13 @@ func GenerateMainFile(ot *OptTree, dir string) error {
if err != nil {
return err
}
if !modified {

binaryExists := true
if _, err := os.Stat(filepath.Join(dir, "bake")); os.IsNotExist(err) {
binaryExists = false
}

if !modified && binaryExists {
return nil
}
Logger.Printf("Found source modifications on %v, regenerating template...\n", files)
Expand Down
19 changes: 19 additions & 0 deletions bake/examples/website/bake/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,22 @@ func Clean(opt *getoptions.GetOpt) getoptions.CommandFn {
func NotUsed() error {
return nil
}

// 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")

switch lang {
case "en":
fmt.Println("Hello")
case "es":
fmt.Println("Hola")
}

return nil
}
}
1 change: 0 additions & 1 deletion bake/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ func program(args []string) int {
}

if requiredFilesPresent {
// TODO: Ensure we populate TM
ot, err := LoadAst(ctx, opt, dir)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
Expand Down
13 changes: 9 additions & 4 deletions bake/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ func NewOptTree(opt *getoptions.GetOpt) *OptTree {
var descriptionRe = regexp.MustCompile(`^\w\S+ -`)

func (ot *OptTree) AddCommand(name, descName, description string) (*getoptions.GetOpt, error) {
Logger.Printf("Adding command %s with function %s\n", descName, name)
keys := strings.Split(descName, ":")
// Logger.Printf("keys: %v\n", keys)
node := ot.Root
var cmd *getoptions.GetOpt
for i, key := range keys {
Expand All @@ -69,15 +69,15 @@ func (ot *OptTree) AddCommand(name, descName, description string) (*getoptions.G
}
cmd = node.Opt.NewCommand(key, desc)
node.Children[key] = &OptNode{
Name: name,
Name: "",
Parent: node.DescName,
Opt: cmd,
Children: make(map[string]*OptNode),
Description: desc,
DescName: key,
}
node = node.Children[key]
if len(keys) == i+1 {
node.Children[key].Name = name
cmd.SetCommandFn(func(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
// TODO: Run os.exec call to the built binary with keys as the arguments
fmt.Printf("Running %v\n", InputArgs)
Expand All @@ -86,6 +86,7 @@ func (ot *OptTree) AddCommand(name, descName, description string) (*getoptions.G
return nil
})
}
node = node.Children[key]
}
return cmd, nil
}
Expand Down Expand Up @@ -136,8 +137,12 @@ func (on *OptNode) String() string {
if parent == "" {
parent = "opt"
}
if on.Name != "" {

if on.DescName != "" {
out += fmt.Sprintf("%s := %s.NewCommand(\"%s\", `%s`)\n", on.DescName, parent, on.DescName, on.Description)
}

if on.Name != "" {
out += fmt.Sprintf("%sFn := %s(%s)\n", on.DescName, on.Name, on.DescName)
out += fmt.Sprintf("%s.SetCommandFn(%sFn)\n", on.DescName, on.DescName)

Expand Down

0 comments on commit 6e79e56

Please sign in to comment.