/
writer.go
84 lines (73 loc) · 1.8 KB
/
writer.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package file
import (
"encoding/json"
"fmt"
"github.com/ninjaneers-team/uropa/state"
"github.com/pkg/errors"
yaml "gopkg.in/yaml.v3"
"io/ioutil"
"path/filepath"
"sort"
"strings"
)
// WriteConfig holds settings to use to write the state file.
type WriteConfig struct {
Filename string
FileFormat Format
}
func compareID(obj1, obj2 id) bool {
return strings.Compare(obj1.id(), obj2.id()) < 0
}
// OpaStateToFile writes a state object to file with filename.
// It will omit timestamps and IDs while writing.
func OpaStateToFile(OpaState *state.OpaState, config WriteConfig) error {
var file Content
// hardcoded as only one version exists currently
file.FormatVersion = "1.1"
policies, err := OpaState.Policies.GetAll()
if err != nil {
return err
}
for _, s := range policies {
s := FPolicy{Policy: s.Policy}
file.Policies = append(file.Policies, s)
}
sort.SliceStable(file.Policies, func(i, j int) bool {
return compareID(file.Policies[i], file.Policies[j])
})
return writeFile(file, config.Filename, config.FileFormat)
}
func writeFile(content Content, filename string, format Format) error {
var c []byte
var err error
switch format {
case YAML:
c, err = yaml.Marshal(content)
if err != nil {
return err
}
case JSON:
c, err = json.MarshalIndent(content, "", " ")
if err != nil {
return err
}
default:
return errors.New("unknown file format: " + string(format))
}
if filename == "-" {
_, err = fmt.Print(string(c))
} else {
filename = addExtToFilename(filename, string(format))
err = ioutil.WriteFile(filename, c, 0600)
}
if err != nil {
return errors.Wrap(err, "writing file")
}
return nil
}
func addExtToFilename(filename, format string) string {
if filepath.Ext(filename) == "" {
filename = filename + "." + strings.ToLower(format)
}
return filename
}