require 'commander' require_relative 'runner' require 'pp' module Henk class CommandsGenerator include Commander::Methods def self.start self.new.run end def run add_commander_methods Henk::Runner.new.verify_cluster_active program :name, 'henk' program :version, Sct::VERSION program :description, 'CLI for \'henk\' - Interact with Henk 3.0 to be able to create/copy/delete customers' $dev = false $verbose = false global_option('--dev') { $dev = true } global_option('--verbose') { $verbose = true } command :create do |c| c.syntax = 'sct henk create' c.description = 'Create a client in your local development.' c.required_option '--domain DOMAIN', 'The domain name of the new customer.' c.required_option '--organization-name ORGANIZATION_NAME', 'The name of the organization.' c.required_option '--modules MODULES', 'Allowed modules. ex: 01, 02, 03, ...' c.action do |args, options| c.validate options options.dev = $dev options.verbose = $verbose Henk::Runner.new.henk "create", options Henk::Runner.new.encrypt_config_data options.domain end end command :copy do |c| c.syntax = 'sct henk copy' c.description = 'Copy a client from another environment to your local environment' c.required_option '--domain DOMAIN', 'The domain to copy.' c.option '--source-environment SOURCE_ENVIRONMENT', 'The name of the source environment (currently only local allowed).' c.option '--target-domain DOMAIN', 'The name of the domain on the local environment. When making a local copy it cannot be the same name' c.option '--overwrite', 'Set this flag if you want to overwrite an existing target-domain.' c.option '--copy-data', 'Set this flag if you want to copy the data of the source client' # Specify command options here c.action do |args, options| c.validate options unless options.source_environment options.source_environment = "local" end # Temporary provision until we can copy environments from Accept if options.source_environment != "local" raise ArgumentError.new("It's currently not possible to copy an cloud environment.".yellow) end options.target_environment = "local" unless options.target_domain options.target_domain = options.domain end if options.source_environment == options.target_environment \ and options.domain == options.target_domain raise ArgumentError.new("The source and target domains cannot be the same.") end options.dev = $dev options.verbose = $verbose Henk::Runner.new.henk "copy", options Henk::Runner.new.encrypt_config_data options.target_domain Henk::Runner.new.synchronize_employees options.target_domain end end command :remove do |c| c.syntax = 'sct henk remove' c.description = 'Remove a client from your local environment.' c.required_option '--domain DOMAIN', 'The domain to remove.' c.option '--user-email EMAIL', 'The email address of the user removing the customer' c.action do |args, options| c.validate options options.dev = $dev options.verbose = $verbose Henk::Runner.new.henk 'remove', options end end command :"list-clients" do |c| c.syntax = 'sct henk list-clients' c.description = 'List all clients that are available on the dev environment' c.action do |args, options| Henk::Runner.new.list_clients end end command :"import" do |c| c.syntax = 'sct henk import []' c.description = 'Import a customer dump from GCP.' c.action do |args, options| case args.length when 0 puts "Loading available customer dumps...".blue available_dumps = `gsutil ls gs://henk-db-dumps/`.lines chomp: true begin dump_url = choose do |menu| menu.prompt = "Please choose a dump to import:" for dump in available_dumps menu.choice dump end end rescue Interrupt # user pressed Ctrl+C, return in order to exit gracefully return end when 1 dump_url = args.first else UI.error "Expected 0 or 1 arguments, received #{args.length}: #{args}" exit 1 end Henk::Runner.new.import args, options, dump_url end end run! end # Ruben's suggestion but still need some workaround def add_commander_methods Commander::Command.class_eval { attr_accessor :required def required_option(*arg, &block) unless self.required @required = [] end option = arg.select{|item| item.start_with?('--')}.map{|a| a.gsub(/\-\-([^\s]*)(.*)/, '\1')}.shift self.required << option.to_s.tr('-', '_') self.option(*arg) end def validate options self.required.each do |option| if options.__hash__[:"#{option}"].nil? || options.__hash__[:"#{option}"] == '' raise ArgumentError.new("The --#{option} argument is a required argument") end end end } end end end