Skip to content

Commit 63136c8

Browse files
Refactor docgen: improve error handling and file operations (#6103)
* Refactor docgen: improve error handling and file operations Moved the argument check to the start of main() so we don’t do any unnecessary work if the arguments aren’t provided. Also added proper error handling for encoder.Encode(jsonschemaData), since it was failing silently before. Switched os.WriteFile to os.Create for better file handling and error reporting. To clean things up, I added a writeToFile() function to remove duplicate code. For replacing schema paths, I used ReplaceAllString() instead of looping through matches—it’s cleaner and a bit more efficient. Also renamed r to t in the Namer function to make things clearer. * Fix a Lint error - Remove importing string
1 parent 04a6c82 commit 63136c8

File tree

1 file changed

+30
-25
lines changed

1 file changed

+30
-25
lines changed

cmd/docgen/docgen.go

+30-25
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,58 @@ import (
66
"os"
77
"reflect"
88
"regexp"
9-
"strings"
109

1110
"github.com/invopop/jsonschema"
12-
1311
"github.com/projectdiscovery/nuclei/v3/pkg/templates"
1412
"github.com/projectdiscovery/nuclei/v3/pkg/utils/json"
1513
)
1614

1715
var pathRegex = regexp.MustCompile(`github\.com/projectdiscovery/nuclei/v3/(?:internal|pkg)/(?:.*/)?([A-Za-z.]+)`)
1816

19-
func main() {
20-
// Generate yaml syntax documentation
21-
data, err := templates.GetTemplateDoc().Encode()
17+
func writeToFile(filename string, data []byte) {
18+
file, err := os.Create(filename)
2219
if err != nil {
23-
log.Fatalf("Could not encode docs: %s\n", err)
20+
log.Fatalf("Could not create file %s: %s\n", filename, err)
2421
}
22+
defer file.Close()
2523

24+
_, err = file.Write(data)
25+
if err != nil {
26+
log.Fatalf("Could not write to file %s: %s\n", filename, err)
27+
}
28+
}
29+
30+
func main() {
2631
if len(os.Args) < 3 {
2732
log.Fatalf("syntax: %s md-docs-file jsonschema-file\n", os.Args[0])
2833
}
2934

30-
err = os.WriteFile(os.Args[1], data, 0644)
35+
// Generate YAML documentation
36+
data, err := templates.GetTemplateDoc().Encode()
3137
if err != nil {
32-
log.Fatalf("Could not write docs: %s\n", err)
38+
log.Fatalf("Could not encode docs: %s\n", err)
3339
}
34-
35-
// Generate jsonschema
36-
r := &jsonschema.Reflector{}
37-
r.Namer = func(r reflect.Type) string {
38-
if r.Kind() == reflect.Slice {
39-
return ""
40-
}
41-
return r.String()
40+
writeToFile(os.Args[1], data)
41+
42+
// Generate JSON Schema
43+
r := &jsonschema.Reflector{
44+
Namer: func(t reflect.Type) string {
45+
if t.Kind() == reflect.Slice {
46+
return ""
47+
}
48+
return t.String()
49+
},
4250
}
51+
4352
jsonschemaData := r.Reflect(&templates.Template{})
4453

4554
var buf bytes.Buffer
4655
encoder := json.NewEncoder(&buf)
4756
encoder.SetIndent("", " ")
48-
_ = encoder.Encode(jsonschemaData)
49-
50-
schema := buf.String()
51-
for _, match := range pathRegex.FindAllStringSubmatch(schema, -1) {
52-
schema = strings.ReplaceAll(schema, match[0], match[1])
53-
}
54-
err = os.WriteFile(os.Args[2], []byte(schema), 0644)
55-
if err != nil {
56-
log.Fatalf("Could not write jsonschema: %s\n", err)
57+
if err := encoder.Encode(jsonschemaData); err != nil {
58+
log.Fatalf("Could not encode JSON schema: %s\n", err)
5759
}
60+
61+
schema := pathRegex.ReplaceAllString(buf.String(), "$1")
62+
writeToFile(os.Args[2], []byte(schema))
5863
}

0 commit comments

Comments
 (0)