2021-08-03 22:33:50 +05:30
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2021-08-23 23:50:45 +05:30
|
|
|
"bytes"
|
2021-08-03 22:33:50 +05:30
|
|
|
"log"
|
|
|
|
|
"os"
|
2024-04-08 03:29:42 +05:30
|
|
|
"reflect"
|
2021-08-23 23:50:45 +05:30
|
|
|
"regexp"
|
2021-08-03 22:33:50 +05:30
|
|
|
|
2024-03-25 15:52:20 -04:00
|
|
|
"github.com/invopop/jsonschema"
|
2023-10-17 17:44:13 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/templates"
|
2025-02-11 04:31:37 +07:00
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/utils/json"
|
2021-08-03 22:33:50 +05:30
|
|
|
)
|
|
|
|
|
|
2023-10-17 17:44:13 +05:30
|
|
|
var pathRegex = regexp.MustCompile(`github\.com/projectdiscovery/nuclei/v3/(?:internal|pkg)/(?:.*/)?([A-Za-z.]+)`)
|
2021-08-23 23:50:45 +05:30
|
|
|
|
2025-03-24 21:42:07 +03:30
|
|
|
func writeToFile(filename string, data []byte) {
|
|
|
|
|
file, err := os.Create(filename)
|
2021-08-03 22:33:50 +05:30
|
|
|
if err != nil {
|
2025-03-24 21:42:07 +03:30
|
|
|
log.Fatalf("Could not create file %s: %s\n", filename, err)
|
2021-08-03 22:33:50 +05:30
|
|
|
}
|
2025-07-01 00:40:44 +07:00
|
|
|
defer func() {
|
|
|
|
|
_ = file.Close()
|
|
|
|
|
}()
|
2023-02-07 09:31:34 +01:00
|
|
|
|
2025-03-24 21:42:07 +03:30
|
|
|
_, err = file.Write(data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalf("Could not write to file %s: %s\n", filename, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main() {
|
2023-02-07 09:31:34 +01:00
|
|
|
if len(os.Args) < 3 {
|
|
|
|
|
log.Fatalf("syntax: %s md-docs-file jsonschema-file\n", os.Args[0])
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-24 21:42:07 +03:30
|
|
|
// Generate YAML documentation
|
|
|
|
|
data, err := templates.GetTemplateDoc().Encode()
|
2021-08-03 22:33:50 +05:30
|
|
|
if err != nil {
|
2025-03-24 21:42:07 +03:30
|
|
|
log.Fatalf("Could not encode docs: %s\n", err)
|
2021-08-03 22:33:50 +05:30
|
|
|
}
|
2025-03-24 21:42:07 +03:30
|
|
|
writeToFile(os.Args[1], data)
|
|
|
|
|
|
|
|
|
|
// Generate JSON Schema
|
|
|
|
|
r := &jsonschema.Reflector{
|
|
|
|
|
Namer: func(t reflect.Type) string {
|
|
|
|
|
if t.Kind() == reflect.Slice {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
return t.String()
|
|
|
|
|
},
|
2024-04-08 03:29:42 +05:30
|
|
|
}
|
2025-03-24 21:42:07 +03:30
|
|
|
|
2021-08-23 23:50:45 +05:30
|
|
|
jsonschemaData := r.Reflect(&templates.Template{})
|
|
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
encoder := json.NewEncoder(&buf)
|
|
|
|
|
encoder.SetIndent("", " ")
|
2025-03-24 21:42:07 +03:30
|
|
|
if err := encoder.Encode(jsonschemaData); err != nil {
|
|
|
|
|
log.Fatalf("Could not encode JSON schema: %s\n", err)
|
2021-08-23 23:50:45 +05:30
|
|
|
}
|
2025-03-24 21:42:07 +03:30
|
|
|
|
|
|
|
|
schema := pathRegex.ReplaceAllString(buf.String(), "$1")
|
|
|
|
|
writeToFile(os.Args[2], []byte(schema))
|
2021-08-03 22:33:50 +05:30
|
|
|
}
|