| 1 | #!/usr/bin/env ruby |
| 2 | |
| 3 | GROUPS = [ |
| 4 | "generic", |
| 5 | "string", |
| 6 | "list", |
| 7 | "set", |
| 8 | "sorted_set", |
| 9 | "hash", |
| 10 | "pubsub", |
| 11 | "transactions", |
| 12 | "connection", |
| 13 | "server", |
| 14 | "scripting" |
| 15 | ].freeze |
| 16 | |
| 17 | GROUPS_BY_NAME = Hash[* |
| 18 | GROUPS.each_with_index.map do |n,i| |
| 19 | [n,i] |
| 20 | end.flatten |
| 21 | ].freeze |
| 22 | |
| 23 | def argument arg |
| 24 | name = arg["name"].is_a?(Array) ? arg["name"].join(" ") : arg["name"] |
| 25 | name = arg["enum"].join "|" if "enum" == arg["type"] |
| 26 | name = arg["command"] + " " + name if arg["command"] |
| 27 | if arg["multiple"] |
| 28 | name = "#{name} [#{name} ...]" |
| 29 | end |
| 30 | if arg["optional"] |
| 31 | name = "[#{name}]" |
| 32 | end |
| 33 | name |
| 34 | end |
| 35 | |
| 36 | def arguments command |
| 37 | return "-" unless command["arguments"] |
| 38 | command["arguments"].map do |arg| |
| 39 | argument arg |
| 40 | end.join " " |
| 41 | end |
| 42 | |
| 43 | def commands |
| 44 | return @commands if @commands |
| 45 | |
| 46 | require "rubygems" |
| 47 | require "net/http" |
| 48 | require "net/https" |
| 49 | require "json" |
| 50 | require "uri" |
| 51 | |
| 52 | url = URI.parse "https://raw.github.com/antirez/redis-doc/master/commands.json" |
| 53 | client = Net::HTTP.new url.host, url.port |
| 54 | client.use_ssl = true |
| 55 | response = client.get url.path |
| 56 | if response.is_a?(Net::HTTPSuccess) |
| 57 | @commands = JSON.parse(response.body) |
| 58 | else |
| 59 | response.error! |
| 60 | end |
| 61 | end |
| 62 | |
| 63 | def generate_groups |
| 64 | GROUPS.map do |n| |
| 65 | "\"#{n}\"" |
| 66 | end.join(",\n "); |
| 67 | end |
| 68 | |
| 69 | def generate_commands |
| 70 | commands.to_a.sort do |x,y| |
| 71 | x[0] <=> y[0] |
| 72 | end.map do |key, command| |
| 73 | group = GROUPS_BY_NAME[command["group"]] |
| 74 | if group.nil? |
| 75 | STDERR.puts "Please update groups array in #{__FILE__}" |
| 76 | raise "Unknown group #{command["group"]}" |
| 77 | end |
| 78 | |
| 79 | ret = <<-SPEC |
| 80 | { "#{key}", |
| 81 | "#{arguments(command)}", |
| 82 | "#{command["summary"]}", |
| 83 | #{group}, |
| 84 | "#{command["since"]}" } |
| 85 | SPEC |
| 86 | ret.strip |
| 87 | end.join(",\n ") |
| 88 | end |
| 89 | |
| 90 | # Write to stdout |
| 91 | puts <<-HELP_H |
| 92 | /* Automatically generated by #{__FILE__}, do not edit. */ |
| 93 | |
| 94 | #ifndef __REDIS_HELP_H |
| 95 | #define __REDIS_HELP_H |
| 96 | |
| 97 | static char *commandGroups[] = { |
| 98 | #{generate_groups} |
| 99 | }; |
| 100 | |
| 101 | struct commandHelp { |
| 102 | char *name; |
| 103 | char *params; |
| 104 | char *summary; |
| 105 | int group; |
| 106 | char *since; |
| 107 | } commandHelp[] = { |
| 108 | #{generate_commands} |
| 109 | }; |
| 110 | |
| 111 | #endif |
| 112 | HELP_H |
| 113 | |