Skip to content

Commit

Permalink
Add support for rendering value type
Browse files Browse the repository at this point in the history
  • Loading branch information
apazzolini committed May 15, 2024
1 parent 6d63650 commit c3f46cd
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 17 deletions.
2 changes: 2 additions & 0 deletions pkg/cmd/enclave_secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func init() {
}
enclaveSecretsCmd.Flags().Bool("raw", false, "print the raw secret value without processing variables")
enclaveSecretsCmd.Flags().Bool("visibility", false, "include secret visibility in table output")
enclaveSecretsCmd.Flags().Bool("value-type", false, "include secret value type in table output")
enclaveSecretsCmd.Flags().Bool("only-names", false, "only print the secret names; omit all values")

enclaveSecretsGetCmd.Flags().StringP("project", "p", "", "enclave project (e.g. backend)")
Expand All @@ -119,6 +120,7 @@ func init() {
enclaveSecretsGetCmd.Flags().Bool("copy", false, "copy the value(s) to your clipboard")
enclaveSecretsGetCmd.Flags().Bool("raw", false, "print the raw secret value without processing variables")
enclaveSecretsGetCmd.Flags().Bool("visibility", false, "include secret visibility in table output")
enclaveSecretsGetCmd.Flags().Bool("value-type", false, "include secret value type in table output")
enclaveSecretsGetCmd.Flags().Bool("no-exit-on-missing-secret", false, "do not exit if unable to find a requested secret")
enclaveSecretsCmd.AddCommand(enclaveSecretsGetCmd)

Expand Down
14 changes: 9 additions & 5 deletions pkg/cmd/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ func secrets(cmd *cobra.Command, args []string) {
jsonFlag := utils.OutputJSON
raw := utils.GetBoolFlag(cmd, "raw")
visibility := utils.GetBoolFlag(cmd, "visibility")
valueType := utils.GetBoolFlag(cmd, "value-type")
onlyNames := utils.GetBoolFlag(cmd, "only-names")
localConfig := configuration.LocalConfig(cmd)

Expand All @@ -182,7 +183,7 @@ func secrets(cmd *cobra.Command, args []string) {
utils.HandleError(parseErr, "Unable to parse API response")
}

printer.Secrets(secrets, []string{}, jsonFlag, false, raw, false, visibility)
printer.Secrets(secrets, []string{}, jsonFlag, false, raw, false, visibility, valueType)
}
}

Expand All @@ -192,6 +193,7 @@ func getSecrets(cmd *cobra.Command, args []string) {
copy := utils.GetBoolFlag(cmd, "copy")
raw := utils.GetBoolFlag(cmd, "raw")
visibility := utils.GetBoolFlag(cmd, "visibility")
valueType := utils.GetBoolFlag(cmd, "value-type")
exitOnMissingSecret := !utils.GetBoolFlag(cmd, "no-exit-on-missing-secret")
localConfig := configuration.LocalConfig(cmd)

Expand Down Expand Up @@ -228,7 +230,7 @@ func getSecrets(cmd *cobra.Command, args []string) {
}
}

printer.Secrets(secrets, args, jsonFlag, plain, raw, copy, visibility)
printer.Secrets(secrets, args, jsonFlag, plain, raw, copy, visibility, valueType)
}

func setSecrets(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -361,7 +363,7 @@ func setSecrets(cmd *cobra.Command, args []string) {
}

if !utils.Silent {
printer.Secrets(response, keys, jsonFlag, false, raw, false, visibilityModified)
printer.Secrets(response, keys, jsonFlag, false, raw, false, visibilityModified, false)
}
}

Expand Down Expand Up @@ -393,7 +395,7 @@ func uploadSecrets(cmd *cobra.Command, args []string) {
}

if !utils.Silent {
printer.Secrets(response, []string{}, jsonFlag, false, raw, false, false)
printer.Secrets(response, []string{}, jsonFlag, false, raw, false, false, false)
}
}

Expand All @@ -417,7 +419,7 @@ func deleteSecrets(cmd *cobra.Command, args []string) {
}

if !utils.Silent {
printer.Secrets(response, []string{}, jsonFlag, false, raw, false, false)
printer.Secrets(response, []string{}, jsonFlag, false, raw, false, false, false)
}
}
}
Expand Down Expand Up @@ -627,6 +629,7 @@ func init() {
}
secretsCmd.Flags().Bool("raw", false, "print the raw secret value without processing variables")
secretsCmd.Flags().Bool("visibility", false, "include secret visibility in table output")
secretsCmd.Flags().Bool("value-type", false, "include secret value type in table output")
secretsCmd.Flags().Bool("only-names", false, "only print the secret names; omit all values")

secretsGetCmd.Flags().StringP("project", "p", "", "project (e.g. backend)")
Expand All @@ -641,6 +644,7 @@ func init() {
secretsGetCmd.Flags().Bool("copy", false, "copy the value(s) to your clipboard")
secretsGetCmd.Flags().Bool("raw", false, "print the raw secret value without processing variables")
secretsGetCmd.Flags().Bool("visibility", false, "include secret visibility in table output")
secretsGetCmd.Flags().Bool("value-type", false, "include secret value type in table output")
secretsGetCmd.Flags().Bool("no-exit-on-missing-secret", false, "do not exit if unable to find a requested secret")
secretsCmd.AddCommand(secretsGetCmd)

Expand Down
30 changes: 19 additions & 11 deletions pkg/models/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@ limitations under the License.
*/
package models

type SecretValueType struct {
Type string `json:"type"`
}

// ComputedSecret holds all info about a secret
type ComputedSecret struct {
Name string `json:"name"`
RawValue *string `json:"raw"`
ComputedValue *string `json:"computed"`
RawVisibility string `json:"rawVisibility"`
ComputedVisibility string `json:"computedVisibility"`
Note string `json:"note"`
Name string `json:"name"`
RawValue *string `json:"raw"`
ComputedValue *string `json:"computed"`
RawVisibility string `json:"rawVisibility"`
ComputedVisibility string `json:"computedVisibility"`
RawValueType SecretValueType `json:"rawValueType"`
ComputedValueType SecretValueType `json:"computedValueType"`
Note string `json:"note"`
}

// ChangeRequest can be used to smartly update secrets
Expand Down Expand Up @@ -141,11 +147,13 @@ type APISecretResponse struct {

// APISecret is the object the API returns for a given secret
type APISecret struct {
RawValue *string `json:"raw"`
ComputedValue *string `json:"computed"`
RawVisibility string `json:"rawVisibility"`
ComputedVisibility string `json:"computedVisibility"`
Note string `json:"note"`
RawValue *string `json:"raw"`
ComputedValue *string `json:"computed"`
RawVisibility string `json:"rawVisibility"`
ComputedVisibility string `json:"computedVisibility"`
RawValueType SecretValueType `json:"rawValueType"`
ComputedValueType SecretValueType `json:"computedValueType"`
Note string `json:"note"`
}

type ActorInfo struct {
Expand Down
2 changes: 2 additions & 0 deletions pkg/models/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ func ConvertAPIToComputedSecrets(apiSecrets map[string]APISecret) map[string]Com
ComputedValue: secret.ComputedValue,
RawVisibility: secret.RawVisibility,
ComputedVisibility: secret.ComputedVisibility,
RawValueType: secret.RawValueType,
ComputedValueType: secret.ComputedValueType,
Note: secret.Note,
}
}
Expand Down
16 changes: 15 additions & 1 deletion pkg/printer/enclave.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func ProjectInfo(info models.ProjectInfo, jsonFlag bool) {
}

// Secrets print secrets
func Secrets(secrets map[string]models.ComputedSecret, secretsToPrint []string, jsonFlag bool, plain bool, raw bool, copy bool, visibility bool) {
func Secrets(secrets map[string]models.ComputedSecret, secretsToPrint []string, jsonFlag bool, plain bool, raw bool, copy bool, visibility bool, valueType bool) {
if len(secretsToPrint) == 0 {
for name := range secrets {
secretsToPrint = append(secretsToPrint, name)
Expand Down Expand Up @@ -234,6 +234,7 @@ func Secrets(secrets map[string]models.ComputedSecret, secretsToPrint []string,
secretsMap[name] = map[string]interface{}{
"note": secrets[name].Note,
"computedVisibility": secrets[name].ComputedVisibility,
"computedValueType": secrets[name].ComputedValueType,
}

if secrets[name].ComputedValue != nil {
Expand All @@ -244,6 +245,7 @@ func Secrets(secrets map[string]models.ComputedSecret, secretsToPrint []string,

if raw {
secretsMap[name]["rawVisibility"] = secrets[name].RawVisibility
secretsMap[name]["rawValueType"] = secrets[name].RawValueType
if secrets[name].RawValue != nil {
secretsMap[name]["raw"] = *secrets[name].RawValue
} else {
Expand Down Expand Up @@ -290,11 +292,17 @@ func Secrets(secrets map[string]models.ComputedSecret, secretsToPrint []string,
if visibility {
headers = append(headers, "visibility")
}
if valueType {
headers = append(headers, "value type")
}
headers = append(headers, "value")
if raw {
if visibility {
headers = append(headers, "raw visibility")
}
if valueType {
headers = append(headers, "raw value type")
}
headers = append(headers, "raw value")
}
headers = append(headers, "note")
Expand All @@ -312,6 +320,9 @@ func Secrets(secrets map[string]models.ComputedSecret, secretsToPrint []string,
if visibility {
row = append(row, secret.ComputedVisibility)
}
if valueType {
row = append(row, secret.ComputedValueType.Type)
}
row = append(row, computedValue)
if raw {
var rawValue string
Expand All @@ -323,6 +334,9 @@ func Secrets(secrets map[string]models.ComputedSecret, secretsToPrint []string,
if visibility {
row = append(row, secret.RawVisibility)
}
if valueType {
row = append(row, secret.RawValueType.Type)
}
row = append(row, rawValue)
}

Expand Down

0 comments on commit c3f46cd

Please sign in to comment.