Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/state/internal/cmdtree/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func newSecretsCommand(secretsClient *secretsapi.Client, prime *primer.Values) *

ccmd := captain.NewCommand(
"secrets",
locale.Tl("secrets_title", "Listing Secrets"),
locale.Tl("secrets_title", "Secrets"),
locale.T("secrets_cmd_description"),
prime.Output(),
[]*captain.Flag{
Expand Down
10 changes: 10 additions & 0 deletions internal/runners/secrets/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ type Get struct {
out output.Outputer
}

// SecretExport defines important information about a secret that should be
// displayed.
type SecretExport struct {
Name string `json:"name"`
Scope string `json:"scope"`
Description string `json:"description"`
HasValue bool `json:"has_value"`
Value string `json:"value,omitempty"`
}

// NewGet prepares a get execution context for use.
func NewGet(p getPrimeable) *Get {
return &Get{
Expand Down
101 changes: 31 additions & 70 deletions internal/runners/secrets/secrets.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package secrets

import (
"encoding/json"
"fmt"

"github.com/bndr/gotabulate"

"github.com/ActiveState/cli/internal/access"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/locale"
"github.com/ActiveState/cli/internal/logging"
"github.com/ActiveState/cli/internal/output"
Expand Down Expand Up @@ -35,6 +31,14 @@ type List struct {
proj *project.Project
}

type secretData struct {
Name string `locale:"name,[HEADING]Name[/RESET]"`
Scope string `locale:"scope,[HEADING]Scope[/RESET]"`
Description string `locale:"description,[HEADING]Description[/RESET]"`
HasValue string `locale:"hasvalue,[HEADING]Value[/RESET]"`
Usage string `locale:"usage,[HEADING]Usage[/RESET]"`
}

// NewList prepares a list execution context for use.
func NewList(client *secretsapi.Client, p listPrimeable) *List {
return &List{
Expand All @@ -54,12 +58,17 @@ func (l *List) Run(params ListRunParams) error {
if err != nil {
return locale.WrapError(err, "secrets_err_defined")
}
exports, err := defsToSecrets(defs)

meta, err := defsToData(defs)
if err != nil {
return locale.WrapError(err, "secrets_err_values")
}

l.out.Print(secretExports(exports))
l.out.Print(struct {
Data []*secretData `opts:"verticalTable" locale:","`
}{
Data: meta,
})

return nil
}
Expand Down Expand Up @@ -119,30 +128,8 @@ func filterSecrets(proj *project.Project, secrectDefs []*secretsModels.SecretDef
return secrectDefsFiltered
}

type secretExports []*SecretExport

func (es secretExports) MarshalOutput(format output.Format) interface{} {
switch format {
case output.JSONFormatName, output.EditorV0FormatName, output.EditorFormatName:
return es

default:
rows, err := secretsToRows(es)
if err != nil {
return locale.WrapError(err, "secrets_err_output")
}

t := gotabulate.Create(rows)
t.SetHeaders([]string{locale.T("secrets_header_name"), locale.T("secrets_header_scope"), locale.T("secrets_header_value"), locale.T("secrets_header_description"), locale.T("secrets_header_usage")})
t.SetHideLines([]string{"betweenLine", "top", "aboveTitle", "LineTop", "LineBottom", "bottomLine"}) // Don't print whitespace lines
t.SetAlign("left")

return t.Render("simple")
}
}

func defsToSecrets(defs []*secretsModels.SecretDefinition) ([]*SecretExport, error) {
secretsExport := make([]*SecretExport, len(defs))
func defsToData(defs []*secretsModels.SecretDefinition) ([]*secretData, error) {
data := make([]*secretData, len(defs))
expander := project.NewSecretExpander(secretsapi.Get(), project.Get(), nil)

for i, def := range defs {
Expand All @@ -151,54 +138,28 @@ func defsToSecrets(defs []*secretsModels.SecretDefinition) ([]*SecretExport, err
continue
}

secretValue, err := expander.FindSecret(*def.Name, *def.Scope == secretsModels.SecretDefinitionScopeUser)
if err != nil {
return secretsExport, err
}

secretsExport[i] = &SecretExport{
data[i] = &secretData{
Name: *def.Name,
Scope: *def.Scope,
Description: def.Description,
HasValue: secretValue != nil,
HasValue: locale.T("secrets_row_value_unset"),
Usage: fmt.Sprintf("%s.%s", *def.Scope, *def.Name),
}
}

return secretsExport, nil
}

func secretsAsJSON(secretExports []*SecretExport) ([]byte, error) {
bs, err := json.Marshal(secretExports)
if err != nil {
return nil, errs.Wrap(err, "Marshal failure")
}

return bs, nil
}
if data[i].Description == "" {
data[i].Description = locale.T("secrets_description_unset")
}

// secretsToRows returns the rows used in our output table
func secretsToRows(secretExports []*SecretExport) ([][]interface{}, error) {
rows := [][]interface{}{}
for _, secret := range secretExports {
description := "-"
if secret.Description != "" {
description = secret.Description
secretValue, err := expander.FindSecret(*def.Name, *def.Scope == secretsModels.SecretDefinitionScopeUser)
if err != nil {
logging.Debug("Could not determine secret value, got error: %v", err)
continue
}
hasValue := locale.T("secrets_row_value_set")
if !secret.HasValue {
hasValue = locale.T("secrets_row_value_unset")

if secretValue != nil {
data[i].HasValue = locale.T("secrets_row_value_set")
}
rows = append(rows, []interface{}{secret.Name, secret.Scope, hasValue, description, fmt.Sprintf("%s.%s", secret.Scope, secret.Name)})
}
return rows, nil
}

// SecretExport defines important information about a secret that should be
// displayed.
type SecretExport struct {
Name string `json:"name"`
Scope string `json:"scope"`
Description string `json:"description"`
HasValue bool `json:"has_value"`
Value string `json:"value,omitempty"`
return data, nil
}
6 changes: 4 additions & 2 deletions locale/en-us.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -631,9 +631,11 @@ secrets_header_description:
secrets_header_usage:
other: Usage
secrets_row_value_set:
other: Defined
other: "[SUCCESS]Defined[/RESET]"
secrets_row_value_unset:
other: Undefined
other: "[ERROR]Not defined[/RESET]"
Comment on lines +634 to +636
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are different from the mock as the output was empty with the special characters. Filed a story here: https://www.pivotaltracker.com/story/show/176372179

secrets_description_unset:
other: "[DISABLED]Not provided.[/RESET]"
pull_not_updated:
other: Your activestate.yaml is already up to date!
pull_updated:
Expand Down