Skip to content
Open
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 docs/reference/manual/hcloud_context_list.md

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

43 changes: 40 additions & 3 deletions internal/cmd/context/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/hetznercloud/cli/internal/cmd/output"
"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/cli/internal/state/config"
)

type presentation struct {
Expand All @@ -16,6 +17,13 @@ type presentation struct {
Active string
}

type schemaPresentation struct {
Name string `json:"name"`
Token string `json:"token"`
Active bool `json:"active"`
Preferences map[string]any `json:"preferences,omitempty"`
}

func NewListCommand(s state.State) *cobra.Command {
cols := newListOutputTable(io.Discard).Columns()
cmd := &cobra.Command{
Expand All @@ -30,19 +38,49 @@ func NewListCommand(s state.State) *cobra.Command {
DisableFlagsInUseLine: true,
RunE: state.Wrap(s, runList),
}
output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(cols))
output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(cols), output.OptionJSON(), output.OptionYAML())
return cmd
}

func runList(s state.State, cmd *cobra.Command, _ []string) error {
cfg := s.Config()
outOpts := output.FlagsForCommand(cmd)

cols := []string{"active", "name"}
if outOpts.IsSet("columns") {
cols = outOpts["columns"]
}

tw := newListOutputTable(cmd.OutOrStdout())
quiet, err := config.OptionQuiet.Get(s.Config())
if err != nil {
return err
}

out := cmd.OutOrStdout()
if quiet {
// If we are in quiet mode, we saved the original output in cmd.errWriter. We can now restore it.
out = cmd.ErrOrStderr()
}

isSchema := outOpts.IsSet("json") || outOpts.IsSet("yaml")
if isSchema {
contexts, activeCtx := cfg.Contexts(), cfg.ActiveContext()
schema := make([]schemaPresentation, 0, len(contexts))
for _, ctx := range contexts {
schema = append(schema, schemaPresentation{
Name: ctx.Name(),
Token: ctx.Token(),
Active: ctx == activeCtx,
Preferences: ctx.Preferences(),
})
}
if outOpts.IsSet("json") {
return util.DescribeJSON(out, schema)
}
return util.DescribeYAML(out, schema)
}

tw := newListOutputTable(out)
warnings, err := tw.ValidateColumns(cols)
if err != nil {
return err
Expand All @@ -54,7 +92,6 @@ func runList(s state.State, cmd *cobra.Command, _ []string) error {
if !outOpts.IsSet("noheader") {
tw.WriteHeader(cols)
}
cfg := s.Config()
for _, context := range cfg.Contexts() {
presentation := presentation{
Name: context.Name(),
Expand Down
23 changes: 23 additions & 0 deletions internal/cmd/context/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,29 @@ my-third-context
my-context
* my-other-context
my-third-context
`,
},
{
name: "json",
args: []string{"-o=json"},
config: testConfig,
expOut: `[
{
"name": "my-context",
"token": "super secret token",
"active": true
},
{
"name": "my-other-context",
"token": "another super secret token",
"active": false
},
{
"name": "my-third-context",
"token": "yet another super secret token",
"active": false
}
]
`,
},
}
Expand Down
Loading