From 651bd17612ece4856e71e61df4b6b70f8a2940b6 Mon Sep 17 00:00:00 2001 From: Alex Ling Date: Sat, 30 May 2020 15:14:39 +0000 Subject: [PATCH] Rewrite option parsing using clim and add the `admin` subcommand --- shard.lock | 6 +++- shard.yml | 2 ++ src/mango.cr | 87 +++++++++++++++++++++++++++++++++++++++------------- 3 files changed, 73 insertions(+), 22 deletions(-) diff --git a/shard.lock b/shard.lock index 74ad3e1..e955617 100644 --- a/shard.lock +++ b/shard.lock @@ -2,7 +2,7 @@ version: 1.0 shards: ameba: github: crystal-ameba/ameba - version: 0.12.0 + version: 0.12.1 archive: github: hkalexling/archive.cr @@ -12,6 +12,10 @@ shards: github: schovi/baked_file_system version: 0.9.8 + clim: + github: at-grandpa/clim + version: 0.12.0 + db: github: crystal-lang/crystal-db version: 0.9.0 diff --git a/shard.yml b/shard.yml index e7c9e2d..7ec37f0 100644 --- a/shard.yml +++ b/shard.yml @@ -23,3 +23,5 @@ dependencies: github: hkalexling/archive.cr ameba: github: crystal-ameba/ameba + clim: + github: at-grandpa/clim diff --git a/src/mango.cr b/src/mango.cr index 26149a7..53a281c 100644 --- a/src/mango.cr +++ b/src/mango.cr @@ -2,31 +2,76 @@ require "./config" require "./server" require "./mangadex/*" require "option_parser" +require "clim" -VERSION = "0.4.0" +MANGO_VERSION = "0.4.0" -config_path = nil +macro common_option + option "-c PATH", "--config=PATH", type: String, + desc: "Path to the config file" +end -OptionParser.parse do |parser| - parser.banner = "Mango e-manga server/reader. Version #{VERSION}\n" +macro throw(msg) + puts "ERROR: #{{{msg}}}" + puts + puts "Please see the `--help`." + exit 1 +end - parser.on "-v", "--version", "Show version" do - puts "Version #{VERSION}" - exit - end - parser.on "-h", "--help", "Show help" do - puts parser - exit - end - parser.on "-c PATH", "--config=PATH", - "Path to the config file. " \ - "Default is `~/.config/mango/config.yml`" do |path| - config_path = path +class CLI < Clim + main do + desc "Mango - Manga Server and Web Reader. Version #{MANGO_VERSION}" + usage "mango [sub_command] [options]" + help short: "-h" + version "Version #{MANGO_VERSION}", short: "-v" + common_option + run do |opts| + Config.load(opts.config).set_current + MangaDex::Downloader.default + + server = Server.new + server.start + end + + sub "admin" do + desc "Run admin tools" + usage "mango admin [tool]" + help short: "-h" + run do |opts| + puts opts.help_string + end + sub "user" do + desc "User management tool" + usage "mango admin user [arguments] [options]" + help short: "-h" + argument "action", type: String, + desc: "Action to make. Can be add/delete/update", required: true + argument "username", type: String, + desc: "Username to update or delete" + option "-u USERNAME", "--username=USERNAME", type: String, + desc: "Username" + option "-p PASSWORD", "--password=PASSWORD", type: String, + desc: "Password" + option "--admin", desc: "Admin flag", type: Bool, default: false + common_option + run do |opts, args| + Config.load(opts.config).set_current + + case args.action + when "add" + throw "Options `-u` and `-p` required." if opts.username.nil? || + opts.password.nil? + when "delete" + throw "Argument `username` required." if args.username.nil? + when "update" + throw "Argument `username` required." if args.username.nil? + else + throw "Unknown action \"#{args.action}\"." + end + end + end + end end end -Config.load(config_path).set_current -MangaDex::Downloader.default - -server = Server.new -server.start +CLI.start(ARGV)