The outputfile is the absolute path + create intermediate dirs

master
Víctor Zamanillo 2020-08-17 17:49:14 +02:00
parent 6650e3665a
commit 6f3cb22b6a
2 changed files with 9 additions and 10 deletions

View File

@ -153,7 +153,7 @@ func (r *Runner) EnumerateSingleDomain(ctx context.Context, domain, output strin
} }
if output != "" { if output != "" {
file, err := outputter.createFile(output, r.options.OutputDirectory, appendToFile) file, err := outputter.createFile(output, appendToFile)
if err != nil { if err != nil {
gologger.Errorf("Could not create file %s for %s: %s\n", output, domain, err) gologger.Errorf("Could not create file %s for %s: %s\n", output, domain, err)
return err return err

View File

@ -5,7 +5,7 @@ import (
"errors" "errors"
"io" "io"
"os" "os"
"path" "path/filepath"
"strings" "strings"
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
@ -28,29 +28,28 @@ func NewOutputter(json bool) *OutPutter {
return &OutPutter{JSON: json} return &OutPutter{JSON: json}
} }
func (o *OutPutter) createFile(filename, outputDirectory string, appendtoFile bool) (*os.File, error) { func (o *OutPutter) createFile(filename string, appendtoFile bool) (*os.File, error) {
if filename == "" { if filename == "" {
return nil, errors.New("empty filename") return nil, errors.New("empty filename")
} }
absFilePath := filename dir := filepath.Dir(filename)
if outputDirectory != "" { if dir != "" {
if _, err := os.Stat(outputDirectory); os.IsNotExist(err) { if _, err := os.Stat(dir); os.IsNotExist(err) {
err := os.MkdirAll(outputDirectory, os.ModePerm) err := os.MkdirAll(dir, os.ModePerm)
if err != nil { if err != nil {
return nil, err return nil, err
} }
} }
absFilePath = path.Join(outputDirectory, filename)
} }
var file *os.File var file *os.File
var err error var err error
if appendtoFile { if appendtoFile {
file, err = os.OpenFile(absFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) file, err = os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
} else { } else {
file, err = os.Create(absFilePath) file, err = os.Create(filename)
} }
if err != nil { if err != nil {
return nil, err return nil, err