Merge pull request #2 from 3bbbeau/v0.0.3

fix: nested components and project naming context
This commit is contained in:
Beau 2024-03-22 20:05:33 +00:00 committed by GitHub
commit 7e9de4fa79
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 13 deletions

View File

@ -180,12 +180,6 @@ func discover(ctx context.Context, flags *Flags) ([]repocfg.Component, error) {
if !info.IsDir() && strings.HasSuffix(info.Name(), TF_EXT) { if !info.IsDir() && strings.HasSuffix(info.Name(), TF_EXT) {
logger.Debug(fmt.Sprintf("found .tf file: %s", path)) logger.Debug(fmt.Sprintf("found .tf file: %s", path))
// Add the directory of the Terraform component as the parent
parent, err := filepath.Rel(flags.Root, filepath.Dir(path))
if err != nil {
return err
}
// Each edge should be a path containing Terraform variables files // Each edge should be a path containing Terraform variables files
// relative to the Terraform component // relative to the Terraform component
err = filepath.Walk(filepath.Dir(path), func(subpath string, subinfo os.FileInfo, suberr error) error { err = filepath.Walk(filepath.Dir(path), func(subpath string, subinfo os.FileInfo, suberr error) error {
@ -195,19 +189,23 @@ func discover(ctx context.Context, flags *Flags) ([]repocfg.Component, error) {
// Filter out Terraform variable files // Filter out Terraform variable files
if !subinfo.IsDir() && (strings.HasSuffix(subinfo.Name(), TFVARS_EXT) || strings.HasSuffix(subinfo.Name(), TFVARS_JSON_EXT)) { if !subinfo.IsDir() && (strings.HasSuffix(subinfo.Name(), TFVARS_EXT) || strings.HasSuffix(subinfo.Name(), TFVARS_JSON_EXT)) {
parent := subpath
// Ignore nested Terraform variable files that might belong to nested components // Ignore nested Terraform variable files that might belong to nested components
if filepath.Dir(subpath) != filepath.Dir(path) && tfExistsInDir(filepath.Dir(subpath)) { if filepath.Dir(subpath) != filepath.Dir(path) && !tfExistsInDir(filepath.Dir(subpath)) {
logger.Sugar().Debugf(`ignoring nested %s: %s logger.Sugar().Debugf(`ignoring nested %s: %s
which belongs to the component at: %s which belongs to the component at: %s
not: %s`, filepath.Ext(subpath), subpath, filepath.Dir(subpath), filepath.Dir(path), not: %s`, filepath.Ext(subpath), subpath, filepath.Dir(subpath), filepath.Dir(path),
) )
parent = path
return filepath.SkipDir
} }
logger.Sugar().Debugf("component parent is %s", parent) relParent, err := filepath.Rel(flags.Root, filepath.Dir(parent))
if err != nil {
return err
}
logger.Sugar().Debugf("component parent is %s", relParent)
found := repocfg.Component{ found := repocfg.Component{
Path: parent, Path: relParent,
} }
logger.Sugar().Debugf("found %s file: %s", filepath.Ext(subpath), subpath) logger.Sugar().Debugf("found %s file: %s", filepath.Ext(subpath), subpath)

View File

@ -9,8 +9,15 @@ import (
func ptr[T any](v T) *T { return &v } func ptr[T any](v T) *T { return &v }
// friendlyName creates a contextual name used for Atlantis projects // friendlyName creates a contextual name used for Atlantis projects
func friendlyName(path, environment string) string { func friendlyName(path, varFile string) string {
name := []string{strings.ReplaceAll(path, "/", "-"), pathWithoutExtension(filepath.Base(environment))} environment := pathWithoutExtension(filepath.Base(varFile))
// avoid constructing a joined path if the context is the current directory
if filepath.Base(path) == "." {
return environment
}
name := []string{strings.ReplaceAll(path, "/", "-"), environment}
return strings.TrimSuffix(strings.Join(name, "-"), "-") return strings.TrimSuffix(strings.Join(name, "-"), "-")
} }