Added useLocalDatabase global flag to allow for local db files for separate engagements.
This commit is contained in:
@@ -75,3 +75,59 @@ func WriteToFile(results sqlite.DehashedResults, outputFile string, fileType fil
|
||||
filePath := fmt.Sprintf("%s.%s", outputFile, fileType)
|
||||
return ioutil.WriteFile(filePath, data, 0644)
|
||||
}
|
||||
|
||||
// WriteQueryResultsToFile writes query results to a file in the specified format
|
||||
func WriteQueryResultsToFile(results []map[string]interface{}, outputFile string, fileType files.FileType) error {
|
||||
var data []byte
|
||||
var err error
|
||||
|
||||
switch fileType {
|
||||
case files.JSON:
|
||||
data, err = json.MarshalIndent(results, "", " ")
|
||||
case files.XML:
|
||||
data, err = xml.MarshalIndent(results, "", " ")
|
||||
case files.YAML:
|
||||
data, err = yaml.Marshal(results)
|
||||
case files.TEXT:
|
||||
var outStrings []string
|
||||
for _, r := range results {
|
||||
var rowStrings []string
|
||||
for k, v := range r {
|
||||
// Format the value to avoid array notation
|
||||
var valueStr string
|
||||
switch val := v.(type) {
|
||||
case []string:
|
||||
valueStr = strings.Join(val, ", ")
|
||||
case []interface{}:
|
||||
strSlice := make([]string, len(val))
|
||||
for i, item := range val {
|
||||
if item == nil {
|
||||
strSlice[i] = ""
|
||||
} else {
|
||||
strSlice[i] = fmt.Sprintf("%v", item)
|
||||
}
|
||||
}
|
||||
valueStr = strings.Join(strSlice, ", ")
|
||||
default:
|
||||
if v == nil {
|
||||
valueStr = ""
|
||||
} else {
|
||||
valueStr = fmt.Sprintf("%v", v)
|
||||
}
|
||||
}
|
||||
rowStrings = append(rowStrings, fmt.Sprintf("%s: %s", k, valueStr))
|
||||
}
|
||||
outStrings = append(outStrings, strings.Join(rowStrings, "\n")+"\n\n")
|
||||
}
|
||||
data = []byte(strings.Join(outStrings, ""))
|
||||
default:
|
||||
return errors.New("unsupported file type")
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
filePath := fmt.Sprintf("%s.%s", outputFile, fileType.String())
|
||||
return os.WriteFile(filePath, data, 0644)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user