diff --git a/flow/cmd/list.go b/flow/cmd/list.go index 87eb3da77..6b2e03454 100644 --- a/flow/cmd/list.go +++ b/flow/cmd/list.go @@ -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 { @@ -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 + }, + ) } diff --git a/lib/datamodel/cmd/list.go b/lib/datamodel/cmd/list.go index 041bf7386..fb25d25a7 100644 --- a/lib/datamodel/cmd/list.go +++ b/lib/datamodel/cmd/list.go @@ -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)) diff --git a/lib/datamodel/cmd/utils.go b/lib/datamodel/cmd/utils.go index 9bcb3dacb..bf77f8dda 100644 --- a/lib/datamodel/cmd/utils.go +++ b/lib/datamodel/cmd/utils.go @@ -14,6 +14,4 @@ func findMaxLabelLen(R *runtime.Runtime, dflags flags.DatamodelPflagpole) int { } } return max -} - - +} \ No newline at end of file diff --git a/lib/gen/cmd/list.go b/lib/gen/cmd/list.go index e3babc9c3..b8f1ee55b 100644 --- a/lib/gen/cmd/list.go +++ b/lib/gen/cmd/list.go @@ -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 { @@ -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 + }, + ) } diff --git a/lib/hof/find.go b/lib/hof/find.go index 6b8e09ab8..4da9f4e83 100644 --- a/lib/hof/find.go +++ b/lib/hof/find.go @@ -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": diff --git a/lib/datamodel/cmd/table.go b/lib/yagu/table.go similarity index 73% rename from lib/datamodel/cmd/table.go rename to lib/yagu/table.go index e4bf29f34..68f8a3a87 100644 --- a/lib/datamodel/cmd/table.go +++ b/lib/yagu/table.go @@ -1,4 +1,4 @@ -package cmd +package yagu import ( "os" @@ -6,7 +6,7 @@ import ( "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) @@ -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 {