Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hof/cli: change list commands to use a table formatter #372

Merged
merged 2 commits into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading