diff --git a/cmd/bulb/main.ha b/cmd/bulb/main.ha index 5dc6c9d..0880dc5 100644 --- a/cmd/bulb/main.ha +++ b/cmd/bulb/main.ha @@ -19,71 +19,63 @@ export fn main() void = { const cmd = getopt::parse( os::args, "bulletin board", - ("post", [ - ('a', "post anonymously"), - ('b', "board", "post on a board other than general"), - ]): getopt::subcmd_help, - ("read", [ - ('b', "board", "read a board other than general"), - ('n', "number", "display N most recent messages"), - ]): getopt::subcmd_help); + ('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); - match (cmd.subcmd) { - case let subcmd: (str, *getopt::command) => - if (subcmd.0 == "post") { - subcmd_post(subcmd.1); - } else if (subcmd.0 == "read") { - match (subcmd_read(subcmd.1)) { - case errors::invalid => - getopt::printhelp(os::stdout, name, cmd.help)!; - os::exit(os::status::FAILURE); - case void => void; - }; - }; - case void => - subcmd_ls(&cmd); - }; -}; - -fn subcmd_post(cmd: *getopt::command) void = { - let anonymous = false; let board = default_board; - for (let opt .. cmd.opts) { - switch (opt.0) { - case 'a' => anonymous = true; - case 'b' => board = opt.1; - case => abort(); - }; - }; - - let user_name = if (anonymous) void else get_user_name(); - let message = strings::join(" ", cmd.args...); - defer free(message); + let list = false; + let number = 8; + let post = false; + let anonymous = false; - match (bulb::post(os::stdin, board, user_name)) { - case let err: bulb::error => - fmt::errorf("{}: Could not read {}: {}\n", name, board, bulb::strerror(err))!; - case void => void; - }; -}; - -fn subcmd_read(cmd: *getopt::command) (void | errors::invalid) = { - let board = default_board; - let number = 8; 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 => return errors::invalid; + case let num: int => + number = num; + case => + getopt::printhelp(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))!; @@ -91,14 +83,6 @@ fn subcmd_read(cmd: *getopt::command) (void | errors::invalid) = { }; }; -fn subcmd_ls(cmd: *getopt::command) void = { - match (bulb::list(os::stdout)) { - case let err: bulb::error => - fmt::errorf("{}: Could not list boards: {}\n", name, 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.