Skip to content

Commit

Permalink
bake: add force and version commands
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGamba committed Jun 21, 2024
1 parent 6090fc4 commit 3d988dc
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion bake/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,22 @@ import (
"io"
"log"
"os"
"path/filepath"
"runtime"
"runtime/debug"

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

//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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
}

0 comments on commit 3d988dc

Please sign in to comment.