diff --git a/bake/main.go b/bake/main.go index 8fa9e45..e60f1d5 100644 --- a/bake/main.go +++ b/bake/main.go @@ -8,7 +8,11 @@ import ( "io" "log" "os" + "path/filepath" + "runtime" + "runtime/debug" + "github.com/DavidGamba/dgtools/buildutils" "github.com/DavidGamba/dgtools/run" "github.com/DavidGamba/go-getoptions" ) @@ -16,7 +20,10 @@ import ( //go:embed templates/*.go.gotmpl var templates embed.FS -const generatedMainFilename = "generated_bake.go" +const ( + version = "0.1.0" + generatedMainFilename = "generated_bake.go" +) var InputArgs []string var Dir string @@ -84,6 +91,12 @@ func program(args []string) int { binit := b.NewCommand("init", "initialize a new bake project") binit.SetCommandFn(initRun(dir)) + bforce := b.NewCommand("force", "force rebuild of the generated bake file and the binary on the next run") + bforce.SetCommandFn(InvalidateCache(dir)) + + bversion := b.NewCommand("version", "print the version of bake") + bversion.SetCommandFn(Version(dir)) + opt.HelpCommand("help", opt.Alias("?")) remaining, err := opt.Parse(args[1:]) if err != nil { @@ -111,3 +124,33 @@ func PrintFuncDeclRun(dir string) getoptions.CommandFn { return nil } } + +func InvalidateCache(dir string) getoptions.CommandFn { + return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error { + fmt.Printf("Invalidating bake cache...\n") + err := buildutils.Touch(filepath.Join(dir, "go.mod")) + if err != nil { + return fmt.Errorf("failed to invalidate cache: %w", err) + } + return nil + } +} + +func Version(dir string) getoptions.CommandFn { + return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error { + fmt.Printf("bake version %s\n", version) + info, ok := debug.ReadBuildInfo() + if !ok { + return fmt.Errorf("failed to read build info") + } + fmt.Printf("go version %s\n", runtime.Version()) + + fmt.Printf("%16s %s\n", "module.path", info.Main.Path) + for _, s := range info.Settings { + if s.Value != "" { + fmt.Printf("%16s %s\n", s.Key, s.Value) + } + } + return nil + } +}