Skip to content

Commit

Permalink
bake: add template code
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGamba committed Jun 9, 2024
1 parent 30d8777 commit 368d255
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 9 deletions.
10 changes: 5 additions & 5 deletions bake/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func LoadAst(ctx context.Context, opt *getoptions.GetOpt, dir string) error {
if ok {
return false
}
Logger.Printf("stmt: %s\n", buf.String())
// Logger.Printf("stmt: %s\n", buf.String())
exprStmt, ok := stmt.(*ast.ExprStmt)
if !ok {
continue
Expand All @@ -275,7 +275,7 @@ func LoadAst(ctx context.Context, opt *getoptions.GetOpt, dir string) error {
if xIdent.Name != optFieldName {
return false
}
Logger.Printf("handling %s.%s\n", xIdent.Name, fun.Sel.Name)
// Logger.Printf("handling %s.%s\n", xIdent.Name, fun.Sel.Name)

switch fun.Sel.Name {
case "String":
Expand Down Expand Up @@ -305,7 +305,7 @@ func handleString(cmd *getoptions.GetOpt, optFieldName string, n ast.Node) error
mfns := []getoptions.ModifyFn{}
// Check for args
for i, arg := range x.Args {
Logger.Printf("i: %d, arg: %T\n", i, arg)
// Logger.Printf("i: %d, arg: %T\n", i, arg)
if i == 0 {
// First argument is the Name
// Logger.Printf("Name: '%s'\n", arg.(*ast.BasicLit).Value)
Expand Down Expand Up @@ -340,14 +340,14 @@ func handleString(cmd *getoptions.GetOpt, optFieldName string, n ast.Node) error
if xIdent.Name != optFieldName {
continue
}
Logger.Printf("\t%s.%s\n", xIdent.Name, fun.Sel.Name)
// Logger.Printf("\t%s.%s\n", xIdent.Name, fun.Sel.Name)
if fun.Sel.Name == "SetCalled" {
// TODO: SetCalled function receives a bool
continue
}
values := []string{}
for _, arg := range callE.Args {
Logger.Printf("Value: %s\n", arg.(*ast.BasicLit).Value)
// Logger.Printf("Value: %s\n", arg.(*ast.BasicLit).Value)
value, err := strconv.Unquote(arg.(*ast.BasicLit).Value)
if err != nil {
value = arg.(*ast.BasicLit).Value
Expand Down
2 changes: 1 addition & 1 deletion bake/examples/website/bake/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func Build(opt *getoptions.GetOpt) getoptions.CommandFn {
func Asciidoc(opt *getoptions.GetOpt) getoptions.CommandFn {
opt.String("lang", "en", opt.ValidValues("en", "es"), opt.Description("Language"))
opt.String("hello", "world")
Logger.Println("Running Asciidoc")
Logger.Println("Running Asciidoc prep")
opt.String("hola", "mundo")
return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
Logger.Println("Running build:diagram")
Expand Down
11 changes: 8 additions & 3 deletions bake/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import (
"strings"
"unicode"

"github.com/DavidGamba/dgtools/run"
"github.com/DavidGamba/go-getoptions"
)

var inputArgs []string
var InputArgs []string
var Dir string

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

Expand All @@ -36,6 +38,7 @@ func program(args []string) int {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
return 1
}
Dir = dir

// bakefile, plug, err := loadPlugin(ctx)
// if err != nil {
Expand All @@ -49,7 +52,7 @@ func program(args []string) int {
// return 1
// }

inputArgs = args[1:]
InputArgs = args[1:]
err = LoadAst(ctx, opt, dir)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
Expand Down Expand Up @@ -142,7 +145,9 @@ func (ot *OptTree) AddCommand(name, description string) *getoptions.GetOpt {
if len(keys) == i+1 {
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)
fmt.Printf("Running %v\n", InputArgs)
c := []string{"./bake"}
run.CMD(append(c, InputArgs...)...).Dir(Dir).Run()
return nil
})
}
Expand Down
53 changes: 53 additions & 0 deletions bake/template.gotmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"errors"
"fmt"
"io"
"os"

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

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

func program(args []string) int {
opt := getoptions.New()
opt.SetUnknownMode(getoptions.Pass)
opt.Bool("quiet", false, opt.GetEnv("QUIET"))

loadFns(opt)

opt.HelpCommand("help", opt.Alias("?"))
remaining, err := opt.Parse(args[1:])
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
return 1
}
if opt.Called("quiet") {
Logger.SetOutput(io.Discard)
}
Logger.Println(remaining)

ctx, cancel, done := getoptions.InterruptContext()
defer func() { cancel(); <-done }()

err = opt.Dispatch(ctx, remaining)
if err != nil {
if errors.Is(err, getoptions.ErrorHelpCalled) {
return 1
}
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
if errors.Is(err, getoptions.ErrorParsing) {
fmt.Fprintf(os.Stderr, "\n"+opt.Help())
}
return 1
}
return 0
}

func loadFns(opt *getoptions.GetOpt) {
{{.Fns}}
}

0 comments on commit 368d255

Please sign in to comment.