Skip to content

Commit 1c3960f

Browse files
author
parauliya
committed
Fix issue #150: Modified the behaviour of few "tink hardware cli"
Following are the change in this commit 1. `tink hardware mac <mac address>` will provide only relevent information instead of complete hardware json 2. `tink hardware ip <ip address>` will provide only relevent information instead of complete hardware json 3. There will be a flag in both of the above clis (--details) which can be provided to get the complete hardware data 4. `tink hardware all` cli has been renamed to `tink hardware list` 5. The above cli will only list the relevent information (which is 'hardware-id', 'mac address', 'ip address' and 'hostname for now) instead of providing the complete json of all the hardware present in the database.
1 parent 9973940 commit 1c3960f

File tree

6 files changed

+69
-24
lines changed

6 files changed

+69
-24
lines changed

cmd/tink-cli/cmd/hardware/all.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,46 @@ package hardware
22

33
import (
44
"context"
5-
"encoding/json"
6-
"fmt"
75
"io"
86
"log"
7+
"os"
98

9+
"github.com/jedib0t/go-pretty/table"
1010
"github.com/spf13/cobra"
1111
"github.com/tinkerbell/tink/client"
1212
"github.com/tinkerbell/tink/protos/hardware"
1313
)
1414

15-
// allCmd represents the all command
16-
var allCmd = &cobra.Command{
17-
Use: "all",
18-
Short: "get all known hardware for facility",
15+
// listCmd represents the all command
16+
var listCmd = &cobra.Command{
17+
Use: "list",
18+
Short: "list all known hardware",
1919
Run: func(cmd *cobra.Command, args []string) {
20-
alls, err := client.HardwareClient.All(context.Background(), &hardware.Empty{})
20+
21+
t := table.NewWriter()
22+
t.SetOutputMirror(os.Stdout)
23+
t.AppendHeader(table.Row{"ID", "MAC Address", "IP address", "Hostname"})
24+
25+
list, err := client.HardwareClient.All(context.Background(), &hardware.Empty{})
2126
if err != nil {
2227
log.Fatal(err)
2328
}
2429

2530
var hw *hardware.Hardware
2631
err = nil
27-
for hw, err = alls.Recv(); err == nil && hw != nil; hw, err = alls.Recv() {
28-
b, err := json.Marshal(hw)
29-
if err != nil {
30-
log.Println(err)
32+
for hw, err = list.Recv(); err == nil && hw != nil; hw, err = list.Recv() {
33+
for _, iface := range hw.GetNetwork().GetInterfaces() {
34+
t.AppendRow(table.Row{hw.Id, iface.Dhcp.Mac, iface.Dhcp.Ip.Address, iface.Dhcp.Hostname})
3135
}
32-
fmt.Println(string(b))
3336
}
3437
if err != nil && err != io.EOF {
3538
log.Println(err)
39+
} else {
40+
t.Render()
3641
}
3742
},
3843
}
3944

4045
func init() {
41-
SubCommands = append(SubCommands, allCmd)
46+
SubCommands = append(SubCommands, listCmd)
4247
}

cmd/tink-cli/cmd/hardware/commands.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package hardware
22

33
import (
4+
"encoding/json"
45
"fmt"
6+
"log"
7+
"os"
58

9+
"github.com/jedib0t/go-pretty/table"
610
"github.com/pkg/errors"
711
uuid "github.com/satori/go.uuid"
812
"github.com/spf13/cobra"
13+
"github.com/tinkerbell/tink/protos/hardware"
914
)
1015

1116
// SubCommands holds the sub commands for template command
@@ -23,3 +28,27 @@ func verifyUUIDs(args []string) error {
2328
}
2429
return nil
2530
}
31+
32+
func printOutput(data bool, hw *hardware.Hardware, input string) {
33+
t := table.NewWriter()
34+
t.SetOutputMirror(os.Stdout)
35+
t.AppendHeader(table.Row{"Field Name", "Value"})
36+
if !data {
37+
for _, iface := range hw.GetNetwork().GetInterfaces() {
38+
if iface.Dhcp.Ip.Address == input || iface.Dhcp.Mac == input {
39+
t.AppendRow(table.Row{"ID", hw.Id})
40+
t.AppendRow(table.Row{"MAC Address", iface.Dhcp.Mac})
41+
t.AppendRow(table.Row{"IP Address", iface.Dhcp.Ip.Address})
42+
t.AppendRow(table.Row{"Hostname", iface.Dhcp.Hostname})
43+
}
44+
}
45+
t.Render()
46+
} else {
47+
hwData, err := json.Marshal(hw)
48+
if err != nil {
49+
log.Println("Failed to marshal hardware data", err)
50+
} else {
51+
log.Println(string(hwData))
52+
}
53+
}
54+
}

cmd/tink-cli/cmd/hardware/ip.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package hardware
44

55
import (
66
"context"
7-
"encoding/json"
87
"fmt"
98
"log"
109
"net"
@@ -14,6 +13,8 @@ import (
1413
"github.com/tinkerbell/tink/protos/hardware"
1514
)
1615

16+
var data bool
17+
1718
// ipCmd represents the ip command
1819
var ipCmd = &cobra.Command{
1920
Use: "ip",
@@ -33,15 +34,20 @@ var ipCmd = &cobra.Command{
3334
if err != nil {
3435
log.Fatal(err)
3536
}
36-
b, err := json.Marshal(hw)
37-
if err != nil {
38-
log.Fatal(err)
37+
if hw.GetId() == "" {
38+
log.Fatal("IP address not found in the database ", ip)
3939
}
40-
fmt.Println(string(b))
40+
printOutput(data, hw, ip)
4141
}
4242
},
4343
}
4444

45+
func addIPFlags() {
46+
flags := ipCmd.Flags()
47+
flags.BoolVarP(&data, "details", "d", false, "Provide the complete hardware details in json format")
48+
}
49+
4550
func init() {
51+
addIPFlags()
4652
SubCommands = append(SubCommands, ipCmd)
4753
}

cmd/tink-cli/cmd/hardware/mac.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package hardware
44

55
import (
66
"context"
7-
"encoding/json"
87
"fmt"
98
"log"
109
"net"
@@ -33,15 +32,20 @@ var macCmd = &cobra.Command{
3332
if err != nil {
3433
log.Fatal(err)
3534
}
36-
b, err := json.Marshal(hw)
37-
if err != nil {
38-
log.Fatal(err)
35+
if hw.GetId() == "" {
36+
log.Fatal("MAC address not found in the database ", mac)
3937
}
40-
fmt.Println(string(b))
38+
printOutput(data, hw, mac)
4139
}
4240
},
4341
}
4442

43+
func addMacFlags() {
44+
flags := macCmd.Flags()
45+
flags.BoolVarP(&data, "details", "d", false, "Provide the complete hardware details in json format")
46+
}
47+
4548
func init() {
49+
addMacFlags()
4650
SubCommands = append(SubCommands, macCmd)
4751
}

cmd/tink-cli/cmd/hardware/push.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var (
2424
// pushCmd represents the push command
2525
var pushCmd = &cobra.Command{
2626
Use: "push",
27-
Short: "push new hardware to tinkerbell",
27+
Short: "push new hardware to tink",
2828
Example: `cat /tmp/data.json | tink hardware push
2929
tink hardware push --file /tmp/data.json`,
3030
PreRunE: func(c *cobra.Command, args []string) error {

metrics/metrics.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ func SetupMetrics(facility string, logger log.Logger) {
8181
{"method": "Ingest", "op": ""},
8282
{"method": "Watch", "op": "get"},
8383
{"method": "Watch", "op": "push"},
84+
{"method": "Delete", "op": "delete"},
8485
}
8586
initCounterLabels(CacheErrors, labels)
8687
initGaugeLabels(CacheInFlight, labels)

0 commit comments

Comments
 (0)