lib/ec2ssh/cli.rb in ec2ssh-2.0.8 vs lib/ec2ssh/cli.rb in ec2ssh-3.0.0.beta1

- old
+ new

@@ -1,124 +1,99 @@ require 'thor' require 'highline' -require 'ec2ssh/hosts' require 'ec2ssh/ssh_config' -require 'ec2ssh/dotfile' +require 'ec2ssh/exceptions' +require 'ec2ssh/migrator' module Ec2ssh class CLI < Thor - class_option :path, :banner => "/path/to/ssh_config" - class_option :dotfile, :banner => '$HOME/.ec2ssh', :default => "#{ENV['HOME']}/.ec2ssh" + class_option :dotfile, banner: '$HOME/.ec2ssh', default: "#{ENV['HOME']}/.ec2ssh" + class_option :verbose, banner: 'enable debug log', default: false - desc "init", "Add ec2ssh mark to ssh_config" + desc 'init', 'Add ec2ssh mark to ssh_config' def init - config = SshConfig.new(config_path) - if config.mark_exist? - red "Marker already exists on #{config_path}" - else - config.append_mark! - green "Added mark to #{config_path}" - end - dotfile = Dotfile.update_or_create(options.dotfile, 'path' => config_path) - yellow "Please check and edit #{options.dotfile} before run `ec2ssh update`" + check_dotfile_version + command = make_command :init + command.run + rescue MarkAlreadyExists + red "Marker already exists in #{command.ssh_config_path}" end - desc "update", "Update ec2 hosts list in ssh_config" - method_option :aws_key, :banner => 'aws key name', :default => 'default' + desc 'update', 'Update ec2 hosts list in ssh_config' + method_option :aws_key, banner: 'aws key name', default: 'default' def update - config = SshConfig.new(config_path, options.aws_key) - unless config.mark_exist? - red "Marker not found on #{config_path}" - red "Execute '#{$0} init --path=/path/to/ssh_config' first!" - return - end - - config.parse! - sections = merge_sections(config) - config_str = config.wrap(sections.join("\n")) - config.replace! config_str - yellow config_str - green "Updated #{hosts.size} hosts on #{config_path}" - rescue AwsEnvNotDefined, AwsKeyNotFound - red "Set aws keys at #{options.dotfile}" + check_dotfile_existence + check_dotfile_version + set_aws_logging + command = make_command :update + command.run + green "Updated #{command.ssh_config_path}" + rescue AwsKeyNotFound + red "Set aws keys at #{command.dotfile_path}" + rescue MarkNotFound + red "Marker not found in #{command.ssh_config_path}" + red "Execute '#{$0} init' first!" end - desc "remove", "Remove ec2ssh mark from ssh_config" + desc 'remove', 'Remove ec2ssh mark from ssh_config' def remove - config = SshConfig.new(config_path) - unless config.mark_exist? - red "Marker not found on #{config_path}" - return - end - config.replace! "" - green "Removed mark from #{config_path}" + check_dotfile_existence + check_dotfile_version + command = make_command :remove + command.run + green "Removed mark from #{command.ssh_config_path}" + rescue MarkNotFound + red "Marker not found in #{command.ssh_config_path}" end + desc 'migrate', 'Migrate dotfile from old versions' + def migrate + command = make_command :migrate + command.run + end + no_tasks do - def hl - @hl ||= HighLine.new + def check_dotfile_version + return unless File.exist?(options.dotfile) + migrator = Migrator.new options.dotfile + if migrator.check_version < '3' + red "#{options.dotfile} is old style." + red "Try '#{$0} migrate' to migrate to version 3" + abort + end end - def hosts - @hosts ||= Hosts.new(dotfile, options.aws_key).all + def check_dotfile_existence + unless File.exist?(options.dotfile) + red "#{options.dotfile} doesn't exist." + red "Try '#{$0} init' to generate it or specify the path with --dotfile option" + abort + end end - def dotfile - @dotfile ||= begin - if File.exist?(options.dotfile) - Dotfile.load(options.dotfile) - else - Dotfile.new - end + def make_command(cmd) + require "ec2ssh/command/#{cmd}" + cls = eval "Ec2ssh::Command::#{cmd.capitalize}" + cls.new(self) + end + + def set_aws_logging + if options.verbose + require 'logger' + require 'aws-sdk' + logger = ::Logger.new($stdout) + logger.level = ::Logger::DEBUG + ::AWS.config logger: logger end end - def config_path - options.path || dotfile['path'] || "#{ENV['HOME']}/.ssh/config" + def hl + @hl ||= ::HighLine.new end [:red,:green,:yellow].each do |col| define_method(col) do |str| puts hl.color(str, col) - end - end - - def merge_sections(config) - ssh_options = dotfile['ssh_options'] - - section_str = hosts.map { |h| - section = <<-END -Host #{h[:host]} - HostName #{h[:dns_name]} - END - - unless ssh_options.nil? - ssh_options.each {|line| - section << " #{line}\n" - } - end - - section - }.join - - config.sections[options.aws_key] ||= SshConfig::Section.new( - options.aws_key, - section_str - ) - - sections = config.sections.values.map do |section| - if ( - # section is matched - (section.name == options.aws_key) || - - # for backward compatibility - (config.sections.size == 1 && options.aws_key != 'default') - ) - section.name = options.aws_key - section.replace!(section_str) - end - - section.to_s end end end end end