Skip to content

Commit

Permalink
Merge pull request #16 from GSA-TTS/luacheck
Browse files Browse the repository at this point in the history
Update scripts per luacheck and add a script to run luacheck
  • Loading branch information
mogul authored Jun 19, 2024
2 parents bf39a74 + a7a33be commit 9b00429
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 26 deletions.
1 change: 1 addition & 0 deletions .cfignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dev-utils
9 changes: 9 additions & 0 deletions dev-utils/luacheck-scripts.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh

# Spins up a docker container to run luacheck on the scripts directory.
# Run this from the repo root (it uses pwd).

# luacheck image tags include "latest" and "edge," "edge" being later than "latest"
docker run -it --rm \
-v "$(pwd)/scripts:/code/scripts" \
pipelinecomponents/luacheck:latest luacheck scripts
8 changes: 8 additions & 0 deletions scripts/README-luacheck.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# README: luacheck

The `dev-utils/luacheck-scripts.sh` script will spin up a small docker container and run luacheck on all `.lua` files in this directory.

luacheck documentation: https://luacheck.readthedocs.io/en/stable/config.html

A `--luacheck:ignore` comment after a function declaration suppresses a warning about setting a global variable and that this is an unused function. (Scripts that are called only from fluent bit will appear to be unused.) It does _not_ tell luacheck to ignore the rest of the function.

14 changes: 0 additions & 14 deletions scripts/parse_http_headers.lua

This file was deleted.

27 changes: 15 additions & 12 deletions scripts/parse_keys_with_eq_pairs.lua
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
-- This parser is just a proof-of-concept.
-- These functions parse "tags" and "gauge" data into JSON key/value
-- pairs. The "eq_pairs" of the function name are strings of
-- non-whitespace (and non-=) characters separated by an equal sign.

KEYS_TO_PARSE = {"tags", "gauge"}
local KEYS_TO_PARSE = {"tags", "gauge"}

-- Splits a string of foo="bar" pairs and makes parsable json out of it.
function parse_keys_with_eq_pairs(tag, timestamp, record)
for k, v in pairs(KEYS_TO_PARSE) do
if (record[v] ~= nil) then
local eq_pairs_to_json_string = function (orig_string)
local new_string = string.gsub(orig_string, "(%S+)=(%S+)", "\"%1\":%2,")
-- trim off that last , and add curly braces around:
new_string = string.gsub(new_string, "(.+),$", "{%1}")
return new_string
end

-- Splits a string of foo="bar" pairs and makes parsable json out of it.
function parse_keys_with_eq_pairs(tag, timestamp, record) --luacheck:ignore
for _, v in pairs(KEYS_TO_PARSE) do
if (record[v] ~= nil) then
record[v] = eq_pairs_to_json_string(record[v])
end
end
-- 2 leaves timestamp unchanged
return 2, timestamp, record
end

function eq_pairs_to_json_string(orig_string)
local new_string = string.gsub(orig_string, "(%S+)=(%S+)", "\"%1\":%2,")
-- trim off that last , and add curly braces around:
new_string = string.gsub(new_string, "(.+),$", "{%1}")
return new_string
end

0 comments on commit 9b00429

Please sign in to comment.