-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathexamples.go
More file actions
82 lines (71 loc) · 1.78 KB
/
Copy pathexamples.go
File metadata and controls
82 lines (71 loc) · 1.78 KB
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
"path"
ecc "github.com/enterprise-contract/enterprise-contract-controller/api/v1alpha1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"
)
func simplePolicy() *ecc.EnterpriseContractPolicySpec {
return &ecc.EnterpriseContractPolicySpec{
Description: "ACME & co policy",
Sources: []ecc.Source{{
Name: "simple",
Policy: []string{
"git::https://github.com/acme/ec-policy.git//policy?ref=prod",
},
Data: []string{
"git::https://github.com/acme/ec-policy.git//data?ref=prod",
},
}},
Configuration: &ecc.EnterpriseContractPolicyConfiguration{
Exclude: []string{
"friday_policy",
"room_temperature",
},
},
}
}
func generateJSONSpecExample() (err error) {
var out bytes.Buffer
encoder := json.NewEncoder(&out)
encoder.SetEscapeHTML(false)
encoder.SetIndent("", " ")
if err = encoder.Encode(simplePolicy()); err == nil {
err = os.WriteFile(path.Join("docs", "modules", "ROOT", "examples", "spec-example.json"), out.Bytes(), 0644)
}
return
}
func generateK8SYAMLExample() (err error) {
policy := ecc.EnterpriseContractPolicy{
TypeMeta: v1.TypeMeta{
Kind: "EnterpriseContractPolicy",
APIVersion: ecc.GroupVersion.Identifier(),
},
ObjectMeta: v1.ObjectMeta{
Name: "ec-policy",
Namespace: "acme",
},
Spec: *simplePolicy(),
}
var out []byte
if out, err = yaml.Marshal(policy); err == nil {
err = os.WriteFile(path.Join("docs", "modules", "ROOT", "examples", "k8s-example.yaml"), out, 0644)
}
return
}
func main() {
generators := []func() error{
generateJSONSpecExample,
generateK8SYAMLExample,
}
for _, g := range generators {
if err := g(); err != nil {
fmt.Fprintf(os.Stderr, "Unable to generate example: %v", err)
os.Exit(1)
}
}
}