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