Added --value flag to secrets get command to return only value

This commit is contained in:
Rhythm Bhiwani
2024-03-05 16:04:21 +05:30
parent d8d2741868
commit 0912903e0d

View File

@@ -406,6 +406,11 @@ func getSecretsByNames(cmd *cobra.Command, args []string) {
util.HandleError(err, "Unable to parse path flag")
}
showOnlyValue, err := cmd.Flags().GetBool("value")
if err != nil {
util.HandleError(err, "Unable to parse path flag")
}
secrets, err := util.GetAllEnvironmentVariables(models.GetAllSecretsParameters{Environment: environmentName, InfisicalToken: infisicalToken, TagSlugs: tagSlugs, SecretsPath: secretsPath}, "")
if err != nil {
util.HandleError(err, "To fetch all secrets")
@@ -427,7 +432,15 @@ func getSecretsByNames(cmd *cobra.Command, args []string) {
}
}
visualize.PrintAllSecretDetails(requestedSecrets)
if showOnlyValue && len(requestedSecrets) > 1 {
util.PrintErrorMessageAndExit("--value only works with one secret.")
}
if showOnlyValue {
fmt.Printf(requestedSecrets[0].Value)
} else {
visualize.PrintAllSecretDetails(requestedSecrets)
}
Telemetry.CaptureEvent("cli-command:secrets get", posthog.NewProperties().Set("secretCount", len(secrets)).Set("version", util.CLI_VERSION))
}
@@ -661,6 +674,7 @@ func init() {
secretsGetCmd.Flags().String("token", "", "Fetch secrets using the Infisical Token")
secretsCmd.AddCommand(secretsGetCmd)
secretsGetCmd.Flags().String("path", "/", "get secrets within a folder path")
secretsGetCmd.Flags().Bool("value", false, "Returns only the value of secret, only works with one secret")
secretsCmd.Flags().Bool("secret-overriding", true, "Prioritizes personal secrets, if any, with the same name over shared secrets")
secretsCmd.AddCommand(secretsSetCmd)