Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow terramate and stack config from multiple files #216

Merged
merged 24 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
ba34534
feat: add initial func to load config from multiple files
katcipis Feb 10, 2022
a428e5b
tests: change tests to allow multiple files
katcipis Feb 10, 2022
e110823
test: redesign to allow specific files for each config
katcipis Feb 11, 2022
2eb76e0
test: start testing using the new ParseDir function
katcipis Feb 11, 2022
8f228ae
Merge branch 'main' of github.com:mineiros-io/terramate into katcipis…
katcipis Feb 11, 2022
47604b2
feat: add working hcl config loading from multiple files
katcipis Feb 11, 2022
26df76b
test: stack init test use new ParseDir
katcipis Feb 11, 2022
8ca82f6
feat: use new dir parsing for backend config
katcipis Feb 11, 2022
cb6fd5a
feat: load configuration from multiple files
katcipis Feb 11, 2022
f179030
refactor: remove unused functions
katcipis Feb 11, 2022
fbe6cb0
test: piggyback on current tests for different config filenames
katcipis Feb 11, 2022
c166767
test: initial merge test for terramate block
katcipis Feb 11, 2022
9146567
feat: improve error for multiple stack blocks
katcipis Feb 11, 2022
bada3a2
test: add failing test for stack listing
katcipis Feb 11, 2022
ff66ae7
feat: stacks list now work with multiple config filenames
katcipis Feb 11, 2022
0c6a8ba
Merge branch 'main' of github.com:mineiros-io/terramate into katcipis…
katcipis Feb 14, 2022
f6228a3
refactor: allow panic instead of error on body cast
katcipis Feb 14, 2022
fb79f1e
style: improve cfgfile declaration
katcipis Feb 14, 2022
a715d9b
Update hcl/hcl.go
katcipis Feb 14, 2022
dbb9ee1
Update hcl/hcl.go
katcipis Feb 14, 2022
14a8028
Update hcl/hcl.go
katcipis Feb 14, 2022
9cd7df4
Update generate/generate.go
katcipis Feb 14, 2022
c5e756d
chore: improve logging and avoid duplication
katcipis Feb 14, 2022
c0984a1
Merge branch 'katcipis-load-tm-config-multiple-files' of github.com:m…
katcipis Feb 14, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions cmd/terramate/e2etests/stacks_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/madlambda/spells/assert"
"github.com/mineiros-io/terramate"
"github.com/mineiros-io/terramate/cmd/terramate/cli"
"github.com/mineiros-io/terramate/config"
"github.com/mineiros-io/terramate/hcl"
"github.com/mineiros-io/terramate/test"
"github.com/mineiros-io/terramate/test/sandbox"
Expand Down Expand Up @@ -217,8 +216,8 @@ func TestStacksInit(t *testing.T) {
}

for _, path := range tc.input {
data := test.ReadFile(t, s.RootDir(), filepath.Join(path, config.DefaultFilename))
got, err := hcl.Parse("TestInitHCL", data)
dir := filepath.Join(s.RootDir(), path)
got, err := hcl.ParseDir(dir)
assert.NoError(t, err, "parsing terramate file")

want := hcl.Config{
Expand Down
10 changes: 10 additions & 0 deletions cmd/terramate/e2etests/stacks_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ z/a
}
}

func TestListStackWithDefinitionOnNonDefaultFilename(t *testing.T) {
s := sandbox.New(t)
s.BuildTree([]string{"d:stack"})
stackDir := s.DirEntry("stack")
stackDir.CreateFile("stack.tm", "stack {}")

cli := newCLI(t, s.RootDir())
assertRunResult(t, cli.run("stacks", "list"), runExpected{Stdout: "stack\n"})
}

func TestListStackWithNoTerramateBlock(t *testing.T) {
s := sandbox.New(t)
s.BuildTree([]string{"s:stack"})
Expand Down
37 changes: 3 additions & 34 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,37 +30,6 @@ const (
DefaultInitConstraint = "~>"
)

// Exists tells if path has a terramate config file.
func Exists(path string) bool {
logger := log.With().
Str("action", "Exists()").
Str("path", path).
Logger()

logger.Trace().
Msg("Get path info.")
st, err := os.Stat(path)
if err != nil {
return false
}

logger.Trace().
Msg("Check if path is directory.")
if !st.IsDir() {
return false
}

logger.Trace().
Msg("Look for config file within directory.")
fname := filepath.Join(path, DefaultFilename)
info, err := os.Stat(fname)
if err != nil {
return false
}

return info.Mode().IsRegular()
}

func TryLoadRootConfig(dir string) (cfg hcl.Config, found bool, err error) {
path := filepath.Join(dir, DefaultFilename)
logger := log.With().
Expand All @@ -80,9 +49,9 @@ func TryLoadRootConfig(dir string) (cfg hcl.Config, found bool, err error) {
return hcl.Config{}, false, err
}

logger.Trace().
Msg("Parse file.")
cfg, err = hcl.ParseFile(path)
logger.Trace().Msg("Parse Terramate config.")

cfg, err = hcl.ParseDir(dir)
if err != nil {
return hcl.Config{}, false, err
}
Expand Down
35 changes: 5 additions & 30 deletions generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/madlambda/spells/errutil"
"github.com/mineiros-io/terramate"
"github.com/mineiros-io/terramate/config"
"github.com/mineiros-io/terramate/generate/genhcl"
"github.com/mineiros-io/terramate/hcl"
"github.com/mineiros-io/terramate/hcl/eval"
Expand Down Expand Up @@ -533,46 +532,22 @@ func generateBackendCfgCode(
Str("configDir", configdir).
Logger()

logger.Trace().
Msg("Check if config dir outside of root dir.")
logger.Trace().Msg("Check if config dir outside of root dir.")

if !strings.HasPrefix(configdir, root) {
// check if we are outside of project's root, time to stop
return "", nil
}

logger.Trace().
Msg("Get config file path.")
configfile := filepath.Join(configdir, config.DefaultFilename)

logger = logger.With().
Str("configFile", configfile).
Logger()
logger.Trace().Msg("Load stack backend config.")

logger.Trace().
Msg("Load stack backend config.")
if _, err := os.Stat(configfile); err != nil {
// FIXME(katcipis): use os.IsNotExist(err) to handle errors properly.
// Unknown stat errors will be ignored right now.
return generateBackendCfgCode(root, stackpath, stackMetadata, globals, filepath.Dir(configdir))
}

logger.Debug().
Msg("Read config file.")
config, err := os.ReadFile(configfile)
parsedConfig, err := hcl.ParseDir(configdir)
if err != nil {
return "", fmt.Errorf("reading config: %v", err)
return "", fmt.Errorf("loading backend config from %q: %v", configdir, err)
}

logger.Debug().
Msg("Parse config file.")
parsedConfig, err := hcl.Parse(configfile, config)
if err != nil {
return "", fmt.Errorf("parsing config: %w", err)
}
logger.Trace().Msg("Check if we have Terramate block")

logger.Trace().
Msg("Check if parsed is empty.")
parsed := parsedConfig.Terramate
if parsed == nil || parsed.Backend == nil {
return generateBackendCfgCode(root, stackpath, stackMetadata, globals, filepath.Dir(configdir))
Expand Down
Loading