require "email" require "option_parser" require "baguette-crystal-base" require "crinja" class Context class_property simulation : Bool = false class_property command : String = "undefined" class_property args = [] of String end class Baguette::Configuration class Mailer < Base # Default mailer: 127.0.0.1, not "localhost" for glibc fuckery reasons. # Without domain name resolution, one can statically compile the mailer # for machines without glibc (such as on alpine linux). property smtpd_host : String = "127.0.0.1" property smtpd_port : UInt16 = 25 # "your.host.example.com" property smtpd_helo_domain : String = "localhost" property verbosity : Int32 = 3 # template => subject property templates : YAML::Any? = nil property templates_directory : String = "/etc/baguette/mailer/templates/" def initialize end end end class OptionParser def to_s(io : IO) if banner = @banner io << banner io << "\n\n" end @flags.join io, "\n" end end opt_simulation = -> (parser : OptionParser) { parser.on "-s", "--simulation", "Simulation: do not do anything." do Context.simulation = true Baguette::Log.info "Simulation." end } unrecognized_args_to_context_args = -> (parser : OptionParser, nexact : Int32?, at_least : Int32?) { # With the right args, these will be interpreted as serialized data. parser.unknown_args do |args| # either we test with the exact expected number of arguments or the least. if exact = nexact if args.size != exact Baguette::Log.error "#{parser}" exit 1 end elsif least = at_least if args.size < least Baguette::Log.error "#{parser}" exit 1 end else Baguette::Log.error "Number of parameters not even provided!" Baguette::Log.error "#{parser}" exit 1 end args.each do |arg| Baguette::Log.debug "Unrecognized argument: #{arg} (adding to Context.args)" if Context.args.nil? Context.args = Array(String).new end Context.args.not_nil! << arg end end } opt_help = -> (parser : OptionParser) { parser.on "-h", "--help", "Prints command usage." do puts "usage: #{PROGRAM_NAME} command -h" puts puts parser case Context.command when /send/ #ENUM.names.each do |n| # Baguette::Log.warning "- #{n}" #end end exit 0 end } parser = OptionParser.new do |parser| parser.banner = "Mailer." parser.on "-v verbosity", "--verbosity v", "Verbosity. From 0 to 4 (debug)." do |v| Baguette::Context.verbosity = v.to_i Baguette::Log.info "verbosity = #{v}" end opt_simulation.call parser parser.on("send", "Send an email.") do parser.banner = "usage: send