package output
import (
"io"
"os"
"sync"
"github.com/logrusorgru/aurora"
"github.com/projectdiscovery/nuclei/v3/pkg/model/types/severity"
fileutil "github.com/projectdiscovery/utils/file"
)
type WriterOptions func(s *StandardWriter) error
func WithJson(json bool, dumpReqResp bool) WriterOptions {
return func(s *StandardWriter) error {
s.json = json
s.jsonReqResp = dumpReqResp
return nil
}
}
func WithTimestamp(timestamp bool) WriterOptions {
return func(s *StandardWriter) error {
s.timestamp = timestamp
return nil
}
}
func WithNoMetadata(noMetadata bool) WriterOptions {
return func(s *StandardWriter) error {
s.noMetadata = noMetadata
return nil
}
}
func WithMatcherStatus(matcherStatus bool) WriterOptions {
return func(s *StandardWriter) error {
s.matcherStatus = matcherStatus
return nil
}
}
func WithAurora(aurora aurora.Aurora) WriterOptions {
return func(s *StandardWriter) error {
s.aurora = aurora
return nil
}
}
func WithWriter(outputFile io.WriteCloser) WriterOptions {
return func(s *StandardWriter) error {
s.outputFile = outputFile
return nil
}
}
func WithTraceSink(traceFile io.WriteCloser) WriterOptions {
return func(s *StandardWriter) error {
s.traceFile = traceFile
return nil
}
}
func WithErrorSink(errorFile io.WriteCloser) WriterOptions {
return func(s *StandardWriter) error {
s.errorFile = errorFile
return nil
}
}
func WithSeverityColors(severityColors func(severity.Severity) string) WriterOptions {
return func(s *StandardWriter) error {
s.severityColors = severityColors
return nil
}
}
func WithStoreResponse(storeResponse bool, respDir string) WriterOptions {
return func(s *StandardWriter) error {
s.storeResponse = storeResponse
s.storeResponseDir = respDir
return nil
}
}
func NewWriter(opts ...WriterOptions) (*StandardWriter, error) {
s := &StandardWriter{
mutex: &sync.Mutex{},
DisableStdout: true,
AddNewLinesOutputFile: true,
}
for _, opt := range opts {
if err := opt(s); err != nil {
return nil, err
}
}
if s.aurora == nil {
s.aurora = aurora.NewAurora(false)
}
if s.outputFile == nil {
s.outputFile = os.Stdout
}
if s.storeResponse && !fileutil.FolderExists(s.storeResponseDir) {
if err := fileutil.CreateFolder(s.storeResponseDir); err != nil {
return nil, err
}
}
return s, nil
}