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

env-loader: bug fixes #295

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
Loading