-
Notifications
You must be signed in to change notification settings - Fork 23
/
output.go
92 lines (83 loc) · 2.48 KB
/
output.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
// Copyright The Enterprise Contract Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
package opa
import (
"fmt"
"io"
"strings"
"text/template"
hd "github.com/MakeNowJust/heredoc"
"github.com/open-policy-agent/opa/ast"
"golang.org/x/exp/slices"
"github.com/enterprise-contract/ec-cli/internal/opa/rule"
)
var templates = map[string]string{
"text": hd.Doc(`
{{ .Package }}.{{ .ShortName }} ({{ .Kind }})
{{ with .DocumentationUrl }}{{ . }}
{{ end }}{{ .Title }}
{{ .Description }}{{ if .Collections }}
{{ .Collections }}{{ end }}
--
`),
"names": hd.Doc(`
{{ .Package }}.{{ .ShortName }}
`),
"short-names": hd.Doc(`
{{ .Code }}
`),
}
// Render using a template
func renderAnn(out io.Writer, a *ast.AnnotationsRef, tmplName string) error {
t := template.Must(template.New("t").Parse(templates[tmplName]))
return t.Execute(out, rule.RuleInfo(a))
}
// Todo:
// - Group by package or collection
// - Filtering by package or collection maybe
// - More useful formats
func OutputText(out io.Writer, allData map[string][]*ast.AnnotationsRef, template string) error {
sources := make([]string, 0, len(allData))
for src := range allData {
i, _ := slices.BinarySearch(sources, src)
sources = slices.Insert(sources, i, src)
}
for _, src := range sources {
annRefs := allData[src]
if template == "text" {
// This part could be templated too I guess but let's keep it simple for now
fmt.Fprintf(out, "# Source: %s\n\n", src)
}
for _, ann := range annRefs {
pathStrings := strings.Split(ann.Path.String(), ".")
if ann.Annotations != nil {
if string(ann.Annotations.Scope) == "rule" {
err := renderAnn(out, ann, template)
if err != nil {
return err
}
}
// Skip package annotations for now
} else {
// Handle edge case where there's no annotations at all
fmt.Fprintf(out, "%s\n%s\n--\n",
strings.Join(pathStrings[1:], "."),
"(No annotations found)")
}
}
}
return nil
}