Skip to content

Commit

Permalink
feat(featctl/cmd): add format for command list feature
Browse files Browse the repository at this point in the history
  • Loading branch information
jinghancc committed Nov 4, 2021
1 parent 39c56af commit 57d0ff8
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
76 changes: 72 additions & 4 deletions featctl/cmd/list_feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ package cmd

import (
"context"
"encoding/csv"
"fmt"
"log"
"os"
"time"

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

var listFeatureOpt types.ListFeatureOpt
var listFeatureOutput *string

var listFeatureCmd = &cobra.Command{
Use: "feature",
Expand All @@ -21,6 +26,9 @@ var listFeatureCmd = &cobra.Command{
if !cmd.Flags().Changed("group") {
listFeatureOpt.GroupName = nil
}
if !cmd.Flags().Changed("output") {
listFeatureOutput = stringPtr(ASCIITable)
}
},
Run: func(cmd *cobra.Command, args []string) {
ctx := context.Background()
Expand All @@ -32,10 +40,9 @@ var listFeatureCmd = &cobra.Command{
log.Fatalf("failed listing features given option %v, error %v\n", listFeatureOpt, err)
}

// print csv to stdout
fmt.Println(types.FeatureCsvHeader())
for _, feature := range features {
fmt.Println(feature.ToCsvRecord())
// print features to stdout
if err := printFeatures(features, *listFeatureOutput); err != nil {
log.Fatalf("failed printing features, error %v\n", err)
}
},
}
Expand All @@ -47,4 +54,65 @@ func init() {

listFeatureOpt.EntityName = flags.StringP("entity", "e", "", "entity")
listFeatureOpt.GroupName = flags.StringP("group", "g", "", "feature group")
listFeatureOutput = flags.StringP("output", "o", "", "output format")
}

func printFeatures(features types.FeatureList, output string) error {
switch output {
case CSV:
return printFeaturesInCSV(features)
case ASCIITable:
return printFeaturesInASCIITable(features)
default:
return fmt.Errorf("unsupported output format %s", output)
}
}

func printFeaturesInCSV(features types.FeatureList) error {
w := csv.NewWriter(os.Stdout)
if err := w.Write(featureHeader()); err != nil {
return err
}
for _, feature := range features {
if err := w.Write(featureRecord(feature)); err != nil {
return err
}
}

w.Flush()
return nil
}

func printFeaturesInASCIITable(features types.FeatureList) error {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(featureHeader())
table.SetAutoFormatHeaders(false)

for _, feature := range features {
table.Append(featureRecord(feature))
}
table.Render()
return nil
}

func featureHeader() []string {
return []string{"Name", "Group", "Entity", "Category", "DBValueType", "ValueType", "Description", "OnlineRevision", "OfflineLatestRevision", "OfflineLatestDataTable", "CreateTime", "ModifyTime"}
}

func featureRecord(f *types.Feature) []string {
onlineRevision := "<NULL>"
offlineRevision := "<NULL>"
offlineDataTable := "<NULL>"

if f.OnlineRevision != nil {
onlineRevision = fmt.Sprint(*f.OnlineRevision)
}
if f.OfflineRevision != nil {
offlineRevision = fmt.Sprint(*f.OfflineRevision)
}
if f.OfflineDataTable != nil {
offlineDataTable = *f.OfflineDataTable
}

return []string{f.Name, f.GroupName, f.EntityName, f.Category, f.DBValueType, f.ValueType, f.Description, onlineRevision, offlineRevision, offlineDataTable, f.CreateTime.Format(time.RFC3339), f.ModifyTime.Format(time.RFC3339)}
}
2 changes: 1 addition & 1 deletion featctl/test/test_list_feature.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ expected='Name,Group,Entity,Category,DBValueType,ValueType,Description,OnlineRev
model,phone,device,batch,varchar(32),string,,1634626568,1634626568,phone_1634626568,2021-10-19T06:56:07Z,2021-10-19T06:56:07Z
price,phone,device,batch,int,int32,1634626568,1634626568,phone_1634626568,2021-10-19T06:56:07Z,2021-10-19T06:56:07Z
'
actual=$(featctl list feature)
actual=$(featctl list feature -o csv)
ignore_time() { cut -d ',' -f 1-6 <<<"$1"; }
assert_eq "$case" "$(ignore_time "$expected" | sort)" "$(ignore_time "$actual" | sort)"

0 comments on commit 57d0ff8

Please sign in to comment.