Skip to content

Commit cb62cac

Browse files
authored
write file in correct JSON format (#201)
1 parent c270fdd commit cb62cac

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

cmd/flow_capture.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ func runFlowCaptureOnAddr(port int, filename string) error {
7979
}
8080
log.Trace("Created flow folder")
8181

82-
f, err = os.Create("./output/flow/" + filename + ".json")
82+
f, err = os.Create("./output/flow/" + filename + ".txt")
8383
if err != nil {
8484
log.Errorf("Create file %s failed: %v", filename, err.Error())
8585
log.Fatal(err)
8686
}
8787
defer f.Close()
88-
log.Trace("Created json file")
88+
log.Trace("Created flow logs txt file")
8989

9090
// Initialize sqlite DB
9191
db := initFlowDB(filename)

scripts/functions.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,25 @@ function copyOutput() {
297297
echo "Copying collector output files..."
298298
mkdir -p ./output
299299
${K8S_CLI_BIN} cp -n "$namespace" collector:output ./output
300+
flowFile=$(find ./output -name "*txt" | sort | tail -1)
301+
if [[ -n "$flowFile" ]] ; then
302+
buildJSON "$flowFile"
303+
rm "$flowFile"
304+
fi
305+
}
306+
307+
function buildJSON() {
308+
file=$1
309+
filename=$(basename "$file")
310+
dirpath=$(dirname "$file")
311+
filenamePrefix=$(echo "$filename" | sed -E 's/(.*)\..*/\1/')
312+
UPDATED_JSON_FILE="$dirpath/$filenamePrefix.json"
313+
{
314+
echo "["
315+
# remove last line and "," (last character) of the last flowlog for valid json
316+
sed '$d' "$file" | sed '$ s/.$//'
317+
echo "]"
318+
} >> "$UPDATED_JSON_FILE"
300319
}
301320

302321
function deleteServiceMonitor() {

0 commit comments

Comments
 (0)