diff --git a/bake/ast.go b/bake/ast.go index c2c0e48..b6d9159 100644 --- a/bake/ast.go +++ b/bake/ast.go @@ -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 @@ -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": @@ -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) @@ -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 diff --git a/bake/examples/website/bake/main.go b/bake/examples/website/bake/main.go index f224fdc..b5251ed 100644 --- a/bake/examples/website/bake/main.go +++ b/bake/examples/website/bake/main.go @@ -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") diff --git a/bake/main.go b/bake/main.go index bd39f55..727f4b8 100644 --- a/bake/main.go +++ b/bake/main.go @@ -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) @@ -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 { @@ -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) @@ -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 }) } diff --git a/bake/template.gotmpl b/bake/template.gotmpl new file mode 100644 index 0000000..cf50759 --- /dev/null +++ b/bake/template.gotmpl @@ -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}} +}