Skip to content

Commit

Permalink
feat(featctl/cmd): add format for command list entity
Browse files Browse the repository at this point in the history
  • Loading branch information
jinghancc committed Nov 4, 2021
1 parent a8e9125 commit 2507715
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
56 changes: 51 additions & 5 deletions featctl/cmd/list_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,29 @@ package cmd
import (
"context"
"encoding/csv"
"fmt"
"log"
"os"
"strconv"
"time"

"github.com/olekukonko/tablewriter"
"github.com/oom-ai/oomstore/pkg/oomstore/types"
"github.com/spf13/cobra"
)

var listEntityOutput *string
var listEntityCmd = &cobra.Command{
Use: "entity",
Short: "list all existing entities",
PreRun: func(cmd *cobra.Command, args []string) {
if !cmd.Flags().Changed("output") {
listEntityOutput = stringPtr(ASCIITable)
}
if !cmd.Flags().Changed("limit") {
getHistoricalFeatureOpt.Limit = nil
}
},
Run: func(cmd *cobra.Command, args []string) {
ctx := context.Background()
oomStore := mustOpenOomStore(ctx, oomStoreCfg)
Expand All @@ -26,28 +37,63 @@ var listEntityCmd = &cobra.Command{
}

// print entities to stdout
if err := printEntities(entities); err != nil {
if err := printEntities(entities, *listEntityOutput); err != nil {
log.Fatalf("failing printing entities, error %v\n", err)
}
},
}

func init() {
listCmd.AddCommand(listEntityCmd)

flags := listEntityCmd.Flags()

listEntityOutput = flags.StringP("output", "o", "", "output format")
}

func printEntities(entities []*types.Entity) error {
func printEntities(entities []*types.Entity, output string) error {
switch output {
case CSV:
return printEntitiesInCSV(entities)
case ASCIITable:
return printEntitiesInASCIITable(entities)
default:
return fmt.Errorf("unsupported output format %s", output)
}
}

func printEntitiesInCSV(entities []*types.Entity) error {
w := csv.NewWriter(os.Stdout)
if err := w.Write([]string{"Name", "Length", "Description", "CreateTime", "ModifyTime"}); err != nil {
if err := w.Write(entityHeader()); err != nil {
return err
}
for _, entity := range entities {
if err := w.Write([]string{entity.Name, strconv.Itoa(entity.Length), entity.Description, entity.CreateTime.Format(time.RFC3339),
entity.ModifyTime.Format(time.RFC3339)}); err != nil {
if err := w.Write(entityRecord(entity)); err != nil {
return err
}
}

w.Flush()
return nil
}

func printEntitiesInASCIITable(entities []*types.Entity) error {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(entityHeader())
table.SetAutoFormatHeaders(false)

for _, entity := range entities {
table.Append(entityRecord(entity))
}
table.Render()
return nil
}

func entityRecord(entity *types.Entity) []string {
return []string{entity.Name, strconv.Itoa(entity.Length), entity.Description, entity.CreateTime.Format(time.RFC3339),
entity.ModifyTime.Format(time.RFC3339)}
}

func entityHeader() []string {
return []string{"Name", "Length", "Description", "CreateTime", "ModifyTime"}
}
9 changes: 9 additions & 0 deletions featctl/cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,19 @@ import (
"github.com/oom-ai/oomstore/pkg/oomstore/types"
)

const (
CSV = "csv"
ASCIITable = "ascii_table"
)

func mustOpenOomStore(ctx context.Context, opt types.OomStoreConfig) *oomstore.OomStore {
store, err := oomstore.Open(ctx, oomStoreCfg)
if err != nil {
log.Fatalf("failed opening OomStore: %v", err)
}
return store
}

func stringPtr(s string) *string {
return &s
}
2 changes: 1 addition & 1 deletion featctl/test/test_list_entity.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ expected='Name,Length,Description,CreateTime,ModifyTime
device,32,,2021-10-19T06:56:07Z,2021-10-19T06:56:07Z
user,64,all users,2021-10-19T06:56:07Z,2021-10-19T06:56:07Z
'
actual=$(featctl list entity)
actual=$(featctl list entity -o csv)
ignore_time() { cut -d ',' -f 1-3 <<<"$1"; }
assert_eq "$case" "$(ignore_time "$expected" | sort)" "$(ignore_time "$actual" | sort)"

0 comments on commit 2507715

Please sign in to comment.