Skip to content

Commit

Permalink
Merge pull request #92 from gauravgahlot/read-hw-data
Browse files Browse the repository at this point in the history
Reading hardware data from stdin
  • Loading branch information
nathangoulding authored May 6, 2020
2 parents 87c2d85 + 96c6a97 commit 01f2e19
Showing 1 changed file with 64 additions and 17 deletions.
81 changes: 64 additions & 17 deletions cli/tink/cmd/hardware/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,88 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"strings"

"github.com/spf13/cobra"
"github.com/tinkerbell/tink/client"
"github.com/tinkerbell/tink/protos/hardware"
"github.com/spf13/cobra"
)

var (
file string
sFile = "file"
)

// pushCmd represents the push command
var pushCmd = &cobra.Command{
Use: "push",
Short: "Push new hardware to tinkerbell",
Example: `tinkerbell hardware push '{"id":"2a1519e5-781c-4251-a979-3a6bedb8ba59", ...}' '{"id:"315169a4-a863-43ef-8817-2b6a57bd1eef", ...}'`,
Args: func(_ *cobra.Command, args []string) error {
s := struct {
ID string
}{}
for _, arg := range args {
if json.NewDecoder(strings.NewReader(arg)).Decode(&s) != nil {
return fmt.Errorf("invalid json: %s", arg)
} else if s.ID == "" {
return fmt.Errorf("invalid json, ID is required: %s", arg)
Use: "push",
Short: "Push new hardware to tinkerbell",
Example: `cat /tmp/data.json | tink hardware push
tink hardware push --file /tmp/data.json`,
PreRunE: func(c *cobra.Command, args []string) error {
if !isInputFromPipe() {
path, _ := c.Flags().GetString(sFile)
if path == "" {
return fmt.Errorf("either pipe the data or provide the required '--file' flag")
}
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
for _, j := range args {
if _, err := client.HardwareClient.Push(context.Background(), &hardware.PushRequest{Data: j}); err != nil {
log.Fatal(err)
}
var data string
if isInputFromPipe() {
data = readDataFromStdin()
} else {
data = readDataFromFile()
}
s := struct {
ID string
}{}
if json.NewDecoder(strings.NewReader(data)).Decode(&s) != nil {
log.Fatalf("invalid json: %s", data)
} else if s.ID == "" {
log.Fatalf("invalid json, ID is required: %s", data)
}
if _, err := client.HardwareClient.Push(context.Background(), &hardware.PushRequest{Data: data}); err != nil {
log.Fatal(err)
}
log.Println("Hardware data pushed successfully")
},
}

func isInputFromPipe() bool {
fileInfo, _ := os.Stdin.Stat()
return fileInfo.Mode()&os.ModeCharDevice == 0
}

func readDataFromStdin() string {
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return ""
}
return string(data)
}

func readDataFromFile() string {
f, err := os.Open(file)
if err != nil {
log.Fatal(err)
}
defer f.Close()

data, err := ioutil.ReadAll(f)
if err != nil {
log.Fatal(err)
}
return string(data)
}

func init() {
flags := pushCmd.PersistentFlags()
flags.StringVarP(&file, "file", "", "", "hardware data file")

SubCommands = append(SubCommands, pushCmd)
}

0 comments on commit 01f2e19

Please sign in to comment.