Skip to content

Instantly share code, notes, and snippets.

@ichiban
Created February 15, 2012 03:50
Show Gist options
  • Save ichiban/1833038 to your computer and use it in GitHub Desktop.
Save ichiban/1833038 to your computer and use it in GitHub Desktop.
こんなので.ssh/config切り替えると便利な気がする
#!/usr/bin/env ruby
require 'thor'
class SSHEnv < Thor
SSH_DIR = "~/.ssh"
desc 'use PRESET', 'Switch SSH config preset to PRESET'
method_option :dry, :type => :boolean, :aliases => '-d'
def use(name="default")
@dry = options.dry
sh "rm #{config}" if File.exists? File.expand_path(config)
sh "touch #{config_preset name}" unless File.exists? File.expand_path("#{config_preset name}")
sh "ln -s #{config_preset name} #{config}"
end
desc 'preset', 'Show current preset'
def preset
puts current_preset_name
end
desc 'presets', 'List presets'
def presets
current = current_preset_name
preset_names.each do |line|
puts "#{current == line ? '* ' : ' '}#{line}"
end
end
no_tasks do
def method_missing(method_name)
use method_name
end
def exists?(file)
File.exists? File.expand_path(file)
end
def sh(cmd)
if @dry
puts cmd
else
system cmd
end
end
def config
"#{SSH_DIR}/config"
end
def config_preset(name)
"#{SSH_DIR}/config.#{name}"
end
def current_preset_name
return nil unless File.exists? File.expand_path(config)
`ls -l #{config}`.gsub(/.*\.([^.]+)$/, '\1')
end
def preset_names
`ls -l #{config_preset '*'}`.gsub(/.*\/config\.([^.]+)$/, '\1').lines.to_a
end
end
end
SSHEnv.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment