diff --git a/internal/exporter/directory.go b/internal/exporter/directory.go index 6cb2602..65d7eab 100644 --- a/internal/exporter/directory.go +++ b/internal/exporter/directory.go @@ -51,22 +51,27 @@ func (e *exporter) isExtensionValid(extension string) bool { func (e *exporter) parseFile(filePath string, value string, localData map[string]string) { // Extract our file extension and cleanup file path ext := filepath.Ext(filePath) - path := e.cleanFilePath(filePath) - // Check if we should parse JSON files - if e.config.ShouldExpandJSON() { - // Check if the file is a JSON one - if ext == ".json" { + cleanedPath := e.cleanFilePath(filePath) + + // Check if the file is a JSON one + if ext == ".json" { + // Check if we should parse JSON files + if e.config.ShouldExpandJSON() { // Great, we should iterate our JSON (And that's the value) - e.expandJSON(path, value, localData) + e.expandJSON(cleanedPath, value, localData) // we must return here, to avoid importing the file as blob return } + + // Not expanding json file, but we should validate anyways + // HEADS UP: Below function will exit program if any error found + _ = e.validateJSON(cleanedPath, value) } // Not expanding JSON files, create new single "piece" with the // value given (the file content) and add to collection - piece := e.createPiece(path, value) + piece := e.createPiece(cleanedPath, value) localData[piece.KVPath] = piece.Value } diff --git a/internal/exporter/jsonexpander.go b/internal/exporter/jsonexpander.go index db4e6bc..3fbd2f4 100644 --- a/internal/exporter/jsonexpander.go +++ b/internal/exporter/jsonexpander.go @@ -9,8 +9,8 @@ import ( "strconv" ) -// expandJSON ... -func (e *exporter) expandJSON(path string, jsonData string, localData map[string]string) { +// validateJSON ... +func (e *exporter) validateJSON(path string, jsonData string) map[string]interface{} { // Create "generic" json struct var arbitraryJSON map[string]interface{} @@ -20,12 +20,19 @@ func (e *exporter) expandJSON(path string, jsonData string, localData map[string // Decoded JSON ok? if err != nil { util.ExitError( - errors.New(fmt.Sprintf("error parsing JSON file: %s", err.Error())), + errors.New(fmt.Sprintf("error parsing JSON file: %s with Message: %s", path, err.Error())), util.ErrorFailedJsonDecode, e.logger, ) } + return arbitraryJSON +} + +// expandJSON ... +func (e *exporter) expandJSON(path string, jsonData string, localData map[string]string) { + arbitraryJSON := e.validateJSON(path, jsonData) + // Iterate over our "generic" JSON structure e.traverseJSON(path, arbitraryJSON, localData) }