/
rpmsg.go
55 lines (48 loc) · 1.4 KB
/
rpmsg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package cmd
import (
"fmt"
"io"
"os"
"github.com/bored-engineer/rms/rpmsg"
"github.com/spf13/cobra"
"github.com/pkg/errors"
)
// rpmsgCmd represents the rpmsg command
var rpmsgCmd = &cobra.Command{
Use: "rpmsg",
Short: "Commmands to interact with restricted-permissions messages (rmpsg)",
}
// rpmsgDecodeCmd represents the decode command on rpmsg
var rpmsgDecodeOutput string
var rpmsgDecodeCmd = &cobra.Command{
Use: "decode [message.rpmsg]",
Args: cobra.ExactArgs(1),
Short: "Decode a rpmsg file into a raw compound file",
RunE: func(cmd *cobra.Command, args []string) error {
input, err := os.Open(args[0])
if err != nil {
return errors.Wrap(err, "failed to open input file")
}
defer input.Close()
r, err := rpmsg.NewReader(input)
if err != nil {
return errors.Wrap(err, "failed to start rpmsg reader")
}
output, err := os.OpenFile(rpmsgDecodeOutput, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
return errors.Wrap(err, "failed to open output file")
}
defer output.Close()
n, err := io.Copy(output, r)
if err != nil {
return errors.Wrap(err, "failed to decode")
}
fmt.Printf("Decoded %d bytes to compound file: %s\n", n, rpmsgDecodeOutput)
return nil
},
}
func init() {
rpmsgDecodeCmd.Flags().StringVarP(&rpmsgDecodeOutput, "output", "o", "rpmsg.compound", "Output file for the decoded file")
rpmsgCmd.AddCommand(rpmsgDecodeCmd)
rootCmd.AddCommand(rpmsgCmd)
}