# Command-line interface # Copyright (C) 2023 Nguyễn Gia Phong # # This file if part of hybring. # # Hybring is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Hybring is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with hybring. If not, see . require "ini" require "option_parser" require "./http" enum Subcommand Serve Usage end struct Configuration @sock : String getter sock @db : Path getter db @css : Path getter css @opennic_local : Path getter opennic_local @opennic_remote : String getter opennic_remote @opennic_api : String getter opennic_api @icann_local : Path getter icann_local @icann_remote : String getter icann_remote @icann_api : String getter icann_api def initialize(ini) @sock = ini["general"]["sock"] @db = Path[ini["general"]["db"]] @css = Path[ini["general"]["css"]] @opennic_local = Path[ini["opennic"]["local"]] @opennic_remote = ini["opennic"]["remote"] @opennic_api = ini["opennic"]["api"] @icann_local = Path[ini["icann"]["local"]] @icann_remote = ini["icann"]["remote"] @icann_api = ini["icann"]["api"] end end def die(error) STDERR << error exit 1 end {% unless @top_level.constant("SPEC") %} subcmd = Subcommand::Usage usage = "" config_path, port = nil, 0 parser = OptionParser.new do |parser| banner_prefix = "Usage: #{Path[PROGRAM_NAME].basename}" parser.banner = "#{banner_prefix} [subcommand] [arguments]" parser.on "serve", "start the HTTP server" do subcmd = Subcommand::Serve parser.banner = "#{banner_prefix} serve --config=PATH [--port=N]" usage = parser.to_s parser.on "-c PATH", "--config=PATH", "path to configuration file (required)" do |path| config_path = path end parser.on "-p N", "--port=N", "listening port" do |n| port = n.to_i end end parser.on "-h", "--help", "show this help message and exit" do puts parser exit end parser.invalid_option do |flag| STDERR.puts "Error: Invalid option: #{flag}" subcmd = Subcommand::Usage end parser.missing_option do |flag| STDERR.puts "Error: Missing argument for #{flag}" subcmd = Subcommand::Usage end parser.unknown_args do |before_dash, after_dash| next if before_dash.empty? && after_dash.empty? STDERR << "Error: Unknown arguments:" STDERR << " " << before_dash.join " " if !before_dash.empty? STDERR << " -- " << after_dash.join " " if !after_dash.empty? STDERR.puts subcmd = Subcommand::Usage end end parser.parse cfg = case subcmd when .serve? unless config_path die usage end begin Configuration.new INI.parse File.read config_path.not_nil! rescue ex die "Error: Failed to read config from #{config_path}: #{ex}\n" end end case subcmd in .serve? server = Server.new cfg.not_nil! server.listen port do |address| puts "Listening on #{address}" end in .usage? die parser end {% end %}