bulb/cmd/bulb/main.ha

92 lines
2.1 KiB
Hare

use errors;
use fmt;
use fs;
use getopt;
use internal::bulb;
use io;
use os;
use path;
use strconv;
use strings;
use unix;
const default_board = "general";
let name = "";
@init fn name() void = name = os::args[0];
export fn main() void = {
const cmd = getopt::parse(
os::args,
"bulletin board",
('b', "board", "specify a board other than general"),
('l', "list available boards and exit"),
('n', "number", "display N most recent messages"),
('p', "post a message (from stdin)"),
('u', "operate undercover (anonymously)"));
defer getopt::finish(&cmd);
let board = default_board;
let list = false;
let number = 8;
let post = false;
let anonymous = false;
for (let opt .. cmd.opts) {
switch (opt.0) {
case 'b' =>
board = opt.1;
case 'l' =>
list = true;
case 'n' =>
match (strconv::stoi(opt.1)) {
case let num: int =>
number = num;
case =>
fmt::errorf("{}: option -{} requires a number\n", name, opt.0)!;
getopt::printusage(os::stdout, name, cmd.help)!;
os::exit(os::status::FAILURE);
};
case 'p' =>
post = true;
case 'u' =>
anonymous = true;
case => abort();
};
};
if (list) {
// list boards and exit
match (bulb::list(os::stdout)) {
case let err: bulb::error =>
fmt::errorf("{}: could not list boards: {}\n", name, bulb::strerror(err))!;
os::exit(os::status::FAILURE);
case void =>
os::exit(os::status::SUCCESS);
};
};
if (post) {
// post a message (from stdin)
let user_name = if (anonymous) void else get_user_name();
match (bulb::post(os::stdin, board, user_name)) {
case let err: bulb::error =>
fmt::errorf("{}: could not post to {}: {}\n", name, board, bulb::strerror(err))!;
case void => void;
};
};
// read latest posts on the board
match (bulb::read(os::stdout, board, number)) {
case let err: bulb::error =>
fmt::errorf("{}: could not read {}: {}\n", name, board, bulb::strerror(err))!;
case void => void;
};
};
fn get_user_name() str = {
// FIXME: could not find support for getting a username from a uid in
// hare. when it is added, use that here.
return os::tryenv("USER", "anonymous");
};