Skip to content

Commit

Permalink
hof/cli: change list commands to use a table formatter (#372)
Browse files Browse the repository at this point in the history
Co-authored-by: Tony Worm <[email protected]>
  • Loading branch information
verdverm and Tony Worm authored May 18, 2024
1 parent 0b6a23a commit 8fb0711
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 47 deletions.
47 changes: 25 additions & 22 deletions flow/cmd/list.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package cmd

import (
"fmt"
"sort"
"github.com/codemodus/kace"
"github.com/olekukonko/tablewriter"

"github.com/hofstadter-io/hof/cmd/hof/flags"
"github.com/hofstadter-io/hof/lib/yagu"
)

func List(args []string, rflags flags.RootPflagpole, cflags flags.FlowPflagpole) error {
Expand All @@ -13,25 +14,27 @@ func List(args []string, rflags flags.RootPflagpole, cflags flags.FlowPflagpole)
return err
}

fmt.Println("Available Generators")
flows := make([]string, 0, len(R.Workflows))
for _, G := range R.Workflows {
fmt.Println(" ", G.Hof.Flow.Name)
flows = append(flows, G.Hof.Flow.Name)
}
sort.Strings(flows)
return yagu.PrintAsTable(
[]string{"Name", "Path", "ID"},
func(table *tablewriter.Table) ([][]string, error) {
var rows = make([][]string, 0, len(R.Workflows))
// fill with data
for _, wf := range R.Workflows {
id := wf.Hof.Metadata.ID
if id == "" {
id = kace.Snake(wf.Hof.Metadata.Name) + " (auto)"
}

name := wf.Hof.Flow.Name
if name == "" {
name = "(anon)"
}
path := wf.Hof.Path

// TODO...
// 1. use table printer
// 2. move this command up, large blocks of this ought
//flows := make([]string, 0, len(R.Workflows))
//for _, G := range R.Workflows {
// flows = append(flows, G.Hof.Flow.Name)
//}
//sort.Strings(flows)
//fmt.Printf("Available Generators\n ")
//fmt.Println(strings.Join(flows, "\n "))

// print gens
return nil
row := []string{name, path, id}
rows = append(rows, row)
}
return rows, nil
},
)
}
3 changes: 2 additions & 1 deletion lib/datamodel/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
"github.com/hofstadter-io/hof/cmd/hof/flags"
"github.com/hofstadter-io/hof/lib/datamodel"
"github.com/hofstadter-io/hof/lib/runtime"
"github.com/hofstadter-io/hof/lib/yagu"
)

func list(R *runtime.Runtime, dflags flags.DatamodelPflagpole) error {
return printAsTable(
return yagu.PrintAsTable(
[]string{"Name", "Type", "Version", "Status", "ID"},
func(table *tablewriter.Table) ([][]string, error) {
var rows = make([][]string, 0, len(R.Datamodels))
Expand Down
4 changes: 1 addition & 3 deletions lib/datamodel/cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,4 @@ func findMaxLabelLen(R *runtime.Runtime, dflags flags.DatamodelPflagpole) int {
}
}
return max
}


}
43 changes: 27 additions & 16 deletions lib/gen/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package cmd

import (
"fmt"
"strings"

"github.com/codemodus/kace"
"github.com/olekukonko/tablewriter"

"github.com/hofstadter-io/hof/cmd/hof/flags"
"github.com/hofstadter-io/hof/lib/yagu"
)

func List(args []string, rflags flags.RootPflagpole, gflags flags.GenFlagpole) error {
Expand All @@ -13,19 +16,27 @@ func List(args []string, rflags flags.RootPflagpole, gflags flags.GenFlagpole) e
return err
}

// TODO...
// 1. use table printer
// 2. move this command up, large blocks of this ought
gens := make([]string, 0, len(R.Generators))
for _, G := range R.Generators {
gens = append(gens, G.Hof.Metadata.Name)
}
if len(gens) == 0 {
return fmt.Errorf("no generators found")
}
fmt.Printf("Available Generators\n ")
fmt.Println(strings.Join(gens, "\n "))

// print gens
return nil
return yagu.PrintAsTable(
[]string{"Name", "Path", "ID", "Creator"},
func(table *tablewriter.Table) ([][]string, error) {
var rows = make([][]string, 0, len(R.Generators))
// fill with data
for _, gen := range R.Generators {
id := gen.Hof.Metadata.ID
if id == "" {
id = kace.Snake(gen.Hof.Metadata.Name) + " (auto)"
}

name := gen.Hof.Gen.Name
if name == "" {
name = "(anon)"
}
path := gen.Hof.Path

row := []string{name, path, id, fmt.Sprint(gen.Hof.Gen.Creator)}
rows = append(rows, row)
}
return rows, nil
},
)
}
4 changes: 4 additions & 0 deletions lib/hof/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ func upgradeAttrs[T any](node *Node[T], label string) bool {
case "gen":
node.Hof.Gen.Root = true
node.Hof.Gen.Name = ac
c := val.LookupPath(cue.ParsePath("Create"))
if c.Exists() {
node.Hof.Gen.Creator = true
}

// this doesnt handle empty @flow()
case "flow":
Expand Down
10 changes: 5 additions & 5 deletions lib/datamodel/cmd/table.go → lib/yagu/table.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package cmd
package yagu

import (
"os"

"github.com/olekukonko/tablewriter"
)

func defaultTableFormat(table *tablewriter.Table) {
func DefaultTableFormat(table *tablewriter.Table) {
table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(true)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
Expand All @@ -20,13 +20,13 @@ func defaultTableFormat(table *tablewriter.Table) {
table.SetNoWhiteSpace(true)
}

type dataPrinter func(table *tablewriter.Table) ([][]string, error)
type DataPrinter func(table *tablewriter.Table) ([][]string, error)

func printAsTable(headers []string, printer dataPrinter) error {
func PrintAsTable(headers []string, printer DataPrinter) error {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(headers)

defaultTableFormat(table)
DefaultTableFormat(table)

rows, err := printer(table)
if err != nil {
Expand Down

0 comments on commit 8fb0711

Please sign in to comment.