-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.go
47 lines (37 loc) · 1.01 KB
/
encode.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
package cmd
import (
"bytes"
"errors"
"fmt"
"os"
"github.com/cfromknecht/phew"
"github.com/spf13/cobra"
)
var (
ErrNumEncodeArgs = errors.New("encode only acceepts zero or one arguments")
)
// encodeCmd represents the encode command
var encodeCmd = &cobra.Command{
Use: "encode [data]",
Short: "Encode data passed as either a single argument or from stdin.",
Long: `Encode accepts zero or one arguments.
When no arguments are passed, encode reads data from stdin and writes the
encoded output to stdout.
When one argument is passed, encode will write the encoded argument to stdout.
Passing in more than one argument will fail.`,
Run: func(cmd *cobra.Command, args []string) {
data := getInputData(args, ErrNumEncodeArgs)
var b bytes.Buffer
w := phew.NewWriter(&b)
write(w, data)
err := w.Close()
if err != nil {
fatal(fmt.Errorf("unable to close phew writer: %v", err))
}
write(os.Stdout, b.Bytes())
write(os.Stdout, newlineBytes)
},
}
func init() {
rootCmd.AddCommand(encodeCmd)
}