Skip to content

Commit

Permalink
env-loader: bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fheinecke committed Nov 19, 2024
1 parent 4bd2c6c commit 0042f04
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
12 changes: 7 additions & 5 deletions tools/env-loader/cmd/env-loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func parseCLI() *config {

kingpin.Flag("environments-directory", "Path to the directory containing all environments, defaulting to the repo root").
Short('d').
Envar(EnvVarPrefix + "ENVIRONMENT").
Envar(EnvVarPrefix + "ENVIRONMENTS_DIRECTORY").
StringVar(&c.EnvironmentsDirectory)

kingpin.Flag("environment", "Name of the environment containing the values to load").
Expand Down Expand Up @@ -88,13 +88,15 @@ func run(c *config) error {
}

// Filter out values not requested
maps.DeleteFunc(envValues, func(key, _ string) bool {
return !slices.Contains(c.Values, key)
})
if len(c.Values) > 0 {
maps.DeleteFunc(envValues, func(key, _ string) bool {
return !slices.Contains(c.Values, key)
})
}

// Build the output string
writer := writers.FromName[c.Writer]
envValueOutput, err := writer.FormatEnvironmentValues(map[string]string{})
envValueOutput, err := writer.FormatEnvironmentValues(envValues)
if err != nil {
return trace.Wrap(err, "failed to format output values with writer %q", c.Writer)
}
Expand Down
36 changes: 27 additions & 9 deletions tools/env-loader/pkg/envloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const EnvironmentNameDirectorySeparator = "/"
// value files.
const CommonFileGlob = "common.*"

const gitFakeLinkFileIdentifier = "gitdir: "

func findGitRepoRoot() (string, error) {
cwd, err := os.Getwd()
if err != nil {
Expand All @@ -47,20 +49,37 @@ func findGitRepoRoot() (string, error) {
// Walk upwards until a '.git' directory is found, or root is reached
path := filepath.Clean(cwd)
for {
fileInfo, err := os.Lstat(filepath.Join(path, ".git"))
gitFsObjectPath := filepath.Join(path, ".git")
fileInfo, err := os.Lstat(gitFsObjectPath)
// If failed to stat the fs object and it exists
if err != nil && !os.IsNotExist(err) {
return "", trace.Wrap(err, "failed to read file information for %q", path)
return "", trace.Wrap(err, "failed to read file information for %q", gitFsObjectPath)
}

// If the .git fs object was found and it is a directory
if err == nil && fileInfo.IsDir() {
absPath, err := filepath.Abs(path)
if err != nil {
return "", trace.Wrap(err, "failed to get absolute path for git repo at %q", path)
if err == nil {
isCurrentPathAGitDirectory := fileInfo.IsDir()

// Perform some rudimentary checking to see if the .git directory
// exists elsewhere, as is the case with submodules:
// https://git-scm.com/docs/git-init#Documentation/git-init.txt-code--separate-git-dircodeemltgit-dirgtem
if fileInfo.Mode().IsRegular() {
fileContents, err := os.ReadFile(gitFsObjectPath)
if err != nil {
return "", trace.Wrap(err, "failed to read .git file at %q", gitFsObjectPath)
}

isCurrentPathAGitDirectory = strings.HasPrefix(string(fileContents), gitFakeLinkFileIdentifier)
}

return absPath, nil
if isCurrentPathAGitDirectory {
absPath, err := filepath.Abs(path)
if err != nil {
return "", trace.Wrap(err, "failed to get absolute path for git repo at %q", path)
}

return absPath, nil
}
}

// If the .git fs object was found and is not a directory, or it wasn't
Expand Down Expand Up @@ -90,8 +109,7 @@ func findCommonFilesInPath(basePath, relativeSubdirectoryPath string) ([]string,
var commonFilePaths []string
currentDirectoryPath := basePath
for _, directoryNameToCheck := range subdirectoryNames {
currentDirectoryPath := filepath.Join(currentDirectoryPath, directoryNameToCheck)

currentDirectoryPath = filepath.Join(currentDirectoryPath, directoryNameToCheck)
fileInfo, err := os.Lstat(currentDirectoryPath)
if err != nil {
return nil, trace.Wrap(err, "failed to lstat %q", currentDirectoryPath)
Expand Down

0 comments on commit 0042f04

Please sign in to comment.