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

Fix issue #150: Modified the behaviour of few "tink hardware cli" #207

Merged
merged 2 commits into from
Jul 10, 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
33 changes: 19 additions & 14 deletions cmd/tink-cli/cmd/hardware/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,46 @@ package hardware

import (
"context"
"encoding/json"
"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"
)

// allCmd represents the all command
var allCmd = &cobra.Command{
Use: "all",
Short: "get all known hardware for facility",
// listCmd represents the list command
var listCmd = &cobra.Command{
Use: "list",
Short: "list all known hardware",
Run: func(cmd *cobra.Command, args []string) {
alls, err := client.HardwareClient.All(context.Background(), &hardware.Empty{})

t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"ID", "MAC Address", "IP address", "Hostname"})

list, err := client.HardwareClient.All(context.Background(), &hardware.Empty{})
if err != nil {
log.Fatal(err)
}

var hw *hardware.Hardware
err = nil
for hw, err = alls.Recv(); err == nil && hw != nil; hw, err = alls.Recv() {
b, err := json.Marshal(hw)
if err != nil {
log.Println(err)
for hw, err = list.Recv(); err == nil && hw != nil; hw, err = list.Recv() {
for _, iface := range hw.GetNetwork().GetInterfaces() {
t.AppendRow(table.Row{hw.Id, iface.Dhcp.Mac, iface.Dhcp.Ip.Address, iface.Dhcp.Hostname})
}
fmt.Println(string(b))
}
if err != nil && err != io.EOF {
log.Println(err)
log.Fatal(err)
} else {
t.Render()
}
},
}

func init() {
SubCommands = append(SubCommands, allCmd)
SubCommands = append(SubCommands, listCmd)
}
29 changes: 29 additions & 0 deletions cmd/tink-cli/cmd/hardware/commands.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package hardware

import (
"encoding/json"
"fmt"
"log"
"os"

"github.com/jedib0t/go-pretty/table"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
"github.com/spf13/cobra"
"github.com/tinkerbell/tink/protos/hardware"
)

// SubCommands holds the sub commands for template command
Expand All @@ -23,3 +28,27 @@ func verifyUUIDs(args []string) error {
}
return nil
}

func printOutput(data bool, hw *hardware.Hardware, input string) {
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"Field Name", "Value"})
if !data {
for _, iface := range hw.GetNetwork().GetInterfaces() {
if iface.Dhcp.Ip.Address == input || iface.Dhcp.Mac == input {
t.AppendRow(table.Row{"ID", hw.Id})
t.AppendRow(table.Row{"MAC Address", iface.Dhcp.Mac})
t.AppendRow(table.Row{"IP Address", iface.Dhcp.Ip.Address})
t.AppendRow(table.Row{"Hostname", iface.Dhcp.Hostname})
}
}
t.Render()
} else {
hwData, err := json.Marshal(hw)
if err != nil {
log.Fatal("Failed to marshal hardware data", err)
} else {
log.Println(string(hwData))
}
}
}
16 changes: 11 additions & 5 deletions cmd/tink-cli/cmd/hardware/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package hardware

import (
"context"
"encoding/json"
"fmt"
"log"
"net"
Expand All @@ -14,6 +13,8 @@ import (
"github.com/tinkerbell/tink/protos/hardware"
)

var data bool

// ipCmd represents the ip command
var ipCmd = &cobra.Command{
Use: "ip",
Expand All @@ -33,15 +34,20 @@ var ipCmd = &cobra.Command{
if err != nil {
log.Fatal(err)
}
b, err := json.Marshal(hw)
if err != nil {
log.Fatal(err)
if hw.GetId() == "" {
log.Fatal("IP address not found in the database ", ip)
parauliya marked this conversation as resolved.
Show resolved Hide resolved
}
fmt.Println(string(b))
printOutput(data, hw, ip)
}
},
}

func addIPFlags() {
flags := ipCmd.Flags()
flags.BoolVarP(&data, "details", "d", false, "provide the complete hardware details in json format")
}

func init() {
addIPFlags()
SubCommands = append(SubCommands, ipCmd)
}
14 changes: 9 additions & 5 deletions cmd/tink-cli/cmd/hardware/mac.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package hardware

import (
"context"
"encoding/json"
"fmt"
"log"
"net"
Expand Down Expand Up @@ -33,15 +32,20 @@ var macCmd = &cobra.Command{
if err != nil {
log.Fatal(err)
}
b, err := json.Marshal(hw)
if err != nil {
log.Fatal(err)
if hw.GetId() == "" {
log.Fatal("MAC address not found in the database ", mac)
}
fmt.Println(string(b))
printOutput(data, hw, mac)
}
},
}

func addMacFlags() {
flags := macCmd.Flags()
flags.BoolVarP(&data, "details", "d", false, "provide the complete hardware details in json format")
}

func init() {
addMacFlags()
SubCommands = append(SubCommands, macCmd)
}
2 changes: 1 addition & 1 deletion cmd/tink-cli/cmd/hardware/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var (
// pushCmd represents the push command
var pushCmd = &cobra.Command{
Use: "push",
Short: "push new hardware to tinkerbell",
Short: "push new hardware to tink",
Example: `cat /tmp/data.json | tink hardware push
tink hardware push --file /tmp/data.json`,
PreRunE: func(c *cobra.Command, args []string) error {
Expand Down
1 change: 1 addition & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func SetupMetrics(facility string, logger log.Logger) {
{"method": "Ingest", "op": ""},
{"method": "Watch", "op": "get"},
{"method": "Watch", "op": "push"},
{"method": "Delete", "op": "delete"},
}
initCounterLabels(CacheErrors, labels)
initGaugeLabels(CacheInFlight, labels)
Expand Down