Skip to content

Commit

Permalink
Implement list -o simple
Browse files Browse the repository at this point in the history
Signed-off-by: Daichi Sakaue <[email protected]>
  • Loading branch information
yokaze committed Apr 5, 2024
1 parent 885f0f1 commit 7990415
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
6 changes: 3 additions & 3 deletions cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ var dumpCmd = &cobra.Command{

Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return runDump(context.Background(), args[0])
return runDump(context.Background(), cmd.OutOrStdout(), args[0])
},
}

func runDump(ctx context.Context, name string) error {
func runDump(ctx context.Context, w io.Writer, name string) error {
clientset, dynamicClient, _, err := createClients(ctx, name)
if err != nil {
return err
Expand Down Expand Up @@ -54,6 +54,6 @@ func runDump(ctx context.Context, name string) error {

var buf bytes.Buffer
json.Indent(&buf, data, "", " ")
fmt.Println(buf.String())
buf.WriteTo(w)
return nil
}
33 changes: 26 additions & 7 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"strconv"
"strings"
"text/tabwriter"

"github.com/cilium/cilium/api/v1/client/endpoint"
"github.com/spf13/cobra"
Expand All @@ -22,7 +24,7 @@ var listCmd = &cobra.Command{

Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return runList(context.Background(), args[0])
return runList(context.Background(), cmd.OutOrStdout(), args[0])
},
}

Expand Down Expand Up @@ -55,7 +57,7 @@ func parseDerivedFromEntry(input []string, direction string) derivedFromEntry {
return val
}

func runList(ctx context.Context, name string) error {
func runList(ctx context.Context, w io.Writer, name string) error {
_, dynamicClient, client, err := createClients(ctx, name)
if err != nil {
return err
Expand Down Expand Up @@ -91,11 +93,28 @@ func runList(ctx context.Context, name string) error {
}
}

text, err := json.MarshalIndent(policyList, "", " ")
if err != nil {
switch rootOptions.output {
case OutputJson:
text, err := json.MarshalIndent(policyList, "", " ")
if err != nil {
return err
}
_, err = w.Write(text)
return err
case OutputSimple:
tw := tabwriter.NewWriter(w, 0, 1, 1, ' ', 0)
_, err := tw.Write([]byte("DIRECTION\tKIND\tNAMESPACE\tNAME\n"))
if err != nil {
return err
}
for _, p := range policyList {
_, err := tw.Write([]byte(fmt.Sprintf("%v\t%v\t%v\t%v\n", p.Direction, p.Kind, p.Namespace, p.Name)))
if err != nil {
return err
}
}
return tw.Flush()
default:
return fmt.Errorf("unknown format: %s", rootOptions.output)
}

fmt.Println(string(text))
return nil
}
7 changes: 6 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import (
"github.com/spf13/cobra"
)

const (
OutputJson = "json"
OutputSimple = "simple"
)

var rootOptions struct {
namespace string
proxySelector string
Expand All @@ -18,7 +23,7 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&rootOptions.namespace, "namespace", "n", "default", "namespace of a pod")
rootCmd.PersistentFlags().StringVar(&rootOptions.proxySelector, "proxy-selector", "app.kubernetes.io/name=cilium-agent-proxy", "label selector to find the proxy pods")
rootCmd.PersistentFlags().Uint16Var(&rootOptions.proxyPort, "proxy-port", 8080, "port number of the proxy endpoints")
rootCmd.PersistentFlags().StringVarP(&rootOptions.output, "output", "o", "json", "output format")
rootCmd.PersistentFlags().StringVarP(&rootOptions.output, "output", "o", OutputSimple, "output format")
}

var rootCmd = &cobra.Command{}
Expand Down
2 changes: 1 addition & 1 deletion e2e/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func testList() {
It("should list applied policies", func() {
for _, c := range cases {
podName := onePodByLabelSelector(Default, "default", c.Selector)
result := runViewerSafe(Default, nil, "list", podName)
result := runViewerSafe(Default, nil, "list", "-o=json", podName)
testJson(Default, result, c.Expected)
}
})
Expand Down

0 comments on commit 7990415

Please sign in to comment.