From bd233ebe9bf6693f72c1a48a84132497ce2a8885 Mon Sep 17 00:00:00 2001 From: Maidul Islam Date: Thu, 23 Feb 2023 00:00:49 -0500 Subject: [PATCH] allow git branch mapping to env --- cli/packages/cmd/export.go | 11 ++++++--- cli/packages/cmd/run.go | 11 ++++++--- cli/packages/cmd/secrets.go | 48 ++++++++++++++++++++++-------------- cli/packages/models/cli.go | 12 +++++---- cli/packages/util/helper.go | 15 +++++++++++ cli/packages/util/secrets.go | 28 ++++++++++++++++++--- 6 files changed, 90 insertions(+), 35 deletions(-) diff --git a/cli/packages/cmd/export.go b/cli/packages/cmd/export.go index d1e72ff2d1..649de41f2b 100644 --- a/cli/packages/cmd/export.go +++ b/cli/packages/cmd/export.go @@ -36,9 +36,12 @@ var exportCmd = &cobra.Command{ // util.RequireLocalWorkspaceFile() }, Run: func(cmd *cobra.Command, args []string) { - envName, err := cmd.Flags().GetString("env") - if err != nil { - util.HandleError(err) + environmentName, _ := cmd.Flags().GetString("env") + if !cmd.Flags().Changed("env") { + environmentFromWorkspace := util.GetEnvelopmentBasedOnGitBranch() + if environmentFromWorkspace != "" { + environmentName = environmentFromWorkspace + } } shouldExpandSecrets, err := cmd.Flags().GetBool("expand") @@ -66,7 +69,7 @@ var exportCmd = &cobra.Command{ util.HandleError(err, "Unable to parse flag") } - secrets, err := util.GetAllEnvironmentVariables(models.GetAllSecretsParameters{Environment: envName, InfisicalToken: infisicalToken, TagSlugs: tagSlugs}) + secrets, err := util.GetAllEnvironmentVariables(models.GetAllSecretsParameters{Environment: environmentName, InfisicalToken: infisicalToken, TagSlugs: tagSlugs}) if err != nil { util.HandleError(err, "Unable to fetch secrets") } diff --git a/cli/packages/cmd/run.go b/cli/packages/cmd/run.go index 70c6e627d2..f70d29726a 100644 --- a/cli/packages/cmd/run.go +++ b/cli/packages/cmd/run.go @@ -54,9 +54,12 @@ var runCmd = &cobra.Command{ return nil }, Run: func(cmd *cobra.Command, args []string) { - envName, err := cmd.Flags().GetString("env") - if err != nil { - util.HandleError(err, "Unable to parse flag") + environmentName, _ := cmd.Flags().GetString("env") + if !cmd.Flags().Changed("env") { + environmentFromWorkspace := util.GetEnvelopmentBasedOnGitBranch() + if environmentFromWorkspace != "" { + environmentName = environmentFromWorkspace + } } infisicalToken, err := cmd.Flags().GetString("token") @@ -79,7 +82,7 @@ var runCmd = &cobra.Command{ util.HandleError(err, "Unable to parse flag") } - secrets, err := util.GetAllEnvironmentVariables(models.GetAllSecretsParameters{Environment: envName, InfisicalToken: infisicalToken, TagSlugs: tagSlugs}) + secrets, err := util.GetAllEnvironmentVariables(models.GetAllSecretsParameters{Environment: environmentName, InfisicalToken: infisicalToken, TagSlugs: tagSlugs}) if err != nil { util.HandleError(err, "Could not fetch secrets", "If you are using a service token to fetch secrets, please ensure it is valid") diff --git a/cli/packages/cmd/secrets.go b/cli/packages/cmd/secrets.go index 4bb8d06632..625152aa95 100644 --- a/cli/packages/cmd/secrets.go +++ b/cli/packages/cmd/secrets.go @@ -19,7 +19,6 @@ import ( "github.com/Infisical/infisical-merge/packages/util" "github.com/Infisical/infisical-merge/packages/visualize" "github.com/go-resty/resty/v2" - log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -31,9 +30,12 @@ var secretsCmd = &cobra.Command{ PreRun: toggleDebug, Args: cobra.NoArgs, Run: func(cmd *cobra.Command, args []string) { - environmentName, err := cmd.Flags().GetString("env") - if err != nil { - util.HandleError(err) + environmentName, _ := cmd.Flags().GetString("env") + if !cmd.Flags().Changed("env") { + environmentFromWorkspace := util.GetEnvelopmentBasedOnGitBranch() + if environmentFromWorkspace != "" { + environmentName = environmentFromWorkspace + } } infisicalToken, err := cmd.Flags().GetString("token") @@ -94,9 +96,12 @@ var secretsSetCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { util.RequireLocalWorkspaceFile() - environmentName, err := cmd.Flags().GetString("env") - if err != nil { - util.HandleError(err, "Unable to parse flag") + environmentName, _ := cmd.Flags().GetString("env") + if !cmd.Flags().Changed("env") { + environmentFromWorkspace := util.GetEnvelopmentBasedOnGitBranch() + if environmentFromWorkspace != "" { + environmentName = environmentFromWorkspace + } } workspaceFile, err := util.GetWorkSpaceFromFile() @@ -270,11 +275,12 @@ var secretsDeleteCmd = &cobra.Command{ PreRun: toggleDebug, Args: cobra.MinimumNArgs(1), Run: func(cmd *cobra.Command, args []string) { - environmentName, err := cmd.Flags().GetString("env") - if err != nil { - log.Errorln("Unable to parse the environment name flag") - log.Debugln(err) - return + environmentName, _ := cmd.Flags().GetString("env") + if !cmd.Flags().Changed("env") { + environmentFromWorkspace := util.GetEnvelopmentBasedOnGitBranch() + if environmentFromWorkspace != "" { + environmentName = environmentFromWorkspace + } } loggedInUserDetails, err := util.GetCurrentLoggedInUserDetails() @@ -330,9 +336,12 @@ var secretsDeleteCmd = &cobra.Command{ } func getSecretsByNames(cmd *cobra.Command, args []string) { - environmentName, err := cmd.Flags().GetString("env") - if err != nil { - util.HandleError(err, "Unable to parse flag") + environmentName, _ := cmd.Flags().GetString("env") + if !cmd.Flags().Changed("env") { + environmentFromWorkspace := util.GetEnvelopmentBasedOnGitBranch() + if environmentFromWorkspace != "" { + environmentName = environmentFromWorkspace + } } infisicalToken, err := cmd.Flags().GetString("token") @@ -373,9 +382,12 @@ func getSecretsByNames(cmd *cobra.Command, args []string) { } func generateExampleEnv(cmd *cobra.Command, args []string) { - environmentName, err := cmd.Flags().GetString("env") - if err != nil { - util.HandleError(err, "Unable to parse flag") + environmentName, _ := cmd.Flags().GetString("env") + if !cmd.Flags().Changed("env") { + environmentFromWorkspace := util.GetEnvelopmentBasedOnGitBranch() + if environmentFromWorkspace != "" { + environmentName = environmentFromWorkspace + } } infisicalToken, err := cmd.Flags().GetString("token") diff --git a/cli/packages/models/cli.go b/cli/packages/models/cli.go index fca9ae0f2c..c2cee9463c 100644 --- a/cli/packages/models/cli.go +++ b/cli/packages/models/cli.go @@ -39,8 +39,9 @@ type Workspace struct { } type WorkspaceConfigFile struct { - WorkspaceId string `json:"workspaceId"` - DefaultEnvironment string `json:"defaultEnvironment"` + WorkspaceId string `json:"workspaceId"` + DefaultEnvironment string `json:"defaultEnvironment"` + GitBranchToEnvironmentMapping map[string]string `json:"gitBranchToEnvironmentMapping"` } type SymmetricEncryptionResult struct { @@ -50,7 +51,8 @@ type SymmetricEncryptionResult struct { } type GetAllSecretsParameters struct { - Environment string - InfisicalToken string - TagSlugs string + Environment string + EnvironmentPassedViaFlag bool + InfisicalToken string + TagSlugs string } diff --git a/cli/packages/util/helper.go b/cli/packages/util/helper.go index 2a7ac7317a..8d70e9e0a5 100644 --- a/cli/packages/util/helper.go +++ b/cli/packages/util/helper.go @@ -1,10 +1,14 @@ package util import ( + "bytes" "crypto/sha256" "encoding/base64" "fmt" "os" + "os/exec" + "path" + "strings" ) type DecodedSymmetricEncryptionDetails = struct { @@ -110,3 +114,14 @@ func GetHashFromStringList(list []string) string { sum := sha256.Sum256(hash.Sum(nil)) return fmt.Sprintf("%x", sum) } + +func getCurrentBranch() (string, error) { + cmd := exec.Command("git", "symbolic-ref", "--short", "HEAD") + var out bytes.Buffer + cmd.Stdout = &out + err := cmd.Run() + if err != nil { + return "", err + } + return path.Base(strings.TrimSpace(out.String())), nil +} diff --git a/cli/packages/util/secrets.go b/cli/packages/util/secrets.go index 8caa07de4d..95065de666 100644 --- a/cli/packages/util/secrets.go +++ b/cli/packages/util/secrets.go @@ -131,10 +131,6 @@ func GetAllEnvironmentVariables(params models.GetAllSecretsParameters) ([]models return nil, err } - if workspaceFile.DefaultEnvironment != "" { - params.Environment = workspaceFile.DefaultEnvironment - } - // Verify environment err = ValidateEnvironmentName(params.Environment, workspaceFile.WorkspaceId, loggedInUserDetails.UserCredentials) if err != nil { @@ -485,3 +481,27 @@ func DeleteBackupSecrets() error { return os.RemoveAll(fullPathToSecretsBackupFolder) } + +func GetEnvelopmentBasedOnGitBranch() string { + branch, err := getCurrentBranch() + if err != nil { + log.Debugf("getEnvelopmentBasedOnGitBranch: [err=%s]", err) + } + + workspaceFile, err := GetWorkSpaceFromFile() + if err != nil { + log.Debugf("getEnvelopmentBasedOnGitBranch: [err=%s]", err) + return "" + } + + envBasedOnGitBranch, ok := workspaceFile.GitBranchToEnvironmentMapping[branch] + + log.Debugf("GetEnvelopmentBasedOnGitBranch: [envBasedOnGitBranch=%s] [ok=%s]", envBasedOnGitBranch, ok) + + if err == nil && ok { + return envBasedOnGitBranch + } else { + log.Debugf("getEnvelopmentBasedOnGitBranch: [err=%s]", err) + return "" + } +}