Debian Codegraph @

cmd/pem2bin/main.go

raw
1 package main
2
3 import (
4 "encoding/pem"
5 "flag"
6 "fmt"
7 "io/ioutil"
8 "os"
9 )
10
11 var ext = ".bin"
12
13 func stripPEM(path string) error {
14 data, err := ioutil.ReadFile(path)
15 if err != nil {
16 return err
17 }
18
19 p, rest := pem.Decode(data)
20 if len(rest) != 0 {
21 fmt.Fprintf(os.Stderr, "[WARNING] extra data in PEM file\n")
22 fmt.Fprintf(os.Stderr, " (only the first object will be decoded)\n")
23 }
24
25 return ioutil.WriteFile(path+ext, p.Bytes, 0644)
26 }
27
28 func main() {
29 flag.Parse()
30
31 for _, path := range flag.Args() {
32 err := stripPEM(path)
33 if err != nil {
34 fmt.Fprintf(os.Stderr, "processing %s failed: %v\n", path, err)
35 } else {
36 fmt.Println(path, "->", path+ext)
37 }
38 }
39 }