Skip to content

Commit

Permalink
bt: allow running stack from anywhere under the stacks config file
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGamba committed Mar 6, 2024
1 parent b8c7fad commit 0e5d091
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
4 changes: 4 additions & 0 deletions bt/stack/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"log"
"os"
"path/filepath"

"github.com/DavidGamba/dgtools/buildutils"
"github.com/DavidGamba/dgtools/cueutils"
Expand Down Expand Up @@ -35,6 +36,9 @@ func Get(ctx context.Context, filename string) (*Config, string, error) {
return &Config{}, f, fmt.Errorf("failed to read config: %w", err)
}

cfg.ConfigFile = f
cfg.ConfigRoot = filepath.Dir(f)

return cfg, f, nil
}

Expand Down
6 changes: 4 additions & 2 deletions bt/stack/config/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package config
import "fmt"

type Config struct {
Component map[ID]Component `json:"component"`
Stack map[ID]Stack `json:"stack"`
Component map[ID]Component `json:"component"`
Stack map[ID]Stack `json:"stack"`
ConfigRoot string `json:"config_root"`
ConfigFile string `json:"config_file"`
}

type Component struct {
Expand Down
23 changes: 20 additions & 3 deletions bt/stack/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package stack
import (
"context"
"fmt"
"os"
"path/filepath"

sconfig "github.com/DavidGamba/dgtools/bt/stack/config"
"github.com/DavidGamba/dgtools/bt/terraform"
Expand All @@ -14,6 +16,11 @@ func generateDAG(id string, cfg *sconfig.Config, normal bool) (*dag.Graph, error
tm := dag.NewTaskMap()
g := dag.NewGraph("stack " + id)

wd, err := os.Getwd()
if err != nil {
return g, fmt.Errorf("failed to get current working directory: %w", err)
}

emptyFn := func(dir string) getoptions.CommandFn {
return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
return nil
Expand All @@ -22,14 +29,24 @@ func generateDAG(id string, cfg *sconfig.Config, normal bool) (*dag.Graph, error
normalFn := func(component, dir string) getoptions.CommandFn {
return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
ctx = terraform.NewComponentContext(ctx, component)
ctx = terraform.NewDirContext(ctx, dir)
d := filepath.Join(cfg.ConfigRoot, dir)
d, err = filepath.Rel(wd, d)
if err != nil {
return fmt.Errorf("failed to get relative path: %w", err)
}
ctx = terraform.NewDirContext(ctx, d)
return terraform.BuildRun(ctx, opt, args)
}
}
wsFn := func(component, dir, ws string) getoptions.CommandFn {
return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
ctx = terraform.NewComponentContext(ctx, fmt.Sprintf("%s:%s", component, ws))
ctx = terraform.NewDirContext(ctx, dir)
d := filepath.Join(cfg.ConfigRoot, dir)
d, err = filepath.Rel(wd, d)
if err != nil {
return fmt.Errorf("failed to get relative path: %w", err)
}
ctx = terraform.NewDirContext(ctx, d)
err := opt.SetValue("ws", ws)
if err != nil {
return fmt.Errorf("failed to set workspace: %w", err)
Expand Down Expand Up @@ -97,7 +114,7 @@ func generateDAG(id string, cfg *sconfig.Config, normal bool) (*dag.Graph, error
}
}

err := g.Validate(tm)
err = g.Validate(tm)
if err != nil {
return g, fmt.Errorf("failed to build graph: %w", err)
}
Expand Down

0 comments on commit 0e5d091

Please sign in to comment.