lib/prize/cli.rb in prize-0.2.1 vs lib/prize/cli.rb in prize-0.3.0
- old
+ new
@@ -1,53 +1,101 @@
-require 'thor'
-require 'redis'
-require 'hiredis'
-require 'pry'
-require 'rainbow'
+require 'optparse'
+require 'ostruct'
module Prize
- class Cli < ::Thor
+ class Cli
+ class << self
+ def start
+ parse_options!
+ App.new(@options).run!
+ end
- default_command :main
+ def parse_options!
+ @options = OpenStruct.new
- desc '', 'Simple Redis CLI client with Pry loaded'
- class_option :url, type: :string, aliases: ['-u'], required: false, desc: 'Server URL, for a TCP connection: `redis://:[password]@[hostname]:[port]/[db]` (password, port and database are optional), for a unix socket connection: `unix://[path to Redis socket]`. This overrides all other options.'
- class_option :host, type: :string, aliases: ['-H'], required: false, desc: 'Server hostname (default: 127.0.0.1)'
- class_option :port, type: :numeric, aliases: ['-p'], required: false, desc: 'Server port (default: 6379)'
- class_option :path, type: :string, aliases: ['-s', '--socket'], required: false, desc: 'Server socket (overrides hostname and port)'
- class_option :timeout, type: :numeric, required: false, desc: 'Timeout in seconds (default: 5.0)'
- class_option :connect_timeout, type: :numeric, required: false, desc: 'Timeout for initial connect in seconds (default: same as timeout)'
- class_option :password, type: :string, aliases: ['-a'], required: false, desc: 'Password to authenticate against server'
- class_option :db, type: :numeric, aliases: ['-n'], required: false, desc: 'Database number (default: 0)'
- class_option :replica, type: :boolean, required: false, desc: 'Whether to use readonly replica nodes in Redis Cluster or not'
- class_option :cluster, type: :string, required: false, desc: 'List of cluster nodes to contact, format: URL1,URL2,URL3...'
- def main
- @redis = Redis.new(build_options)
- Pry.config.prompt = Pry::Prompt.new("", "", prompt)
- if File.exists?(File.expand_path('~/.prizerc'))
- Pry.load_file_at_toplevel(File.expand_path('~/.prizerc'))
- end
- Pry.start(@redis)
- end
- no_commands do
- def build_options
- opts = options.to_h.merge(driver: :hiredis)
- opts['cluster'] = opts['cluster'].split(',') if opts['cluster']
- opts
- end
+ OptionParser.new do |opts|
+ opts.banner = <<~EOF
+ Usage: prize [options]
+ EOF
- def prompt
- opts = @redis.instance_variable_get('@client').options
- host = opts[:url] || opts[:path] || "#{opts[:host]}:#{opts[:port]}/#{opts[:db]}"
- [proc do |obj, nest_level, _|
- if obj == @redis && nest_level == 0
- nest_level_prompt = ''
- else
- nest_level_prompt = "(#{obj}:#{nest_level})"
- end
- "%s#{Rainbow('@').green}%s#{nest_level_prompt} %s " % [Rainbow('PRIZE').red, Rainbow(host).yellow, Rainbow('❯').green]
- end]
+ opts.on('-uURL', '--url=URL', 'Server URL, for a TCP connection: `redis://:[password]@[hostname]:[port]/[db]` (password, port and database are optional), for a unix socket connection: `unix://[path to Redis socket]`. This overrides all other options.') do |url|
+ @options.url = url
+ end
+
+ opts.on('-hHOST', '--host=HOST', 'Server hostname (default: 127.0.0.1)') do |host|
+ @options.host = host
+ end
+
+ opts.on('-pPORT', '--port=PORT', 'Server port (default: 6379)') do |port|
+ @options.port = port.to_i
+ end
+
+ opts.on('-sPATH', '--sock=PATH', 'Server socket (overrides hostname and port)') do |path|
+ @options.path = path
+ end
+
+ opts.on('-dDB', '--db=DB', 'Specify database') do |db|
+ @options.db = db
+ end
+
+ opts.on('-PPASSWORD', '--password=PASSWORD', 'Specify password') do |password|
+ @options.password = password
+ end
+
+ opts.on('', '--timeout=TIMEOUT', 'Timeout in seconds (default: 5.0)') do |timeout|
+ @options.timeout = timeout.to_i
+ end
+
+ opts.on('', '--connect-timeout=TIMEOUT', 'Timeout for initial connect in seconds (default: same as timeout)') do |timeout|
+ @options.connect_timeout = timeout.to_i
+ end
+
+ opts.on('', '--replica', 'Whether to use readonly replica nodes in Redis Cluster or not') do
+ @options.replica = true
+ end
+
+ opts.on('', '--cluster=CLUSTER_URL', 'List of cluster nodes to contact, format: URL1,URL2,URL3...') do |cluster|
+ @options.cluster = cluster.split(',')
+ end
+
+ opts.on('-HSSH_HOST', '--ssh-host=SSH_HOST', 'Specify SSH host') do |ssh_host|
+ @options.ssh_host = ssh_host
+ end
+
+ opts.on('-OSSH_PORT', '--ssh-port=SSH_PORT', 'Specify SSH port') do |ssh_port|
+ @options.ssh_port = ssh_port.to_i
+ end
+
+ opts.on('-USSH_USER', '--ssh-user=SSH_USER', 'Specify SSH user') do |ssh_user|
+ @options.ssh_user = ssh_user
+ end
+
+ opts.on('-WSSH_PASSWORD', '--ssh-password=SSH_PASSWORD', 'Specify SSH password') do |ssh_password|
+ @options.ssh_password = ssh_password
+ end
+
+ opts.on('-LSSH_LOCAL_PORT', '--ssh-local-port=SSH_LOCAL_PORT', 'Specify local SSH proxy port') do |local_port|
+ @options.ssh_local_port = local_port.to_i
+ end
+
+ opts.on('-ECODE', '--eval=CODE', 'evaluate CODE') do |code|
+ @options.code = code
+ end
+
+ opts.on('-V', '--version', 'Prints version') do
+ puts "PRIZE #{Prize::VERSION}"
+ exit
+ end
+
+ opts.on('', '--help', 'Prints this help') do
+ puts opts
+ exit
+ end
+
+ end.parse!
+
+ @options.args = ARGV
end
end
end
end