Skip to content

Commit

Permalink
bake: allow using a bakefiles directory as an alternative to bake
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGamba committed Jun 20, 2024
1 parent 1208265 commit 285581b
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions bake/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func buildBinary(dir string) error {

var ErrNotFound = fmt.Errorf("not found")

// findBakeDir - searches for bakefiles/ dir first then for bake/ dir.
// This allows me to use bake in this repo.
func findBakeDir(ctx context.Context) (string, error) {
wd, err := os.Getwd()
if err != nil {
Expand All @@ -41,26 +43,44 @@ func findBakeDir(ctx context.Context) (string, error) {

// First case, bake folder lives in CWD
// This has higher priority to allow me to have a bake folder for bake itself.
dir := filepath.Join(wd, "bake")
dir := filepath.Join(wd, "bakefiles")
if fi, err := os.Stat(dir); err == nil && fi.Mode().IsDir() {
return dir, nil
}
dir = filepath.Join(wd, "bake")
if fi, err := os.Stat(dir); err == nil && fi.Mode().IsDir() {
return dir, nil
}

// Second case, we are withing the bake folder
base := filepath.Base(wd)
if base == "bakefiles" {
return ".", nil
}
if base == "bake" {
return ".", nil
}

// Third case, search for bake folder in parent directories
d, err := buildutils.FindDirUpwards(ctx, dir)
d, err := buildutils.FindDirUpwards(ctx, "bakefiles")
if err == nil {
return d, nil
}
if err != nil {
if !errors.Is(err, buildutils.ErrNotFound) {
return ".", fmt.Errorf("failed to find bake folder: %w", err)
}
}
d, err = buildutils.FindDirUpwards(ctx, "bake")
if err == nil {
return d, nil
}
if err != nil {
if errors.Is(err, buildutils.ErrNotFound) {
return ".", ErrNotFound
if !errors.Is(err, buildutils.ErrNotFound) {
return ".", fmt.Errorf("failed to find bake folder: %w", err)
}
return ".", fmt.Errorf("failed to find bake folder: %w", err)
}
return d, nil
return ".", ErrNotFound
}

func GenerateMainFile(ot *OptTree, dir string) error {
Expand Down

0 comments on commit 285581b

Please sign in to comment.