Skip to content

Instantly share code, notes, and snippets.

@cadwallion
Created November 28, 2011 15:55
Show Gist options
  • Save cadwallion/1400870 to your computer and use it in GitHub Desktop.
Save cadwallion/1400870 to your computer and use it in GitHub Desktop.
portal commandline script
1.9.3-p0 in portal/
› ./bin/portal
Manages SSH portals as easy hosts
usage: portal command [command options]
Version: 0.0.1
Commands:
add - Adds a portal to the list
connect - Connect to an SSH portal by name
help - Shows list of commands or help for one command
list - List all portals
rm - Remove a portal for a given name
#!/usr/bin/env ruby
# 1.9 adds realpath to resolve symlinks; 1.8 doesn't
# have this method, so we add it so we get resolved symlinks
# and compatibility
unless File.respond_to? :realpath
class File #:nodoc:
def self.realpath path
return realpath(File.readlink(path)) if symlink?(path)
path
end
end
end
$: << File.expand_path(File.dirname(File.realpath(__FILE__)) + '/../lib')
require 'rubygems'
require 'gli'
require 'portal_version'
include GLI
program_desc 'Manages SSH portals as easy hosts'
version Portal::VERSION
desc 'Adds a portal to the list'
command :add do |c|
c.desc 'user to connect as. default: current user'
c.default_value `whoami`
c.flag [:u, :user]
c.desc 'port to connect to. default: 22'
c.default_value '22'
c.flag [:p, :port]
c.action do |global_options,options,args|
if args.length < 1
raise 'You must specify the name of the portal to add'
else
Portal.add(args.first, options)
end
end
end
desc 'Remove a portal for a given name'
command :rm do |c|
c.action do |global_options,options,args|
if args.length < 1
raise 'You must specify the name of the portal to remove.'
else
Portal.remove(args.first)
end
end
end
desc 'List all portals'
command :list do |c|
c.action do |global_options,options,args|
Portal.list
end
end
desc 'Connect to an SSH portal by name'
arg_name 'NAME'
command :connect do |c|
c.desc 'connect NAME'
c.action do |global_options,options,args|
if args.length < 1
raise 'You must specify the name of the portal to connect to.'
else
Portal.connect(args.first)
end
end
end
on_error do |exception|
# Error logic here
# return false to skip default error handling
true
end
exit GLI.run(ARGV)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment