diff --git a/lib/schleuder-cli.rb b/lib/schleuder-cli.rb index 6ddff3d536cee6ce033f4b5f98dedf21864b48c0..f8218f902521e46bf34bde1082d8c06dfd94cb1c 100644 --- a/lib/schleuder-cli.rb +++ b/lib/schleuder-cli.rb @@ -19,4 +19,4 @@ require 'schleuder-cli/lists' require 'schleuder-cli/keys' require 'schleuder-cli/base' -ENV["SCHLEUDER_CLI_CONFIG"] ||= File.join(ENV['HOME'], '.schleuder-cli/schleuder-cli.yml') +DEFAULT_CONFIG_FILE = File.join(ENV['HOME'], '.schleuder-cli/schleuder-cli.yml') diff --git a/lib/schleuder-cli/base.rb b/lib/schleuder-cli/base.rb index d5cd318bd6b9a7bc976ddc2d297a665a0dca2f3c..850a02a18dff28301769c7f74a0455eb5e5bdbce 100644 --- a/lib/schleuder-cli/base.rb +++ b/lib/schleuder-cli/base.rb @@ -2,6 +2,8 @@ module SchleuderCli class Base < Thor include Helper + class_option :configfile, aliases: '-c', banner: '<path/to/file>', desc: 'alternative configuration file' + register(Subscriptions, 'subscriptions', 'subscriptions ...', diff --git a/lib/schleuder-cli/conf.rb b/lib/schleuder-cli/conf.rb index 3ac74417ccdf2e94359d0dac9024729d250a65b2..b187107393c36f90db9a4216e5bbf295f7e6d90b 100644 --- a/lib/schleuder-cli/conf.rb +++ b/lib/schleuder-cli/conf.rb @@ -1,6 +1,6 @@ module SchleuderCli class Conf - include Singleton + attr_accessor :filename DEFAULTS = { 'api' => { @@ -12,39 +12,31 @@ module SchleuderCli 'api_key' => nil } - def config - @config ||= self.class.load_config('schleuder-cli', ENV['SCHLEUDER_CLI_CONFIG']) + def initialize(filename) + @config ||= load_config(filename) + @filename = filename end - def self.load_config(filename) - file = Pathname.new(filename) - if file.exist? - config = load_config_file(file) - else - config = write_defaults_to_config_file(file) - end - end - - def self.api - instance.config['api'] || {} + def api + @config['api'] || {} end - def self.api_use_tls? + def api_use_tls? api['use_tls'].to_s == "true" end - def self.api_key - instance.config['api_key'].to_s + def api_key + @config['api_key'].to_s end - def self.api_cert_file + def api_cert_file path = api['remote_cert_file'].to_s if path.empty? - fatal "Error: remote_cert_file is empty, can't verify remote server without it (in #{ENV['SCHLEUDER_CLI_CONFIG']})." + fatal "Error: remote_cert_file is empty, can't verify remote server without it (in #{self.filename})." end file = Pathname.new(api['remote_cert_file'].to_s).expand_path if ! file.readable? - fatal "Error: remote_cert_file is set to a not readable file (in #{ENV['SCHLEUDER_CLI_CONFIG']})." + fatal "Error: remote_cert_file is set to a not readable file (in #{self.filename})." end file.to_s end @@ -52,7 +44,16 @@ module SchleuderCli private - def self.load_config_file(file) + def load_config(filename) + file = Pathname.new(filename) + if file.exist? + load_config_file(file) + else + write_defaults_to_config_file(file) + end + end + + def load_config_file(file) if ! file.readable? fatal "Error: #{file} is not readable." end @@ -60,9 +61,10 @@ module SchleuderCli if ! yaml.is_a?(Hash) fatal "Error: #{file} cannot be parsed correctly, please fix it. (To get a new default configuration file remove the current one and run again.)" end + yaml end - def self.write_defaults_to_config_file(file) + def write_defaults_to_config_file(file) dir = file.dirname if ! dir.writable? fatal "Error: '#{dir}' is not writable, cannot write default config to '#{file}'." @@ -76,7 +78,7 @@ module SchleuderCli DEFAULTS end - def self.fatal(msg) + def fatal(msg) $stderr.puts msg exit 1 end diff --git a/lib/schleuder-cli/helper.rb b/lib/schleuder-cli/helper.rb index 84e651ead7857a2bfee661a71d94e7a7e8338ea6..7478f97c2df7345b094e4c5978294c851ea540cf 100644 --- a/lib/schleuder-cli/helper.rb +++ b/lib/schleuder-cli/helper.rb @@ -3,15 +3,15 @@ module SchleuderCli def api @http ||= begin - host = Conf.api['host'] - port = Conf.api['port'] + host = conf.api['host'] + port = conf.api['port'] http = Net::HTTP.new(host, port) - if Conf.api_use_tls? + if conf.api_use_tls? require 'openssl_ssl_patch' http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER http.verify_callback = lambda { |*a| ssl_verify_callback(*a) } - #http.ca_file = Conf.api_cert_file + #http.ca_file = conf.api_cert_file end http end @@ -63,8 +63,8 @@ module SchleuderCli def request(req, &block) test_mandatory_config - if Conf.api_use_tls? || Conf.api['host'] == 'localhost' - req.basic_auth 'schleuder', Conf.api_key + if conf.api_use_tls? || conf.api['host'] == 'localhost' + req.basic_auth 'schleuder', conf.api_key end debug "Request to API: #{req.inspect}" debug "API request path: #{req.path.inspect}" @@ -215,23 +215,27 @@ module SchleuderCli return true end fingerprint = OpenSSL::Digest::SHA256.new(cert.to_der).to_s - fingerprint == Conf.api['tls_fingerprint'] + fingerprint == conf.api['tls_fingerprint'] + end + + def conf + @conf ||= Conf.new(options.configfile || DEFAULT_CONFIG_FILE) end def test_mandatory_config - if Conf.api['host'].to_s.empty? - fatal "Error: 'host' is empty, can't connect (in #{ENV['SCHLEUDER_CLI_CONFIG']})." + if conf.api['host'].to_s.empty? + fatal "Error: 'host' is empty, can't connect (in #{conf.filename})." end - if Conf.api['port'].to_s.empty? - fatal "Error: 'port' is empty, can't connect (in #{ENV['SCHLEUDER_CLI_CONFIG']})." + if conf.api['port'].to_s.empty? + fatal "Error: 'port' is empty, can't connect (in #{conf.filename})." end - if Conf.api_use_tls? - if Conf.api['tls_fingerprint'].to_s.empty? - fatal "Error: 'tls_fingerprint' is empty but required if 'use_tls' is true (in #{ENV['SCHLEUDER_CLI_CONFIG']})." + if conf.api_use_tls? + if conf.api['tls_fingerprint'].to_s.empty? + fatal "Error: 'tls_fingerprint' is empty but required if 'use_tls' is true (in #{conf.filename})." end - if Conf.api_key.empty? - fatal "Error: 'api_key' is empty but required if 'use_tls' is true (in #{ENV['SCHLEUDER_CLI_CONFIG']})." + if conf.api_key.empty? + fatal "Error: 'api_key' is empty but required if 'use_tls' is true (in #{conf.filename})." end end end