Skip to content

Commit

Permalink
using ioutil to read data and changed flag for the file
Browse files Browse the repository at this point in the history
Signed-off-by: Gaurav Gahlot <gaurav.gahlot19@gmail.com>
  • Loading branch information
gauravgahlot committed May 6, 2020
1 parent c03d4b4 commit 96c6a97
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions cli/tink/cmd/hardware/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package hardware

import (
"bufio"
"context"
"encoding/json"
"fmt"
Expand All @@ -18,21 +17,21 @@ import (
)

var (
filePath string
fPath = "file-path"
file string
sFile = "file"
)

// pushCmd represents the push command
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
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}

0 comments on commit 96c6a97

Please sign in to comment.