lib/ec2ssh/cli.rb in ec2ssh-1.0.3 vs lib/ec2ssh/cli.rb in ec2ssh-2.0.0
- old
+ new
@@ -1,68 +1,77 @@
require 'thor'
-require 'right_aws'
require 'highline'
+require 'ec2ssh/hosts'
+require 'ec2ssh/ssh_config'
+require 'ec2ssh/dotfile'
module Ec2ssh
class CLI < Thor
- path_option = [:path, {:banner => "/path/to/ssh_config", :default=>"#{ENV['HOME']}/.ssh/config"}]
+ class_option :path, :banner => "/path/to/ssh_config"
+ class_option :dotfile, :banner => '$HOME/.ec2ssh', :default => "#{ENV['HOME']}/.ec2ssh"
desc "init", "Add ec2ssh mark to ssh_config"
- method_option *path_option
def init
- config = Config.new(options.path)
+ config = SshConfig.new(config_path)
if config.mark_exist?
- red "Marker already exists on #{options.path}"
- return
+ red "Marker already exists on #{config_path}"
+ else
+ config.append_mark!
+ green "Added mark to #{config_path}"
end
- config.append_mark!
- green "Added mark to #{options.path}"
+ dotfile = Dotfile.update_or_create(options.dotfile, 'path' => options.path)
+ yellow "Please check and edit #{options.dotfile} before run `ec2ssh update`"
end
desc "update", "Update ec2 hosts list in ssh_config"
- method_option *path_option
+ method_option :aws_key, :banner => 'aws key name', :default => 'default'
def update
- config = Config.new(options.path)
+ config = SshConfig.new(config_path)
unless config.mark_exist?
- red "Marker not found on #{options.path}"
- red "Execute '#{$0} init --path=#{options.path}' first!"
+ red "Marker not found on #{config_path}"
+ red "Execute '#{$0} init --path=/path/to/ssh_config' first!"
return
end
- hosts = Hosts.new.all
config_str = config.wrap(hosts.map{|h|<<-END}.join)
Host #{h[:host]}
HostName #{h[:dns_name]}
END
config.replace! config_str
yellow config_str
- green "Updated #{hosts.size} hosts on #{options.path}"
- rescue AwsEnvNotDefined
- red <<-END
-Set environment variables to access AWS such as:
- export AMAZON_ACCESS_KEY_ID="..."
- export AMAZON_SECRET_ACCESS_KEY="..."
- END
+ green "Updated #{hosts.size} hosts on #{config_path}"
+ rescue AwsEnvNotDefined, AwsKeyNotFound
+ red "Set aws keys at #{options.dotfile}"
end
desc "remove", "Remove ec2ssh mark from ssh_config"
- method_option *path_option
def remove
- config = Config.new(options.path)
+ config = SshConfig.new(config_path)
unless config.mark_exist?
- red "Marker not found on #{options.path}"
+ red "Marker not found on #{config_path}"
return
end
config.replace! ""
- green "Removed mark from #{options.path}"
+ green "Removed mark from #{config_path}"
end
- private
- def hl
- @hl ||= HighLine.new
- end
+ no_tasks do
+ def hl
+ @hl ||= HighLine.new
+ end
- [:red,:green,:yellow].each do |col|
- no_tasks do
+ def hosts
+ @hosts ||= Hosts.new(dotfile, options.aws_key).all
+ end
+
+ def dotfile
+ @dotfile ||= Dotfile.load(options.dotfile)
+ end
+
+ def config_path
+ options.path || dotfile['path'] || "#{$ENV['HOME']}/.ssh/config"
+ end
+
+ [:red,:green,:yellow].each do |col|
define_method(col) do |str|
puts hl.color(str, col)
end
end
end