Skip to content

Commit

Permalink
feat(cmd/cup): support json output for cup config context
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeMac committed Aug 14, 2023
1 parent 5d19da7 commit c4983ef
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
29 changes: 21 additions & 8 deletions cmd/cup/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,35 @@ func configCommand() *cli.Command {
return err
}

wr := writer()
fmt.Fprintln(wr, "NAME\tADDRESS\tNAMESPACE\tCURRENT\t")
for name, ctx := range cfg.Contexts {
type namedContext struct {
*config.Context
Name string `json:"name"`
}

enc, err := encoder(cfg, func(c *namedContext) [][]string {
var current string
if name == cfg.CurrentContext {
if c.Name == cfg.CurrentContext {
current = "*"
}

namespace := "default"
if ctx.Namespace != "" {
namespace = ctx.Namespace
if c.Namespace != "" {
namespace = c.Namespace
}

fmt.Fprintf(wr, "%s\t%s\t%s\t%s\t\n", name, ctx.Address, namespace, current)
return [][]string{{c.Name, c.Address, namespace, current}}
}, "NAME", "ADDRESS", "NAMESPACE", "CURRENT")
if err != nil {
return err
}

for name, ctx := range cfg.Contexts {
if err := enc.Encode(&namedContext{Name: name, Context: ctx}); err != nil {
return err
}
}
return wr.Flush()

return enc.Flush()
},
Subcommands: []*cli.Command{
{
Expand Down
6 changes: 1 addition & 5 deletions cmd/cup/ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ type tableEncoding[T any] struct {
type rowsFn[T any] func(*T) [][]string

func newTableEncoding[T any](rowFn rowsFn[T], headers ...string) *tableEncoding[T] {
return &tableEncoding[T]{Writer: writer(), rowFn: rowFn, headers: headers}
return &tableEncoding[T]{Writer: tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0), rowFn: rowFn, headers: headers}
}

func (e *tableEncoding[T]) Encode(t *T) error {
Expand Down Expand Up @@ -431,7 +431,3 @@ func getDefintions(cfg config.Config, client *http.Client) (map[string]*core.Res

return definitions, nil
}

func writer() *tabwriter.Writer {
return tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
}

0 comments on commit c4983ef

Please sign in to comment.