Path: blob/dev/pkg/reporting/exporters/jsonexporter/jsonexporter.go
2070 views
package jsonexporter12import (3"os"4"sync"56"github.com/pkg/errors"7"github.com/projectdiscovery/nuclei/v3/pkg/output"8"github.com/projectdiscovery/nuclei/v3/pkg/utils/json"9)1011type Exporter struct {12options *Options13mutex *sync.Mutex14rows []output.ResultEvent15}1617// Options contains the configuration options for JSON exporter client18type Options struct {19// File is the file to export found JSON result to20File string `yaml:"file"`21OmitRaw bool `yaml:"omit-raw"`22}2324// New creates a new JSON exporter integration client based on options.25func New(options *Options) (*Exporter, error) {26exporter := &Exporter{27mutex: &sync.Mutex{},28options: options,29rows: []output.ResultEvent{},30}31return exporter, nil32}3334// Export appends the passed result event to the list of objects to be exported to35// the resulting JSON file36func (exporter *Exporter) Export(event *output.ResultEvent) error {37exporter.mutex.Lock()38defer exporter.mutex.Unlock()3940if exporter.options.OmitRaw {41event.Request = ""42event.Response = ""43}4445// Add the event to the rows46exporter.rows = append(exporter.rows, *event)4748return nil49}5051// Close writes the in-memory data to the JSON file specified by options.JSONExport52// and closes the exporter after operation53func (exporter *Exporter) Close() error {54exporter.mutex.Lock()55defer exporter.mutex.Unlock()5657// Convert the rows to JSON byte array58obj, err := json.Marshal(exporter.rows)59if err != nil {60return errors.Wrap(err, "failed to generate JSON report")61}6263// Attempt to write the JSON to file specified in options.JSONExport64if err := os.WriteFile(exporter.options.File, obj, 0644); err != nil {65return errors.Wrap(err, "failed to create JSON file")66}6768return nil69}707172