-
Notifications
You must be signed in to change notification settings - Fork 54
fix(#1768): remove stale .adoc files before doc generation #1769
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -362,12 +362,40 @@ func inspect(rego []string) ([]ast.FlatAnnotationsRefSet, error) { | |
| return annotations, nil | ||
| } | ||
|
|
||
| // removeAdocFiles removes all .adoc files from the given directory. | ||
| // If the directory does not exist, it returns nil. | ||
| func removeAdocFiles(dir string) error { | ||
| entries, err := os.ReadDir(dir) | ||
| if err != nil { | ||
| if os.IsNotExist(err) { | ||
| return nil | ||
| } | ||
| return fmt.Errorf("reading directory %q: %w", dir, err) | ||
| } | ||
| for _, entry := range entries { | ||
| if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".adoc") { | ||
| if err := os.Remove(filepath.Join(dir, entry.Name())); err != nil { | ||
| return fmt.Errorf("removing file %q: %w", entry.Name(), err) | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [low] nil-dereference Pre-existing bug (not introduced by this PR): Suggested fix: Wrap the range loop in |
||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func GenerateAsciidoc(module string, rego ...string) error { | ||
| annotations, err := inspect(rego) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Remove all .adoc files from the packages directory before | ||
| // regenerating. Everything in this directory is generated, so | ||
| // removing stale files from deleted packages is safe. | ||
| packagesDir := filepath.Join(module, "pages", "packages") | ||
| if err := removeAdocFiles(packagesDir); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| for _, d := range docs { | ||
| d.SetAnnotations(annotations) | ||
| if err := d.generateNav(module); err != nil { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[low] best-practice
Uses os.IsNotExist(err) instead of the idiomatic errors.Is(err, fs.ErrNotExist). While both work correctly here (os.ReadDir returns an unwrapped *os.PathError), the errors.Is form handles wrapped errors and is the Go 1.13+ convention. The io/fs package is already imported in this file.
Suggested fix: Replace
if os.IsNotExist(err)withif errors.Is(err, fs.ErrNotExist)and add"errors"to the import block.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will fullsend the coder notice this suggestion made by fullsend the reviewer, and make the suggested change?