| 1 |
--[[ |
| 2 |
Copyright (c) 2022, Vsevolod Stakhov <[email protected]> |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
]]-- |
| 16 |
|
| 17 |
local argparse = require "argparse" |
| 18 |
|
| 19 |
|
| 20 |
-- Define command line options |
| 21 |
local parser = argparse() |
| 22 |
:name "rspamadm template" |
| 23 |
:description "Apply jinja templates for strings/files" |
| 24 |
:help_description_margin(30) |
| 25 |
parser:argument "file" |
| 26 |
:description "File to process" |
| 27 |
:argname "<file>" |
| 28 |
:args "*" |
| 29 |
|
| 30 |
parser:flag "-n --no-vars" |
| 31 |
:description "Don't add Rspamd internal variables" |
| 32 |
parser:option "-e --env" |
| 33 |
:description "Load additional environment vars from specific file (name=value)" |
| 34 |
:argname "<filename>" |
| 35 |
:count "*" |
| 36 |
parser:option "-l --lua-env" |
| 37 |
:description "Load additional environment vars from specific file (lua source)" |
| 38 |
:argname "<filename>" |
| 39 |
:count "*" |
| 40 |
parser:mutex( |
| 41 |
parser:option "-s --suffix" |
| 42 |
:description "Store files with the new suffix" |
| 43 |
:argname "<suffix>", |
| 44 |
parser:flag "-i --inplace" |
| 45 |
:description "Replace input file(s)" |
| 46 |
) |
| 47 |
|
| 48 |
local lua_util = require "lua_util" |
| 49 |
|
| 50 |
local function set_env(opts, env) |
| 51 |
if opts.env then |
| 52 |
for _, fname in ipairs(opts.env) do |
| 53 |
for kv in assert(io.open(fname)):lines() do |
| 54 |
if not kv:match('%s*#.*') then |
| 55 |
local k, v = kv:match('([^=%s]+)%s*=%s*(.+)') |
| 56 |
|
| 57 |
if k and v then |
| 58 |
env[k] = v |
| 59 |
else |
| 60 |
io.write(string.format('invalid env line in %s: %s\n', fname, kv)) |
| 61 |
end |
| 62 |
end |
| 63 |
end |
| 64 |
end |
| 65 |
end |
| 66 |
|
| 67 |
if opts.lua_env then |
| 68 |
for _, fname in ipairs(opts.env) do |
| 69 |
local ret, res_or_err = pcall(loadfile(fname)) |
| 70 |
|
| 71 |
if not ret then |
| 72 |
io.write(string.format('cannot load %s: %s\n', fname, res_or_err)) |
| 73 |
else |
| 74 |
if type(res_or_err) == 'table' then |
| 75 |
for k, v in pairs(res_or_err) do |
| 76 |
env[k] = lua_util.deepcopy(v) |
| 77 |
end |
| 78 |
else |
| 79 |
io.write(string.format('cannot load %s: not a table\n', fname)) |
| 80 |
end |
| 81 |
end |
| 82 |
end |
| 83 |
end |
| 84 |
end |
| 85 |
|
| 86 |
local function read_file(file) |
| 87 |
local content |
| 88 |
if file == '-' then |
| 89 |
content = io.read("*all") |
| 90 |
else |
| 91 |
local f = assert(io.open(file, "rb")) |
| 92 |
content = f:read("*all") |
| 93 |
f:close() |
| 94 |
end |
| 95 |
return content |
| 96 |
end |
| 97 |
|
| 98 |
local function handler(args) |
| 99 |
local opts = parser:parse(args) |
| 100 |
local env = {} |
| 101 |
set_env(opts, env) |
| 102 |
|
| 103 |
if not opts.file or #opts.file == 0 then |
| 104 |
opts.file = { '-' } |
| 105 |
end |
| 106 |
for _, fname in ipairs(opts.file) do |
| 107 |
local content = read_file(fname) |
| 108 |
local res = lua_util.jinja_template(content, env, opts.no_vars) |
| 109 |
|
| 110 |
if opts.inplace then |
| 111 |
local nfile = string.format('%s.new', fname) |
| 112 |
local out = assert(io.open(nfile, 'w')) |
| 113 |
out:write(content) |
| 114 |
out:close() |
| 115 |
os.rename(nfile, fname) |
| 116 |
elseif opts.suffix then |
| 117 |
local nfile = string.format('%s.%s', opts.suffix) |
| 118 |
local out = assert(io.open(nfile, 'w')) |
| 119 |
out:write(content) |
| 120 |
out:close() |
| 121 |
else |
| 122 |
io.write(res) |
| 123 |
end |
| 124 |
end |
| 125 |
end |
| 126 |
|
| 127 |
return { |
| 128 |
handler = handler, |
| 129 |
description = parser._description, |
| 130 |
name = 'template' |
| 131 |
} |