From f788c92705ec9d4a796e01701dcdcf4c16b06188 Mon Sep 17 00:00:00 2001 From: Gaurav Gahlot Date: Mon, 4 May 2020 17:28:14 +0530 Subject: [PATCH 1/3] reading hardware data from stdin Signed-off-by: Gaurav Gahlot --- cli/tink/cmd/hardware/push.go | 39 ++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/cli/tink/cmd/hardware/push.go b/cli/tink/cmd/hardware/push.go index 07f4b51be..45da90e90 100644 --- a/cli/tink/cmd/hardware/push.go +++ b/cli/tink/cmd/hardware/push.go @@ -3,44 +3,49 @@ package hardware import ( + "bufio" "context" "encoding/json" - "fmt" + "io" "log" + "os" "strings" + "github.com/spf13/cobra" "github.com/tinkerbell/tink/client" "github.com/tinkerbell/tink/protos/hardware" - "github.com/spf13/cobra" ) // 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 { + Example: "cat data.json | tink hardware push", + Run: func(cmd *cobra.Command, args []string) { + data := readHardwareData(os.Stdin) 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) - } + 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) } - 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) - } + if _, err := client.HardwareClient.Push(context.Background(), &hardware.PushRequest{Data: data}); err != nil { + log.Fatal(err) } + log.Println("Hardware data pushed successfully") }, } +func readHardwareData(r io.Reader) string { + scanner := bufio.NewScanner(bufio.NewReader(r)) + for scanner.Scan() { + return scanner.Text() + } + return "" +} + func init() { SubCommands = append(SubCommands, pushCmd) } From c03d4b48daae1db16fd5b4279079cb0fec981c57 Mon Sep 17 00:00:00 2001 From: Gaurav Gahlot Date: Wed, 6 May 2020 12:51:31 +0530 Subject: [PATCH 2/3] using flag to receive data Signed-off-by: Gaurav Gahlot --- cli/tink/cmd/hardware/push.go | 57 ++++++++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 7 deletions(-) diff --git a/cli/tink/cmd/hardware/push.go b/cli/tink/cmd/hardware/push.go index 45da90e90..7c0551a1c 100644 --- a/cli/tink/cmd/hardware/push.go +++ b/cli/tink/cmd/hardware/push.go @@ -6,7 +6,8 @@ import ( "bufio" "context" "encoding/json" - "io" + "fmt" + "io/ioutil" "log" "os" "strings" @@ -16,13 +17,33 @@ import ( "github.com/tinkerbell/tink/protos/hardware" ) +var ( + filePath string + fPath = "file-path" +) + // pushCmd represents the push command var pushCmd = &cobra.Command{ - Use: "push", - Short: "Push new hardware to tinkerbell", - Example: "cat data.json | tink hardware push", + Use: "push", + Short: "Push new hardware to tinkerbell", + Example: `cat /tmp/data.json | tink hardware push +tink hardware push -p /tmp/data.json`, + PreRunE: func(c *cobra.Command, args []string) error { + if !isInputFromPipe() { + path, _ := c.Flags().GetString(fPath) + if path == "" { + return fmt.Errorf("%v either pipe the data or provide the required flag", c.UseLine()) + } + } + return nil + }, Run: func(cmd *cobra.Command, args []string) { - data := readHardwareData(os.Stdin) + var data string + if isInputFromPipe() { + data = readDataFromStdin() + } else { + data = readDataFromFile() + } s := struct { ID string }{} @@ -38,14 +59,36 @@ var pushCmd = &cobra.Command{ }, } -func readHardwareData(r io.Reader) string { - scanner := bufio.NewScanner(bufio.NewReader(r)) +func isInputFromPipe() bool { + fileInfo, _ := os.Stdin.Stat() + return fileInfo.Mode()&os.ModeCharDevice == 0 +} + +func readDataFromStdin() string { + scanner := bufio.NewScanner(bufio.NewReader(os.Stdin)) for scanner.Scan() { return scanner.Text() } return "" } +func readDataFromFile() string { + f, err := os.Open(filePath) + 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(&filePath, "file-path", "p", "", "path to the hardware data file") + SubCommands = append(SubCommands, pushCmd) } From 96c6a974dadb67a9f6ce689b43c3a11a33c7b59d Mon Sep 17 00:00:00 2001 From: Gaurav Gahlot Date: Thu, 7 May 2020 01:44:09 +0530 Subject: [PATCH 3/3] using ioutil to read data and changed flag for the file Signed-off-by: Gaurav Gahlot --- cli/tink/cmd/hardware/push.go | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/cli/tink/cmd/hardware/push.go b/cli/tink/cmd/hardware/push.go index 7c0551a1c..da803a788 100644 --- a/cli/tink/cmd/hardware/push.go +++ b/cli/tink/cmd/hardware/push.go @@ -3,7 +3,6 @@ package hardware import ( - "bufio" "context" "encoding/json" "fmt" @@ -18,8 +17,8 @@ import ( ) var ( - filePath string - fPath = "file-path" + file string + sFile = "file" ) // pushCmd represents the push command @@ -27,12 +26,12 @@ var pushCmd = &cobra.Command{ Use: "push", Short: "Push new hardware to tinkerbell", Example: `cat /tmp/data.json | tink hardware push -tink hardware push -p /tmp/data.json`, +tink hardware push --file /tmp/data.json`, PreRunE: func(c *cobra.Command, args []string) error { if !isInputFromPipe() { - path, _ := c.Flags().GetString(fPath) + path, _ := c.Flags().GetString(sFile) if path == "" { - return fmt.Errorf("%v either pipe the data or provide the required flag", c.UseLine()) + return fmt.Errorf("either pipe the data or provide the required '--file' flag") } } return nil @@ -65,15 +64,15 @@ func isInputFromPipe() bool { } func readDataFromStdin() string { - scanner := bufio.NewScanner(bufio.NewReader(os.Stdin)) - for scanner.Scan() { - return scanner.Text() + data, err := ioutil.ReadAll(os.Stdin) + if err != nil { + return "" } - return "" + return string(data) } func readDataFromFile() string { - f, err := os.Open(filePath) + f, err := os.Open(file) if err != nil { log.Fatal(err) } @@ -88,7 +87,7 @@ func readDataFromFile() string { func init() { flags := pushCmd.PersistentFlags() - flags.StringVarP(&filePath, "file-path", "p", "", "path to the hardware data file") + flags.StringVarP(&file, "file", "", "", "hardware data file") SubCommands = append(SubCommands, pushCmd) }