require_relative "../errors" 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] end def self.validate_decrypt(options) 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] end end def self.validate_plain(options) abort "--plain needs the --consul_key option passed." if !options[:consul_key] end def self.options_has_one_mode?(options) [ options[:encrypt], options[:decrypt], options[:plain] ].count(true) == 1 end def self.primary_mode_error_message "You must pass only one and at least one of the following flags: --encrypt, --decrypt, or --plain" end def self.secret_adapter MuchKeys::Secret end end