Skip to content

Commit

Permalink
feat(credentials.go): change tablewriter for credentials show
Browse files Browse the repository at this point in the history
  • Loading branch information
vdice committed Jun 7, 2019
1 parent 37f8553 commit 2a77431
Show file tree
Hide file tree
Showing 15 changed files with 2,425 additions and 39 deletions.
18 changes: 18 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 42 additions & 32 deletions pkg/porter/credentials.go
Expand Up @@ -15,6 +15,8 @@ import (

dtprinter "github.com/carolynvs/datetime-printer"
credentials "github.com/deislabs/cnab-go/credentials"

tablewriter "github.com/olekukonko/tablewriter"
"github.com/pkg/errors"
yaml "gopkg.in/yaml.v2"
)
Expand Down Expand Up @@ -249,43 +251,51 @@ func (p *Porter) ShowCredential(opts CredentialShowOptions) error {
case printer.FormatYaml:
return printer.PrintYaml(p.Out, credSet)
case printer.FormatTable:
printCredentialRow :=
func(v interface{}) []interface{} {
cs, ok := v.(credentials.CredentialStrategy)
if !ok {
return nil
}

// Build a reflected Source of type reflectedStruct, for use below
reflectedSource := reflectedStruct{
Value: reflect.ValueOf(cs.Source),
Type: reflect.TypeOf(cs.Source),
}
var data [][]string

// Iterate through all CredentialStrategies to build up our data set
for _, cs := range credSet.Credentials {
// Build a reflected Source of type reflectedStruct, for use below
reflectedSource := reflectedStruct{
Value: reflect.ValueOf(cs.Source),
Type: reflect.TypeOf(cs.Source),
}

// Determine the source type by seeing which reflected source's field
// corresponds to a non-empty reflected source value
var source string
var sourceType string
// Iterate through all of the fields of a credentials.Source struct
for i := 0; i < reflectedSource.Type.NumField(); i++ {
// A Field name would be 'Path', 'EnvVar', etc.
fieldName := reflectedSource.Type.Field(i).Name
// Get the value for said Field
fieldValue := reflect.Indirect(reflectedSource.Value).FieldByName(fieldName).String()
// If not empty, this field value and name represent our source and source type, respectively
if fieldValue != "" {
source = fieldValue
sourceType = fieldName
}
// Determine the source type ('Path', 'EnvVar', etc.) by seeing which
// reflected source's field corresponds to a non-empty reflected source value

// Iterate through all of the fields of a credentials.Source struct
for i := 0; i < reflectedSource.Type.NumField(); i++ {
// A Field name would be 'Path', 'EnvVar', etc.
fieldName := reflectedSource.Type.Field(i).Name
// Get the value for said Field
fieldValue := reflect.Indirect(reflectedSource.Value).FieldByName(fieldName).String()
// If not empty, this field value and name represent our source and source type, respectively
if fieldValue != "" {
data = append(data, []string{cs.Name, fieldValue, fieldName})
}
return []interface{}{cs.Name, source, sourceType}
}
}

// Build and configure our tablewriter
table := tablewriter.NewWriter(p.Out)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetBorders(tablewriter.Border{Left: false, Right: false, Bottom: false, Top: true})
table.SetAutoFormatHeaders(false)

// First, print the CredentialSet name
fmt.Fprintf(p.Out, "Name: %s\n\n", credSet.Name)
fmt.Fprintln(p.Out, "Credential Mappings")
fmt.Fprintln(p.Out, "===================")
return printer.PrintTable(p.Out, credSet.Credentials, printCredentialRow,
"Name", "Local Source", "Source Type")

// Now print the table
table.SetHeader([]string{"Name", "Local Source", "Source Type"})
for _, v := range data {
table.Append(v)
}
table.Render()
return nil
default:
return fmt.Errorf("invalid format: %s", opts.Format)
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/porter/credentials_test.go
Expand Up @@ -388,13 +388,13 @@ credentials:
format: printer.FormatTable,
wantOutput: `Name: kool-kreds
Credential Mappings
===================
Name Local Source Source Type
kool-config /path/to/kool-config Path
kool-envvar KOOL_ENV_VAR EnvVar
kool-cmd echo 'kool' Command
kool-val kool Value
------------------------------------------------
Name Local Source Source Type
------------------------------------------------
kool-config /path/to/kool-config Path
kool-envvar KOOL_ENV_VAR EnvVar
kool-cmd echo 'kool' Command
kool-val kool Value
`,
},
}
Expand Down
21 changes: 21 additions & 0 deletions vendor/github.com/mattn/go-runewidth/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2a77431

Please sign in to comment.