A bunch of changes

Signed-off-by: Dave Henderson <dhenderson@gmail.com>
This commit is contained in:
Dave Henderson
2018-08-20 20:06:33 -04:00
parent 16a4d2826c
commit a1879b1eb0
16 changed files with 274 additions and 203 deletions

46
cmd/onerng/image.go Normal file
View File

@ -0,0 +1,46 @@
package main
import (
"context"
"fmt"
"os"
"github.com/pkg/errors"
"github.com/hairyhenderson/go-onerng"
"github.com/spf13/cobra"
)
// imageCmd represents the image command
func imageCmd(ctx context.Context) *cobra.Command {
var imgOut string
cmd := &cobra.Command{
Use: "image",
Short: "Dump the OneRNG's firmware image",
RunE: func(cmd *cobra.Command, args []string) error {
o := onerng.OneRNG{Path: opts.Device}
err := o.Init(ctx)
if err != nil {
return errors.Wrapf(err, "init failed before image extraction")
}
image, err := o.Image(ctx)
if err != nil {
return err
}
var out *os.File
if imgOut == "-" {
out = os.Stdout
} else {
out, err = os.OpenFile(imgOut, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
return err
}
}
n, err := out.Write(image)
fmt.Fprintf(os.Stderr, "Wrote %db to %s\n", n, imgOut)
return err
},
}
cmd.Flags().StringVarP(&imgOut, "out", "o", "onerng.img", "output file for image (use - for stdout)")
return cmd
}