From 1777f98aeffbf0e544546ded8107f9e6ab273a67 Mon Sep 17 00:00:00 2001 From: jon4hz Date: Sat, 25 Feb 2023 01:25:36 +0100 Subject: [PATCH] feat: search for workspace config in parent dir --- cli/packages/util/config.go | 37 ++++++++++++++++++++++++++++++++++++- cli/packages/util/helper.go | 4 ++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/cli/packages/util/config.go b/cli/packages/util/config.go index 48bb57241..82d74e8fb 100644 --- a/cli/packages/util/config.go +++ b/cli/packages/util/config.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "os" + "path/filepath" "github.com/Infisical/infisical-merge/packages/models" log "github.com/sirupsen/logrus" @@ -73,7 +74,12 @@ func WorkspaceConfigFileExistsInCurrentPath() bool { } func GetWorkSpaceFromFile() (models.WorkspaceConfigFile, error) { - configFileAsBytes, err := os.ReadFile(INFISICAL_WORKSPACE_CONFIG_FILE_NAME) + cfgFile, err := FindWorkspaceConfigFile() + if err != nil { + return models.WorkspaceConfigFile{}, err + } + + configFileAsBytes, err := os.ReadFile(cfgFile) if err != nil { return models.WorkspaceConfigFile{}, err } @@ -87,6 +93,35 @@ func GetWorkSpaceFromFile() (models.WorkspaceConfigFile, error) { return workspaceConfigFile, nil } +// FindWorkspaceConfigFile searches for a .infisical.json file in the current directory and all parent directories. +func FindWorkspaceConfigFile() (string, error) { + dir, err := os.Getwd() + if err != nil { + return "", err + } + + for { + path := filepath.Join(dir, INFISICAL_WORKSPACE_CONFIG_FILE_NAME) + _, err := os.Stat(path) + if err == nil { + // file found + return path, nil + } + + // check if we have reached the root directory + if dir == filepath.Dir(dir) { + break + } + + // move up one directory + dir = filepath.Dir(dir) + } + + // file not found + return "", fmt.Errorf("file not found: %s", INFISICAL_WORKSPACE_CONFIG_FILE_NAME) + +} + func GetFullConfigFilePath() (fullPathToFile string, fullPathToDirectory string, err error) { homeDir, err := GetHomeDir() if err != nil { diff --git a/cli/packages/util/helper.go b/cli/packages/util/helper.go index 8d70e9e0a..db62bc1dc 100644 --- a/cli/packages/util/helper.go +++ b/cli/packages/util/helper.go @@ -89,8 +89,8 @@ func RequireServiceToken() { } func RequireLocalWorkspaceFile() { - workspaceFileExists := WorkspaceConfigFileExistsInCurrentPath() - if !workspaceFileExists { + workspaceFilePath, _ := FindWorkspaceConfigFile() + if workspaceFilePath == "" { PrintErrorMessageAndExit("It looks you have not yet connected this project to Infisical", "To do so, run [infisical init] then run your command again") }