Compare commits

..

No commits in common. "main" and "v0.0.2" have entirely different histories.
main ... v0.0.2

3 changed files with 16 additions and 41 deletions

2
.env
View File

@ -1 +1 @@
VERSION=v0.0.4 VERSION=v0.0.2

View File

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"slices"
"strings" "strings"
"github.com/3bbbeau/tfvars-atlantis-config/logger" "github.com/3bbbeau/tfvars-atlantis-config/logger"
@ -169,7 +168,7 @@ const (
func discover(ctx context.Context, flags *Flags) ([]repocfg.Component, error) { func discover(ctx context.Context, flags *Flags) ([]repocfg.Component, error) {
logger := logger.FromContext(ctx) logger := logger.FromContext(ctx)
discovered := []repocfg.Component{} var discovered []repocfg.Component
// Walk the directory tree from the root // Walk the directory tree from the root
err := filepath.Walk(flags.Root, func(path string, info os.FileInfo, err error) error { err := filepath.Walk(flags.Root, func(path string, info os.FileInfo, err error) error {
@ -181,6 +180,12 @@ 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 {
@ -190,23 +195,19 @@ 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
} }
relParent, err := filepath.Rel(flags.Root, filepath.Dir(parent)) logger.Sugar().Debugf("component parent is %s", parent)
if err != nil {
return err
}
logger.Sugar().Debugf("component parent is %s", relParent)
found := repocfg.Component{ found := repocfg.Component{
Path: relParent, Path: parent,
} }
logger.Sugar().Debugf("found %s file: %s", filepath.Ext(subpath), subpath) logger.Sugar().Debugf("found %s file: %s", filepath.Ext(subpath), subpath)
@ -219,27 +220,8 @@ func discover(ctx context.Context, flags *Flags) ([]repocfg.Component, error) {
logger.Sugar().Debugf("component %s has var file %s", parent, child) logger.Sugar().Debugf("component %s has var file %s", parent, child)
found.VarFiles = append(found.VarFiles, child) found.VarFiles = append(found.VarFiles, child)
// Check if the component already exists in the slice
// If it does, ensure the var file is not a duplicate and append it.
exists := false
for idx, component := range discovered {
if component.Path == found.Path {
exists = true
for _, varFile := range found.VarFiles {
if !slices.Contains(component.VarFiles, varFile) {
discovered[idx].VarFiles = append(discovered[idx].VarFiles, varFile)
}
}
}
}
// If the component does not exist in the slice, then
// it can be treated as new component.
if !exists {
discovered = append(discovered, found) discovered = append(discovered, found)
} }
}
return err return err
}) })
return filepath.SkipDir return filepath.SkipDir

View File

@ -9,15 +9,8 @@ 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, varFile string) string { func friendlyName(path, environment string) string {
environment := pathWithoutExtension(filepath.Base(varFile)) name := []string{strings.ReplaceAll(path, "/", "-"), pathWithoutExtension(filepath.Base(environment))}
// 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, "-"), "-")
} }