module DNSUpdater::CLI

For usage in command-line applications

Public Class Methods

main(args) click to toggle source

Main entry point for CLI

# File lib/dnsupdater/cli.rb, line 13
def self.main(args)
    options = getOptions(args)
    if options[:Serve]
        serveWebUpdates(options)
    else
        doSingleUpdate(options)
    end
rescue RuntimeError => e
    abort(e.to_s)
end

Private Class Methods

doSingleUpdate(options) click to toggle source
# File lib/dnsupdater/cli.rb, line 73
def doSingleUpdate(options)
    config = Config.new(options[:ConfigFile])
    DNSUpdater.new(config).update(options[:Target], options[:Protocol])
    puts 'Updated!'
end
getOptionParser(options) click to toggle source
# File lib/dnsupdater/cli.rb, line 27
def getOptionParser(options)
    OptionParser.new do |opts|
        programName = File.basename($PROGRAM_NAME)
        opts.banner = "Usage: #{programName} [options] <target>"
        opts.on('-c', '--config config.yaml', 'Path to config file') do |configFile|
            options[:ConfigFile] = configFile
        end
        opts.on('-t', '--target=PROTOCOL', 'Target protocol (useful for SSH)') do |protocol|
            options[:Protocol] = protocol
        end
        opts.on('-s', '--serve', 'Serve/handle HTTP') do |serve|
            options[:Serve] = true if serve
        end
        opts.on_tail('-h', '--help', 'Show this message') do
            puts opts
            puts "\nSupported targets are: " + Updaters.getAllProtocols.map(&:to_s).sort.join(', ')
            puts 'Target examples:'
            puts '* default:///example.com/10.0.0.1'
            puts '* ssh://dns.example.com:123/example.org/client'
            puts '* http://example.org/dns.example.com/127.0.0.1,192.168.1.1'

            raise SystemExit
        end
    end
end
getOptions(args) click to toggle source
# File lib/dnsupdater/cli.rb, line 53
def getOptions(args)
    options = {
        ConfigFile: 'config.yaml',
        Target: nil,
        Protocol: nil,
        Serve: false
    }

    parser = getOptionParser(options)

    begin
        parser.parse!(args)
    rescue OptionParser::ParseError => e
        warn e.message
        raise SystemExit
    end
    options[:Target] = args.first unless args.empty?
    options
end
prepareWebParams(target) click to toggle source
# File lib/dnsupdater/cli.rb, line 84
def prepareWebParams(target)
    params = DNSUpdater.buildParams
    uri = DNSUpdater.parseTarget(target)
    DNSUpdater.fillUriParams(uri, params)

    params[:Protocol] = :http if params[:Protocol].to_s.empty?
    raise 'Wrong protocol for --serve, must be http!' unless params[:Protocol] == :http

    params
end
serveWebUpdates(options) click to toggle source
# File lib/dnsupdater/cli.rb, line 79
def serveWebUpdates(options)
    params = prepareWebParams(options[:Target])
    Web.startServer(options[:ConfigFile], options[:Protocol], params)
end