Skip to content

Commit

Permalink
Fixed: consistent user experience across tink-cli list command -q (ti…
Browse files Browse the repository at this point in the history
…nkerbell#404)

## Description

The PR introduces a new flag --quiet or -q to only display IDs while listing hardware and workflow.

```
/ # tink hardware list
+--------------------------------------+-------------------+-------------+-----------+
| ID                                   | MAC ADDRESS       | IP ADDRESS  | HOSTNAME  |
+--------------------------------------+-------------------+-------------+-----------+
| 0eba0bf8-3772-4b4a-ab9f-6ebe93b90a95 | 08:00:27:00:00:01 | 192.168.1.5 | server001 |
| 1eba0bf8-3772-4b4a-ab9f-6ebe93b90a95 | 08:00:27:00:00:02 | 192.168.1.5 | server001 |
+--------------------------------------+-------------------+-------------+-----------+

/ # tink hardware list -q
0eba0bf8-3772-4b4a-ab9f-6ebe93b90a95
1eba0bf8-3772-4b4a-ab9f-6ebe93b90a95

/ # tink workflow list
+--------------------------------------+--------------------------------------+-----------------------------------+-------------------------------+-------------------------------+
| WORKFLOW ID                          | TEMPLATE ID                          | HARDWARE DEVICE                   | CREATED AT                    | UPDATED AT                    |
+--------------------------------------+--------------------------------------+-----------------------------------+-------------------------------+-------------------------------+
| c465b4b2-48e6-11eb-a673-0242ac120004 | aabad754-48e6-11eb-a673-0242ac120004 | {"device_1": "08:00:27:00:00:02"} | 2020-12-28 08:29:13 +0000 UTC | 2020-12-28 08:29:13 +0000 UTC |
| c7812ec3-48e6-11eb-a673-0242ac120004 | aabad754-48e6-11eb-a673-0242ac120004 | {"device_1": "08:00:27:00:00:02"} | 2020-12-28 08:29:19 +0000 UTC | 2020-12-28 08:29:19 +0000 UTC |
+--------------------------------------+--------------------------------------+-----------------------------------+-------------------------------+-------------------------------+

/ # tink workflow list -q
c465b4b2-48e6-11eb-a673-0242ac120004
c7812ec3-48e6-11eb-a673-0242ac120004
```

## Why is this needed

Fixes: tinkerbell#403 

## How Has This Been Tested?

Tested manually over the vagrant setup.

## Checklist:

I have:

- [ ] updated the documentation and/or roadmap (if required)
- [ ] added unit or e2e tests
- [x] provided instructions on how to upgrade
  • Loading branch information
mergify[bot] authored Dec 28, 2020
2 parents 65a446c + a1dac32 commit d7856c9
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 56 deletions.
46 changes: 0 additions & 46 deletions cmd/tink-cli/cmd/hardware/all.go

This file was deleted.

67 changes: 67 additions & 0 deletions cmd/tink-cli/cmd/hardware/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package hardware

import (
"context"
"fmt"
"io"
"log"
"os"

"github.com/jedib0t/go-pretty/table"
"github.com/spf13/cobra"
"github.com/tinkerbell/tink/client"
"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) {
if quiet {
listHardware()
return
}
t = table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"ID", "MAC Address", "IP address", "Hostname"})
listHardware()
t.Render()
},
}

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() {
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)
}
}

func addListFlags() {
flags := listCmd.Flags()
flags.BoolVarP(&quiet, "quiet", "q", false, "only display hardware IDs")
}

func init() {
addListFlags()
SubCommands = append(SubCommands, listCmd)
}
4 changes: 2 additions & 2 deletions cmd/tink-cli/cmd/template/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ 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 {
log.Fatal(err)
}
}

func printOutput(t table.Writer, tmp *template.WorkflowTemplate) {
func printOutput(tmp *template.WorkflowTemplate) {
if quiet {
fmt.Println(tmp.Id)
} else {
Expand Down
35 changes: 27 additions & 8 deletions cmd/tink-cli/cmd/workflow/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import (
)

var (
quiet bool
t table.Writer

hCreatedAt = "Created At"
hUpdatedAt = "Updated At"
)
Expand All @@ -31,36 +34,52 @@ 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)
}

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)
}

0 comments on commit d7856c9

Please sign in to comment.