-
Notifications
You must be signed in to change notification settings - Fork 326
feature: Documentation command #1009
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
9a51f45
feat: parse the annotations
xNok ca52b2e
feat: generate section that can be used to generate documentations
xNok d2eab46
feat: generate the documentation file
xNok b817229
feat: add the command doc to conftest
xNok 0cf20bf
chore: improve test case
xNok 6a07b06
build(deps): bump github.com/open-policy-agent/opa from 0.68.0 to 0.6…
dependabot[bot] 9736e82
chore: Fixed changes requested by @boranx
xNok dc4e660
test: refactor to use Document instead of []Section
xNok b91e99f
chore: improve doc string in code
xNok 1c294f2
chore: address changes requested by @
xNok dbb40c4
chore: remove error handling on file close
xNok 535f89e
fix: revert bad renaming
xNok 0396ea4
fix: go mod tidy
xNok 998c2eb
fix: update template in acceptance test
xNok File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: generate the documentation file
Signed-off-by: Alexandre Couedelo <[email protected]>
- Loading branch information
commit d2eab460116eb8fba62d78d19fc24fdaff224fac
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # Policies | ||
| {{ range . }} | ||
| {{ .H }} {{ .Path }} - {{ .Annotations.Title }} | ||
|
|
||
| {{ .Annotations.Description }} | ||
| {{- if .Annotations.RelatedResources }} | ||
|
|
||
| Related Resources: | ||
| {{ range .Annotations.RelatedResources }} | ||
| {{- if .Description -}} | ||
| * [{{.Description}}]({{ .Ref }}) | ||
| {{- else }} | ||
| * {{ .Ref }} | ||
| {{ end -}} | ||
| {{- end -}} | ||
| {{ end }} | ||
| {{ end }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package document | ||
|
|
||
| import ( | ||
| "embed" | ||
| "io" | ||
| "io/fs" | ||
| "text/template" | ||
| ) | ||
|
|
||
| //go:embed resources/* | ||
| var resources embed.FS | ||
|
|
||
| func generateDocument(out io.Writer, s []Section) error { | ||
|
|
||
| err := renderTemplate(resources, "resources/document.md", s, out) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func renderTemplate(fs fs.FS, tpl string, args interface{}, out io.Writer) error { | ||
| // read the template | ||
| t, err := template.ParseFS(fs, tpl) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // we render the template | ||
| err = t.Execute(out, args) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package document | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "os" | ||
| "testing" | ||
| ) | ||
|
|
||
| func Test_generateDocument(t *testing.T) { | ||
xNok marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| tests := []struct { | ||
| name string | ||
| testdata string | ||
| wantOut string | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| name: "Nested packages", | ||
| testdata: "./testdata/foo", | ||
| wantOut: "./testdata/doc/foo.md", | ||
| wantErr: false, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run( | ||
xNok marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| tt.name, func(t *testing.T) { | ||
| as, err := ParseRegoWithAnnotations(tt.testdata) | ||
| assert.NoError(t, err) | ||
|
|
||
| s := GetDocument(as) | ||
|
|
||
| gotOut := &bytes.Buffer{} | ||
| err = generateDocument(gotOut, s) | ||
| if (err != nil) != tt.wantErr { | ||
| t.Errorf("GenVariableDoc() error = %v, wantErr %v", err, tt.wantErr) | ||
| return | ||
| } | ||
|
|
||
| wantOut, err := os.ReadFile(tt.wantOut) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, string(wantOut), gotOut.String()) | ||
|
|
||
| // un comment this to generate the golden file when changing the template | ||
| //os.WriteFile(tt.wantOut+".golden", gotOut.Bytes(), 0644) | ||
| }, | ||
| ) | ||
| } | ||
| } | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,23 @@ | ||
| # Policies - bar | ||
| # Policies | ||
|
|
||
| ## Rules | ||
| ## foo - My package foo | ||
|
|
||
| ### My Rule P | ||
| the package with rule A and subpackage bar | ||
|
|
||
| ### foo.a - My Rule A | ||
|
|
||
| the rule A = 3 | ||
|
|
||
| Related Resources: | ||
|
|
||
| * https://example.com | ||
| * [Yet another link](https://example.com/more) | ||
|
|
||
| ### foo.bar - My package bar | ||
|
|
||
| The package with rule P | ||
|
|
||
| #### foo.bar.p - My Rule P | ||
|
|
||
| the Rule P = 7 | ||
|
|
||
| ```opa | ||
| p := 7 | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,9 @@ | ||
| # METADATA | ||
| # title: My package bar | ||
| # description: A couple of useful rules | ||
| # description: The package with rule P | ||
| package foo.bar | ||
|
|
||
| # METADATA | ||
| # title: My Rule P | ||
| # description: the Rule P = 7 | ||
| p := 7 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,16 @@ | ||
| # METADATA | ||
| # title: My package foo | ||
| # description: the package with rule A and subpackage bar | ||
| # scope: subpackages | ||
| # organizations: | ||
| # - Acme Corp. | ||
| package foo | ||
|
|
||
| # METADATA | ||
| # title: My Rule A | ||
| # description: the rule A = 3 | ||
| # related_resources: | ||
| # - ref: https://example.com | ||
| # - ref: https://example.com/more | ||
| # description: Yet another link | ||
| a := 3 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.