lib/muchkeys/cli/validator.rb in muchkeys-0.3.3 vs lib/muchkeys/cli/validator.rb in muchkeys-0.3.6
- old
+ new
@@ -1,30 +1,55 @@
require_relative "../errors"
+require_relative "./validation"
class MuchKeys::CLI::Validator
def self.validate_primary_mode_option(options)
raise MuchKeys::CLIOptionsError, primary_mode_error_message unless options_has_one_mode?(options)
end
- def self.validate_encrypt(options)
- abort "--encrypt needs the --file option passed." if !options[:file]
- abort "--encrypt needs the --public_key option passed." if !options[:public_key]
+ def self.validate_encrypt_options(options)
+ validation = MuchKeys::CLI::Validation.new
+ if !options[:file] || !options[:public_key]
+ validation.errors << "--decrypt needs the --file and --public_key set."
+ end
+
+ validation
end
- def self.validate_decrypt(options)
+ def self.validate_decrypt_options(options)
+ validation = MuchKeys::CLI::Validation.new
+ if options[:consul_key] && options[:public_key] && options[:private_key]
+ validate_automatic_certificate(validation, options)
+ else
+ validation.errors << "--decrypt needs the --consul_key, --public_key and --private_key set."
+ end
+
+ validation
+ end
+
+ def self.validate_automatic_certificate(chained_validation, options)
+ # i won't mutate chained_validation on principle
+ validation = MuchKeys::CLI::Validation.new
+ validation.errors = chained_validation.errors.dup
key_name = options[:consul_key]
- abort "--decrypt needs the --consul_key option passed." if !key_name
if !secret_adapter.auto_certificates_exist_for_key?(key_name)
certfile_expected = secret_adapter.certfile_name(key_name)
- abort "--decrypt needs the --public_key option passed or a PEM file needs to be at #{certfile_expected}." if !options[:public_key]
- abort "--decrypt needs the --private_key option passed or a PEM file needs to be at #{certfile_expected}." if !options[:private_key]
+ validation.errors << "--decrypt needs the --public_key option passed or a PEM file needs to be at #{certfile_expected}." if !options[:public_key]
+ validation.errors << "--decrypt needs the --private_key option passed or a PEM file needs to be at #{certfile_expected}." if !options[:private_key]
end
+
+ validation
end
- def self.validate_plain(options)
- abort "--plain needs the --consul_key option passed." if !options[:consul_key]
+ def self.validate_plain_options(options)
+ validation = MuchKeys::CLI::Validation.new
+ if !options[:consul_key]
+ validation.errors << "--plain needs the --consul_key option passed."
+ end
+
+ validation
end
def self.options_has_one_mode?(options)
[ options[:encrypt], options[:decrypt], options[:plain] ].count(true) == 1
end