Sat, 17 Dec 2011 17:08:33 +0000
Initial commit
yaml-merge | file | annotate | diff | comparison | revisions | |
yaml-query | file | annotate | diff | comparison | revisions | |
yaml-tidy | file | annotate | diff | comparison | revisions |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/yaml-merge Sat Dec 17 17:08:33 2011 +0000 @@ -0,0 +1,39 @@ +#!/usr/bin/env lua5.1 + +local yaml = require "yaml"; + +function merge_in(t1, t2, depth) + depth = depth or 0; + if type(t1) == "table" and type(t2) == "table" then + local array = {}; + if depth < 2 then -- numeric + for i,v in ipairs(t1) do + array[v] = true; + t1[i] = nil; + end + for i,v in ipairs(t2) do + array[v] = true; + end + end + for k,v in pairs(t2) do -- non-numeric + if not(type(k) == "number" and array[v]) then -- fixme, merge non-numeric + t1[k] = merge_in(t1[k], v, depth + 1); + end + end + for v in pairs(array) do + table.insert(t1, v); + end + elseif t2 ~= nil then + t1 = t2; + end + return t1; +end + +local out = {}; +for i, fn in ipairs(arg) do + local f = assert(io.open(fn, "r")); + merge_in(out, yaml.load(f:read("*a"))); + f:close(); +end + +print(yaml.dump(out));
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/yaml-query Sat Dec 17 17:08:33 2011 +0000 @@ -0,0 +1,29 @@ +#!/usr/bin/env lua5.1 + +local yaml = require "yaml"; + +local query = arg[1]; +table.remove(arg, 1); + +local query, string_out = query:gsub("^=", ""); + +local query_func = assert(loadstring("return "..query)); + +if #arg == 0 then arg[1] = "-"; end + +for i, fn in ipairs(arg) do + local f = io.stdin; + if fn ~= "-" then + f = assert(io.open(fn, "r")); + end + local yaml_doc = yaml.load(f:read("*a")); + setfenv(query_func, yaml_doc); + local result = query_func(); + if string_out == 1 then + print(result); + else + print(yaml.dump(result)); + end + f:close(); +end +