-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
47 lines (39 loc) · 1.14 KB
/
json.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 encoder
import (
"encoding/json"
"fmt"
"os"
"github.com/caiofilipini/certgen/cert"
)
// jsonEncoder is an implementation of Encoder that encodes
// Certificate objects as JSON.
type jsonEncoder struct {
opts Options
}
// Encode takes a Certificate object and encodes it in JSON
// format. If the Encoder has been configured to write to disk,
// then the resulting JSON will be written to a file; otherwise,
// it will be printed to STDOUT.
//
// It returns a non-nil error in case it fails to encode the given
// Certificate into JSON, or if it fails to write to disk.
func (e jsonEncoder) Encode(cert cert.Certificate) (Result, error) {
var artifacts []string
out := os.Stdout
if e.opts.WriteToDisk {
jsonFile, err := openFile(fmt.Sprintf("%s.json", fileNamePrefixFor(cert, e.opts.OutputDir)))
if err != nil {
return errResult, err
}
defer jsonFile.Close()
artifacts = append(artifacts, jsonFile.Name())
out = jsonFile
}
if err := json.NewEncoder(out).Encode(cert); err != nil {
return errResult, fmt.Errorf("failed to generate JSON output: %w", err)
}
return Result{
Success: true,
Artifacts: artifacts,
}, nil
}