yaml-query

Sat, 17 Dec 2011 17:08:33 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Sat, 17 Dec 2011 17:08:33 +0000
changeset 0
0757d85e604b
permissions
-rwxr-xr-x

Initial commit

0
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env lua5.1
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local yaml = require "yaml";
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local query = arg[1];
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 table.remove(arg, 1);
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local query, string_out = query:gsub("^=", "");
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local query_func = assert(loadstring("return "..query));
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 if #arg == 0 then arg[1] = "-"; end
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 for i, fn in ipairs(arg) do
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local f = io.stdin;
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 if fn ~= "-" then
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 f = assert(io.open(fn, "r"));
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 end
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local yaml_doc = yaml.load(f:read("*a"));
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 setfenv(query_func, yaml_doc);
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 local result = query_func();
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 if string_out == 1 then
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 print(result);
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 else
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 print(yaml.dump(result));
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 end
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 f:close();
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 end
0757d85e604b Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29

mercurial