-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.go
158 lines (134 loc) · 3.47 KB
/
main.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"fmt"
"github.com/opencontrol/fedramp-templater/opencontrols"
"github.com/opencontrol/fedramp-templater/ssp"
"github.com/opencontrol/fedramp-templater/templater"
"log"
"os"
"path/filepath"
)
type subCommand uint8
const (
_ subCommand = iota // Default value. This value is used as a placeholder when creating instances of subCommands
diff
fill
)
func (cmd subCommand) isType(otherCmd subCommand) bool {
return cmd == otherCmd
}
type options struct {
openControlsDir string
inputPath string
outputPath string
cmd subCommand
}
func printUsage() {
log.Fatal(`Usage:
fedramp-templater fill <optional_opencontrols_dir> <inputDoc> <outputDoc>
or
fedramp-templater diff <optional_opencontrols_dir> <inputDoc>`)
}
func parseArgs() (opts options) {
if len(os.Args) < 4 || len(os.Args) > 5 {
printUsage()
}
switch os.Args[1] {
case "diff":
opts.cmd = diff
case "fill":
opts.cmd = fill
default:
log.Printf("Unknown command: %s\n", os.Args[1])
printUsage()
}
if opts.cmd.isType(diff) && len(os.Args) == 4 || len(os.Args) == 3 {
// diff command only has three or four args
if len(os.Args) == 4 {
opts.openControlsDir = os.Args[2]
opts.inputPath = os.Args[3]
} else {
dirname := "opencontrols"
if _, err := os.Stat(dirname); os.IsNotExist(err) {
fmt.Println("OpenControls directory not found in current directory. Please specify opencontrols directory")
} else {
opts.openControlsDir = dirname
opts.inputPath = os.Args[2]
}
}
} else if opts.cmd.isType(fill) && len(os.Args) == 5 || len(os.Args) == 4 {
// fill command only has four or five args
if len(os.Args) == 5 {
opts.openControlsDir = os.Args[2]
opts.inputPath = os.Args[3]
opts.outputPath = os.Args[4]
} else {
dirname := "opencontrols"
if _, err := os.Stat(dirname); os.IsNotExist(err) {
fmt.Println("OpenControls directory not found in current directory. Please specify opencontrols directory")
} else {
opts.openControlsDir = dirname
opts.inputPath = os.Args[2]
opts.outputPath = os.Args[3]
}
}
} else {
printUsage()
}
return
}
func loadOpenControls(path string) opencontrols.Data {
path, err := filepath.Abs(path)
if err != nil {
log.Fatalln(err)
}
openControlData, errors := opencontrols.LoadFrom(path)
if len(errors) > 0 {
log.Fatal(errors)
}
return openControlData
}
func diffCmd(openControlData opencontrols.Data, doc *ssp.Document) {
reporters, err := templater.DiffSSP(doc, openControlData)
if err != nil {
log.Fatalln(err)
}
if len(reporters) == 0 {
log.Println("No diff detected")
return
}
for _, reporter := range reporters {
reporter.WriteTextTo(os.Stdout)
}
log.Fatalf("%d diffs detected\n", len(reporters))
}
func fillCmd(openControlData opencontrols.Data, doc *ssp.Document, opts options) {
err := templater.TemplatizeSSP(doc, openControlData)
if err != nil {
log.Fatalln(err)
}
outputDir := filepath.Dir(opts.outputPath)
err = os.MkdirAll(outputDir, 0755)
if err != nil {
log.Fatalln(err)
}
err = doc.CopyTo(opts.outputPath)
if err != nil {
log.Fatalln(err)
}
}
func main() {
opts := parseArgs()
openControlData := loadOpenControls(opts.openControlsDir)
doc, err := ssp.Load(opts.inputPath)
if err != nil {
log.Fatalln(err)
}
defer doc.Close()
// right now we don't want to do a fill and diff together.
if opts.cmd.isType(diff) {
diffCmd(openControlData, doc)
} else if opts.cmd.isType(fill) {
fillCmd(openControlData, doc, opts)
}
}