Sha256: e99004f1e1da36d0b8db6136ff659d029bb2dbf3a37c96766def0a5fb3e175ae

Contents?: true

Size: 1.76 KB

Versions: 2

Compression:

Stored size: 1.76 KB

Contents

require 'thor'
require 'right_aws'
require 'highline'

module Ec2ssh
  class CLI < Thor
    path_option = [:path, {:banner => "/path/to/ssh_config", :default=>"#{ENV['HOME']}/.ssh/config"}]

    desc "init", "Add ec2ssh mark to ssh_config"
    method_option *path_option
    def init
      config = Config.new(options.path)
      if config.mark_exist?
        red "Marker already exists on #{options.path}"
        return
      end
      config.append_mark!
      green "Added mark to #{options.path}"
    end

    desc "update", "Update ec2 hosts list in ssh_config"
    method_option *path_option
    def update
      config = Config.new(options.path)
      unless config.mark_exist?
        red "Marker not found on #{options.path}"
        red "Execute '#{$0} init --path=#{options.path}' 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
    end

    desc "remove", "Remove ec2ssh mark from ssh_config"
    method_option *path_option
    def remove
      config = Config.new(options.path)
      unless config.mark_exist?
        red "Marker not found on #{options.path}"
        return
      end
      config.replace! ""
      green "Removed mark from #{options.path}"
    end

    private
    def hl
      @hl ||= HighLine.new
    end

    [:red,:green,:yellow].each do |col|
      define_method(col) do |str|
        puts hl.color(str, col)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ec2ssh-1.0.2 lib/ec2ssh/cli.rb
ec2ssh-1.0.1 lib/ec2ssh/cli.rb