123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- #!/usr/bin/lua
- local cli = require "cli"
- local json = require "cjson"
- local plpath = require "pl.path"
- local nsm = require "volksdata.namespace"
- local term = require "volksdata.term"
- local triple = require "volksdata.triple"
- local graph = require "volksdata.graph"
- local pkar = require "pocket_archive"
- local cmdoc = require "pocket_archive.cmdoc"
- local model = require "pocket_archive.model"
- local pres = require "pocket_archive.presentation"
- local repo = require "pocket_archive.repo"
- local sub = require "pocket_archive.submission"
- cli.locale "en_US" -- TODO set with multilingual support.
- io.output(io.stdout)
- init = cli.command {
- "Initialize a new Pocket Archive store.",
- function(args)
- io.write("WARNING! This will WIPE all your archive and web data!\n")
- io.write("Enter 'yes' if you know what you are doing: ")
- local a = io.read()
- if a == "yes" then
- io.write("Alright, you asked for it.\n")
- repo.reset_store()
- sub.reset_ores()
- pres.reset_site()
- else io.write("Chicken out.\n")
- end
- end
- }
- list = cli.command {
- "List all resource IDs.",
- function()
- for s in repo.gr:unique_terms(triple.POS_S):iter() do
- print(nsm.denormalize_uri(s.data))
- end
- end,
- }
- deposit = cli.command {
- "Deposit a package.",
- cli.positional "path" { "Path of the laundry list file." },
- cli.flag "c,cleanup" {
- "Remove laundry list and SIP after successful submission.",
- type = cli.boolean,
- },
- function(args)
- sub.deposit(args.path, args.cleanup)
- end
- }
- remove = cli.command {
- "Remove a list of resources.",
- cli.positional "path" {
- "Path of the delete list file or input stream. It must contain \z
- one ID per line, in the short URI format (`par:<ID>'). If not \z
- provided or `-', it is set to standard input.",
- type = cli.string,
- default = "-",
- },
- cli.flag "m,members" {
- "Remove the resource members recursively.",
- type = cli.boolean,
- default = false,
- },
- function(args)
- if args.path == "-" then args.path = nil end
- local ct = 0
- for id in io.lines(args.path) do
- if #id > 0 then -- skip blank lines.
- io.write("Deleting: " .. id .. "\n")
- local del_ids = repo.remove(id, args.members)
- for _ in pairs(del_ids) do ct = ct + 1 end
- end
- end
- io.write("Deleted " .. ct .. " resources.\n")
- end
- }
- gen_site = cli.command {
- "Generate a static site from the archive.",
- function(args) pres.generate_site() end
- }
- dump_res = cli.command {
- "Generate an RDF representation of a resource.",
- cli.positional "id" {
- "ID of the resource, prefixed by `par:`",
- type = cli.string,
- },
- cli.flag "f,format" {
- "RDF format. `nt` and `ttl` are available.",
- type = cli.string,
- default = "ttl",
- },
- cli.flag "o,output" {
- "Output file. If not specified, output to stdout.",
- type = cli.string,
- default = io.stdout,
- },
- function(args)
- local s = term.new_iriref_ns(args.id)
- io.output(args.output)
- for chunk in repo.serialize_rsrc(s, args.format) do
- io.write(chunk)
- end
- io.close() -- This will fail for io.stdout, but it's OK.
- if args.output ~= io.stdout then
- print("File written to ", args.output)
- end
- end,
- }
- dump_ll = cli.command {
- "Generate a laundry list for a stored resource or a whole submission.",
- cli.positional "id" {
- "ID of the resource, prefixed by `par:`",
- type = cli.string,
- },
- cli.flag "o,output" {
- "Output file. If not specified, output to stdout.",
- type = cli.string,
- default = io.stdout,
- },
- function(args)
- local s = term.new_iriref_ns(args.id)
- io.output(args.output)
- if args.id:find("^sub:") then
- -- Dump whole submission.
- --[[
- local co = coroutine.create(pres.generate_sub_ll)
- while true do
- local r, res = coroutine.resume(co, s)
- if not r then break end
- if res then io.write(res) end
- end
- --]]
- io.write(pres.generate_sub_ll(s))
- else
- -- One resource only.
- io.write(pres.generate_res_ll(s))
- end
- io.close() -- This will fail for io.stdout, but it's OK.
- if args.output ~= io.stdout then
- print("File written to ", args.output)
- end
- end
- }
- dump_archive = cli.command {
- "Generate a RDF representation of the full archive.",
- cli.positional "path" {
- "Destination file path.",
- type = cli.string,
- },
- cli.flag "f,format" {
- "RDF serialization format. One of `ttl` [default], `nt`.",
- type = cli.string,
- default = "ttl",
- },
- function(args)
- repo.dump(args.path, args.format)
- print ("File written to ", args.path)
- end,
- }
- list_ctypes = cli.command {
- "List all content types.",
- function(args)
- for tname in pairs(model.types) do
- io.write(tname)
- io.write("\n")
- end
- end,
- }
- dump_schema = cli.command {
- "Generate a structured representation ofa content type schema as JSON.",
- cli.positional "ctype" {
- "Content type name.",
- type = cli.string,
- },
- function(args)
- local schema = model.types[args.ctype]
- if not schema then
- print("No such content type: ", ctype)
- return 1
- end
- io.write(json.encode(schema))
- io.write("\n")
- end,
- }
- gen_cmdoc = cli.command {
- "Generate a static website with content model documentation.",
- function(args)
- cmdoc.generate_doc()
- io.write("Documentation generated at " .. cmdoc.out_dir .. "\n")
- end,
- }
- cli.program {
- "Pocket Archive command line interface.",
- }
|