From 672a72f9ddafe028d06e8d45bc0f2566c4dd5730 Mon Sep 17 00:00:00 2001 From: Gaurav Gahlot Date: Sat, 26 Dec 2020 11:33:20 +0530 Subject: [PATCH 1/4] rename hardware/all.go to hardware/list.go Signed-off-by: Gaurav Gahlot --- cmd/tink-cli/cmd/hardware/{all.go => list.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename cmd/tink-cli/cmd/hardware/{all.go => list.go} (100%) diff --git a/cmd/tink-cli/cmd/hardware/all.go b/cmd/tink-cli/cmd/hardware/list.go similarity index 100% rename from cmd/tink-cli/cmd/hardware/all.go rename to cmd/tink-cli/cmd/hardware/list.go From 261266e03b2263199e956bd31a8a08f690964ffe Mon Sep 17 00:00:00 2001 From: Gaurav Gahlot Date: Mon, 28 Dec 2020 13:22:21 +0530 Subject: [PATCH 2/4] tink hardware list -q Signed-off-by: Gaurav Gahlot --- cmd/tink-cli/cmd/hardware/list.go | 51 ++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/cmd/tink-cli/cmd/hardware/list.go b/cmd/tink-cli/cmd/hardware/list.go index 48737c48b..487c69a13 100644 --- a/cmd/tink-cli/cmd/hardware/list.go +++ b/cmd/tink-cli/cmd/hardware/list.go @@ -2,6 +2,7 @@ package hardware import ( "context" + "fmt" "io" "log" "os" @@ -12,35 +13,55 @@ import ( "github.com/tinkerbell/tink/protos/hardware" ) +var ( + quiet bool + t table.Writer +) + // listCmd represents the list command var listCmd = &cobra.Command{ Use: "list", Short: "list all known hardware", Run: func(cmd *cobra.Command, args []string) { - - t := table.NewWriter() + if quiet { + listHardware() + return + } + t = table.NewWriter() t.SetOutputMirror(os.Stdout) t.AppendHeader(table.Row{"ID", "MAC Address", "IP address", "Hostname"}) + listHardware() + t.Render() + }, +} - list, err := client.HardwareClient.All(context.Background(), &hardware.Empty{}) - if err != nil { - log.Fatal(err) - } +func listHardware() { + list, err := client.HardwareClient.All(context.Background(), &hardware.Empty{}) + if err != nil { + log.Fatal(err) + } - var hw *hardware.Hardware - for hw, err = list.Recv(); err == nil && hw != nil; hw, err = list.Recv() { - for _, iface := range hw.GetNetwork().GetInterfaces() { + var hw *hardware.Hardware + for hw, err = list.Recv(); err == nil && hw != nil; hw, err = list.Recv() { + for _, iface := range hw.GetNetwork().GetInterfaces() { + if quiet { + fmt.Println(hw.Id) + } else { t.AppendRow(table.Row{hw.Id, iface.Dhcp.Mac, iface.Dhcp.Ip.Address, iface.Dhcp.Hostname}) } } - if err != nil && err != io.EOF { - log.Fatal(err) - } else { - t.Render() - } - }, + } + if err != nil && err != io.EOF { + log.Fatal(err) + } +} + +func addListFlags() { + flags := listCmd.Flags() + flags.BoolVarP(&quiet, "quiet", "q", false, "only display hardware IDs") } func init() { + addListFlags() SubCommands = append(SubCommands, listCmd) } From 0c8a6fc5b322d29a543ca9fd0857602a3e1d60f6 Mon Sep 17 00:00:00 2001 From: Gaurav Gahlot Date: Mon, 28 Dec 2020 13:47:37 +0530 Subject: [PATCH 3/4] tink workflow list -q Signed-off-by: Gaurav Gahlot --- cmd/tink-cli/cmd/workflow/list.go | 35 ++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/cmd/tink-cli/cmd/workflow/list.go b/cmd/tink-cli/cmd/workflow/list.go index f76a6b498..5239a0c24 100644 --- a/cmd/tink-cli/cmd/workflow/list.go +++ b/cmd/tink-cli/cmd/workflow/list.go @@ -15,6 +15,9 @@ import ( ) var ( + quiet bool + t table.Writer + hCreatedAt = "Created At" hUpdatedAt = "Updated At" ) @@ -31,16 +34,19 @@ var listCmd = &cobra.Command{ return nil }, Run: func(c *cobra.Command, args []string) { - t := table.NewWriter() + if quiet { + listWorkflows() + return + } + t = table.NewWriter() t.SetOutputMirror(os.Stdout) t.AppendHeader(table.Row{hID, hTemplate, hDevice, hCreatedAt, hUpdatedAt}) - listWorkflows(c, t) + listWorkflows() t.Render() - }, } -func listWorkflows(c *cobra.Command, t table.Writer) { +func listWorkflows() { list, err := client.WorkflowClient.ListWorkflows(context.Background(), &workflow.Empty{}) if err != nil { log.Fatal(err) @@ -48,19 +54,32 @@ func listWorkflows(c *cobra.Command, t table.Writer) { var wf *workflow.Workflow for wf, err = list.Recv(); err == nil && wf.Id != ""; wf, err = list.Recv() { + printOutput(wf) + } + + if err != nil && err != io.EOF { + log.Fatal(err) + } +} + +func printOutput(wf *workflow.Workflow) { + if quiet { + fmt.Println(wf.Id) + } else { cr := wf.CreatedAt up := wf.UpdatedAt t.AppendRows([]table.Row{ {wf.Id, wf.Template, wf.Hardware, time.Unix(cr.Seconds, 0), time.Unix(up.Seconds, 0)}, }) } +} - if err != nil && err != io.EOF { - log.Fatal(err) - } +func addListFlags() { + flags := listCmd.Flags() + flags.BoolVarP(&quiet, "quiet", "q", false, "only display workflow IDs") } func init() { - listCmd.DisableFlagsInUseLine = true + addListFlags() SubCommands = append(SubCommands, listCmd) } From a1dac321f28f50af5b9966b9a3e5faf32e4cd28d Mon Sep 17 00:00:00 2001 From: Gaurav Gahlot Date: Mon, 28 Dec 2020 13:48:00 +0530 Subject: [PATCH 4/4] small fix in tink template list Signed-off-by: Gaurav Gahlot --- cmd/tink-cli/cmd/template/list.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/tink-cli/cmd/template/list.go b/cmd/tink-cli/cmd/template/list.go index 6951ffb0c..7fc8b1e73 100644 --- a/cmd/tink-cli/cmd/template/list.go +++ b/cmd/tink-cli/cmd/template/list.go @@ -63,7 +63,7 @@ func listTemplates() { var tmp *template.WorkflowTemplate for tmp, err = list.Recv(); err == nil && tmp.Name != ""; tmp, err = list.Recv() { - printOutput(t, tmp) + printOutput(tmp) } if err != nil && err != io.EOF { @@ -71,7 +71,7 @@ func listTemplates() { } } -func printOutput(t table.Writer, tmp *template.WorkflowTemplate) { +func printOutput(tmp *template.WorkflowTemplate) { if quiet { fmt.Println(tmp.Id) } else {