Skip to content

Commit

Permalink
chore: add json/yaml output to CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuildthecloud committed Nov 13, 2024
1 parent f309fe9 commit c04c290
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 21 deletions.
11 changes: 8 additions & 3 deletions pkg/cli/agents.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
)

type Agents struct {
root *Otto8
Quiet bool `usage:"Only print IDs of agents" short:"q"`
Wide bool `usage:"Print more information" short:"w"`
root *Otto8
Quiet bool `usage:"Only print IDs of agents" short:"q"`
Wide bool `usage:"Print more information" short:"w"`
Output string `usage:"Output format (table, json, yaml)" short:"o" default:"table"`
}

func (l *Agents) Customize(cmd *cobra.Command) {
Expand All @@ -24,6 +25,10 @@ func (l *Agents) Run(cmd *cobra.Command, args []string) error {
return err
}

if ok, err := output(l.Output, agents); ok || err != nil {
return err
}

if l.Quiet {
for _, agent := range agents.Items {
fmt.Println(agent.ID)
Expand Down
30 changes: 30 additions & 0 deletions pkg/cli/output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cli

import (
"encoding/json"
"fmt"
"os"

"sigs.k8s.io/yaml"
)

func output(format string, obj any) (bool, error) {
if format == "" || format == "table" {
return false, nil
}
switch format {
case "json":
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
return true, enc.Encode(obj)
case "yaml":
data, err := yaml.Marshal(obj)
if err != nil {
return false, err
}
_, err = os.Stdout.Write(data)
return true, err
default:
return false, fmt.Errorf("unsupported output format: %s", format)
}
}
2 changes: 1 addition & 1 deletion pkg/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func New() *cobra.Command {
cmd.Command(&Tools{root: root},
&ToolUnregister{root: root},
&ToolRegister{root: root},
&StepTemplateUpdate{root: root}),
&ToolUpdate{root: root}),
&Webhooks{root: root},
&Server{},
&Version{},
Expand Down
10 changes: 7 additions & 3 deletions pkg/cli/runs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import (

type Runs struct {
root *Otto8
Wide bool `usage:"Print more information" short:"w"`
Quiet bool `usage:"Only print IDs of runs" short:"q"`
Follow bool `usage:"Follow the output of runs" short:"f"`
Wide bool `usage:"Print more information" short:"w"`
Quiet bool `usage:"Only print IDs of runs" short:"q"`
Output string `usage:"Output format (table, json, yaml)" short:"o" default:"table"`
Follow bool `usage:"Follow the output of runs" short:"f"`
}

func (l *Runs) Customize(cmd *cobra.Command) {
Expand Down Expand Up @@ -110,6 +111,9 @@ func (l *Runs) Run(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
if ok, err := output(l.Output, runs); ok || err != nil {
return err
}
list = sliceToIter(runs.Items)
}

Expand Down
12 changes: 9 additions & 3 deletions pkg/cli/threads.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
)

type Threads struct {
root *Otto8
Quiet bool `usage:"Only print IDs of threads" short:"q"`
Wide bool `usage:"Print more information" short:"w"`
root *Otto8
Quiet bool `usage:"Only print IDs of threads" short:"q"`
Wide bool `usage:"Print more information" short:"w"`
Output string `usage:"Output format (table, json, yaml)" short:"o" default:"table"`
}

func (l *Threads) Customize(cmd *cobra.Command) {
Expand All @@ -25,11 +26,16 @@ func (l *Threads) Run(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
opts.AgentID = args[0]
}

threads, err := l.root.Client.ListThreads(cmd.Context(), opts)
if err != nil {
return err
}

if ok, err := output(l.Output, threads); ok || err != nil {
return err
}

if l.Quiet {
for _, thread := range threads.Items {
fmt.Println(thread.ID)
Expand Down
9 changes: 7 additions & 2 deletions pkg/cli/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
)

type Tools struct {
root *Otto8
Quiet bool `usage:"Only print IDs of tools" short:"q"`
root *Otto8
Quiet bool `usage:"Only print IDs of tools" short:"q"`
Output string `usage:"Output format (table, json, yaml)" short:"o" default:"table"`
}

func (l *Tools) Customize(cmd *cobra.Command) {
Expand All @@ -24,6 +25,10 @@ func (l *Tools) Run(cmd *cobra.Command, args []string) error {
return err
}

if ok, err := output(l.Output, toolRefs); ok || err != nil {
return err
}

if l.Quiet {
for _, toolRef := range toolRefs.Items {
fmt.Println(toolRef.ID)
Expand Down
6 changes: 3 additions & 3 deletions pkg/cli/steptemplates_update.go → pkg/cli/tools_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import (
"github.com/spf13/cobra"
)

type StepTemplateUpdate struct {
type ToolUpdate struct {
root *Otto8
Quiet bool `usage:"Only print IDs of updated step template" short:"q"`
}

func (l *StepTemplateUpdate) Customize(cmd *cobra.Command) {
func (l *ToolUpdate) Customize(cmd *cobra.Command) {
cmd.Use = "update [flags] NAME REFERENCE"
cmd.Args = cobra.ExactArgs(2)
}

func (l *StepTemplateUpdate) Run(cmd *cobra.Command, args []string) error {
func (l *ToolUpdate) Run(cmd *cobra.Command, args []string) error {
tr, err := l.root.Client.UpdateToolReference(cmd.Context(), args[0], args[1])
if err != nil {
return err
Expand Down
11 changes: 8 additions & 3 deletions pkg/cli/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
)

type Webhooks struct {
root *Otto8
Quiet bool `usage:"Only print IDs of agents" short:"q"`
Wide bool `usage:"Print more information" short:"w"`
root *Otto8
Quiet bool `usage:"Only print IDs of agents" short:"q"`
Wide bool `usage:"Print more information" short:"w"`
Output string `usage:"Output format (table, json, yaml)" short:"o" default:"table"`
}

func (l *Webhooks) Customize(cmd *cobra.Command) {
Expand All @@ -23,6 +24,10 @@ func (l *Webhooks) Run(cmd *cobra.Command, args []string) error {
return err
}

if ok, err := output(l.Output, whs); ok || err != nil {
return err
}

if l.Quiet {
for _, webhook := range whs.Items {
fmt.Println(webhook.ID)
Expand Down
11 changes: 8 additions & 3 deletions pkg/cli/workflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
)

type Workflows struct {
root *Otto8
Quiet bool `usage:"Only print IDs of agents" short:"q"`
Wide bool `usage:"Print more information" short:"w"`
root *Otto8
Quiet bool `usage:"Only print IDs of agents" short:"q"`
Wide bool `usage:"Print more information" short:"w"`
Output string `usage:"Output format (table, json, yaml)" short:"o" default:"table"`
}

func (l *Workflows) Customize(cmd *cobra.Command) {
Expand All @@ -24,6 +25,10 @@ func (l *Workflows) Run(cmd *cobra.Command, args []string) error {
return err
}

if ok, err := output(l.Output, wfs); ok || err != nil {
return err
}

if l.Quiet {
for _, agent := range wfs.Items {
fmt.Println(agent.ID)
Expand Down

0 comments on commit c04c290

Please sign in to comment.