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

Fixed: consistent user experience across tink-cli list command -q #404

Merged
merged 4 commits into from
Dec 28, 2020
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
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)
}