Skip to content

Commit

Permalink
Find a fallback for PROJECT_DIR env for local tests and debugging (#4296
Browse files Browse the repository at this point in the history
)
  • Loading branch information
mszadkow authored Feb 18, 2025
1 parent f7df8a8 commit 4f01318
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion test/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -967,8 +967,41 @@ func KExecute(ctx context.Context, cfg *rest.Config, client *rest.RESTClient, ns
return out.Bytes(), outErr.Bytes(), nil
}

// GetProjectBaseDir retrieves the project base directory either from an environment variable or by searching for a Makefile.
// The fallback to the search is useful for running in IDEs like vs-code which don't set the PROJECT_DIR env. variable by default.
func GetProjectBaseDir() string {
return filepath.Dir(os.Getenv("PROJECT_DIR"))
projectBasePath, found := os.LookupEnv("PROJECT_DIR")
if found {
return filepath.Dir(projectBasePath)
}

projectBaseDir, err := findMakefileDir()
if err != nil {
klog.Error(err)
return ""
}
return projectBaseDir
}

// findMakefileDir traverses directories upward from the current directory until it finds a directory containing a Makefile.
func findMakefileDir() (string, error) {
startDir, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("could not get current working directory: %w", err)
}

for {
makefilePath := filepath.Join(startDir, "Makefile")
if _, err := os.Stat(makefilePath); err == nil {
return startDir, nil
}

parentDir := filepath.Dir(startDir)
if parentDir == startDir {
return "", errors.New("not able to locate Makefile")
}
startDir = parentDir
}
}

func FindDeploymentCondition(deployment *appsv1.Deployment, deploymentType appsv1.DeploymentConditionType) *appsv1.DeploymentCondition {
Expand Down

0 comments on commit 4f01318

Please sign in to comment.