1#!/usr/bin/env ruby 2 3def colorized(str, color) 4 return str if !(ENV['TERM'] || '')["xterm"] 5 color_code = { 6 white: 29, 7 bold: '29;1', 8 black: 30, 9 red: 31, 10 green: 32, 11 yellow: 33, 12 blue: 34, 13 magenta: 35, 14 cyan: 36, 15 gray: 37 16 }[color] 17 return str if !color_code 18 "\033[#{color_code}m#{str}\033[0m" 19end 20 21class String 22 23 %w(white bold black red green yellow blue magenta cyan gray).each{|color| 24 color = :"#{color}" 25 define_method(color){ 26 colorized(self, color) 27 } 28 } 29 30end 31 32COMMANDS = %w(create check info fix reshard rebalance add-node 33 del-node set-timeout call import help) 34 35ALLOWED_OPTIONS={ 36 "create" => {"replicas" => true}, 37 "add-node" => {"slave" => false, "master-id" => true}, 38 "import" => {"from" => :required, "copy" => false, "replace" => false}, 39 "reshard" => {"from" => true, "to" => true, "slots" => true, "yes" => false, "timeout" => true, "pipeline" => true}, 40 "rebalance" => {"weight" => [], "auto-weights" => false, "use-empty-masters" => false, "timeout" => true, "simulate" => false, "pipeline" => true, "threshold" => true}, 41 "fix" => {"timeout" => 0}, 42} 43 44def parse_options(cmd) 45 cmd = cmd.downcase 46 idx = 0 47 options = {} 48 args = [] 49 while (arg = ARGV.shift) 50 if arg[0..1] == "--" 51 option = arg[2..-1] 52 53 # --verbose is a global option 54 if option == "--verbose" 55 options['verbose'] = true 56 next 57 end 58 if ALLOWED_OPTIONS[cmd] == nil || 59 ALLOWED_OPTIONS[cmd][option] == nil 60 next 61 end 62 if ALLOWED_OPTIONS[cmd][option] != false 63 value = ARGV.shift 64 next if !value 65 else 66 value = true 67 end 68 69 # If the option is set to [], it's a multiple arguments 70 # option. We just queue every new value into an array. 71 if ALLOWED_OPTIONS[cmd][option] == [] 72 options[option] = [] if !options[option] 73 options[option] << value 74 else 75 options[option] = value 76 end 77 else 78 next if arg[0,1] == '-' 79 args << arg 80 end 81 end 82 83 return options,args 84end 85 86def command_example(cmd, args, opts) 87 cmd = "redis-cli --cluster #{cmd}" 88 args.each{|a| 89 a = a.to_s 90 a = a.inspect if a[' '] 91 cmd << " #{a}" 92 } 93 opts.each{|opt, val| 94 opt = " --cluster-#{opt.downcase}" 95 if val != true 96 val = val.join(' ') if val.is_a? Array 97 opt << " #{val}" 98 end 99 cmd << opt 100 } 101 cmd 102end 103 104$command = ARGV.shift 105$opts, $args = parse_options($command) if $command 106 107puts "WARNING: redis-trib.rb is not longer available!".yellow 108puts "You should use #{'redis-cli'.bold} instead." 109puts '' 110puts "All commands and features belonging to redis-trib.rb "+ 111 "have been moved\nto redis-cli." 112puts "In order to use them you should call redis-cli with the #{'--cluster'.bold}" 113puts "option followed by the subcommand name, arguments and options." 114puts '' 115puts "Use the following syntax:" 116puts "redis-cli --cluster SUBCOMMAND [ARGUMENTS] [OPTIONS]".bold 117puts '' 118puts "Example:" 119if $command 120 example = command_example $command, $args, $opts 121else 122 example = "redis-cli --cluster info 127.0.0.1:7000" 123end 124puts example.bold 125puts '' 126puts "To get help about all subcommands, type:" 127puts "redis-cli --cluster help".bold 128puts '' 129exit 1 130