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

Reading hardware data from stdin #92

Merged
merged 3 commits into from
May 6, 2020
Merged
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
81 changes: 64 additions & 17 deletions cli/tink/cmd/hardware/push.go
Original file line number Diff line number Diff line change
@@ -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)
}