| 
									
										
										
										
											2022-04-06 14:33:33 +08:00
										 |  |  | #!/usr/bin/env ruby -w | 
					
						
							|  |  |  | # Usage: generate-command-help.r [path/to/commands.json] | 
					
						
							|  |  |  | #    or: generate-commands-json.py | generate-command-help.rb - | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Defaults to downloading commands.json from the redis-doc repo if not provided | 
					
						
							|  |  |  | # or STDINed. | 
					
						
							| 
									
										
										
										
											2010-11-16 21:50:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-29 04:37:19 +08:00
										 |  |  | GROUPS = [ | 
					
						
							|  |  |  |   "generic", | 
					
						
							|  |  |  |   "string", | 
					
						
							|  |  |  |   "list", | 
					
						
							|  |  |  |   "set", | 
					
						
							| 
									
										
										
										
											2022-01-28 03:48:06 +08:00
										 |  |  |   "sorted-set", | 
					
						
							| 
									
										
										
										
											2010-11-29 04:37:19 +08:00
										 |  |  |   "hash", | 
					
						
							|  |  |  |   "pubsub", | 
					
						
							|  |  |  |   "transactions", | 
					
						
							|  |  |  |   "connection", | 
					
						
							| 
									
										
										
										
											2012-04-03 21:29:47 +08:00
										 |  |  |   "server", | 
					
						
							| 
									
										
										
										
											2014-04-22 22:13:58 +08:00
										 |  |  |   "scripting", | 
					
						
							| 
									
										
										
										
											2015-11-17 22:38:34 +08:00
										 |  |  |   "hyperloglog", | 
					
						
							|  |  |  |   "cluster", | 
					
						
							| 
									
										
										
										
											2018-06-08 00:52:01 +08:00
										 |  |  |   "geo", | 
					
						
							| 
									
										
										
										
											2021-09-09 15:16:30 +08:00
										 |  |  |   "stream", | 
					
						
							|  |  |  |   "bitmap" | 
					
						
							| 
									
										
										
										
											2010-11-29 04:37:19 +08:00
										 |  |  | ].freeze | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GROUPS_BY_NAME = Hash[* | 
					
						
							|  |  |  |   GROUPS.each_with_index.map do |n,i| | 
					
						
							|  |  |  |     [n,i] | 
					
						
							|  |  |  |   end.flatten | 
					
						
							|  |  |  | ].freeze | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-16 21:50:26 +08:00
										 |  |  | def argument arg | 
					
						
							| 
									
										
										
										
											2020-11-30 21:00:16 +08:00
										 |  |  |   if "block" == arg["type"] | 
					
						
							| 
									
										
										
										
											2022-01-28 03:48:06 +08:00
										 |  |  |     name = arg["arguments"].map do |entry| | 
					
						
							| 
									
										
										
										
											2020-11-30 21:00:16 +08:00
										 |  |  |       argument entry | 
					
						
							|  |  |  |     end.join " " | 
					
						
							| 
									
										
										
										
											2022-01-28 03:48:06 +08:00
										 |  |  |   elsif "oneof" == arg["type"] | 
					
						
							|  |  |  |     name = arg["arguments"].map do |entry| | 
					
						
							|  |  |  |       argument entry | 
					
						
							|  |  |  |     end.join "|" | 
					
						
							|  |  |  |   elsif "pure-token" == arg["type"] | 
					
						
							|  |  |  |     name = nil    # prepended later | 
					
						
							| 
									
										
										
										
											2020-11-30 21:00:16 +08:00
										 |  |  |   else | 
					
						
							|  |  |  |     name = arg["name"].is_a?(Array) ? arg["name"].join(" ") : arg["name"] | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2010-11-29 00:45:58 +08:00
										 |  |  |   if arg["multiple"] | 
					
						
							| 
									
										
										
										
											2022-06-07 15:15:39 +08:00
										 |  |  |     if arg["multiple_token"] | 
					
						
							|  |  |  |       name = "#{name} [#{arg["token"]} #{name} ...]" | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       name = "#{name} [#{name} ...]" | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2010-11-29 00:45:58 +08:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2022-01-28 03:48:06 +08:00
										 |  |  |   if arg["token"] | 
					
						
							|  |  |  |     name = [arg["token"], name].compact.join " " | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2010-11-29 00:45:58 +08:00
										 |  |  |   if arg["optional"] | 
					
						
							|  |  |  |     name = "[#{name}]" | 
					
						
							| 
									
										
										
										
											2010-11-16 21:50:26 +08:00
										 |  |  |   end | 
					
						
							|  |  |  |   name | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def arguments command | 
					
						
							| 
									
										
										
										
											2021-05-18 22:13:10 +08:00
										 |  |  |   return "" unless command["arguments"] | 
					
						
							| 
									
										
										
										
											2010-11-29 00:45:58 +08:00
										 |  |  |   command["arguments"].map do |arg| | 
					
						
							| 
									
										
										
										
											2010-11-16 21:50:26 +08:00
										 |  |  |     argument arg | 
					
						
							| 
									
										
										
										
											2010-11-29 00:45:58 +08:00
										 |  |  |   end.join " " | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def commands | 
					
						
							|  |  |  |   return @commands if @commands | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-16 00:39:40 +08:00
										 |  |  |   require "rubygems" | 
					
						
							| 
									
										
										
										
											2010-11-29 00:45:58 +08:00
										 |  |  |   require "net/http" | 
					
						
							|  |  |  |   require "net/https" | 
					
						
							|  |  |  |   require "json" | 
					
						
							|  |  |  |   require "uri" | 
					
						
							| 
									
										
										
										
											2022-04-06 14:33:33 +08:00
										 |  |  |   if ARGV.length > 0
 | 
					
						
							|  |  |  |     if ARGV[0] == '-' | 
					
						
							|  |  |  |       data = STDIN.read | 
					
						
							|  |  |  |     elsif FileTest.exist? ARGV[0] | 
					
						
							|  |  |  |       data = File.read(ARGV[0]) | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       raise Exception.new "File not found: #{ARGV[0]}" | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2010-11-29 00:45:58 +08:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2022-04-06 14:33:33 +08:00
										 |  |  |     url = URI.parse "https://raw.githubusercontent.com/redis/redis-doc/master/commands.json" | 
					
						
							|  |  |  |     client = Net::HTTP.new url.host, url.port | 
					
						
							|  |  |  |     client.use_ssl = true | 
					
						
							|  |  |  |     response = client.get url.path | 
					
						
							|  |  |  |     if !response.is_a?(Net::HTTPSuccess) | 
					
						
							|  |  |  |       response.error! | 
					
						
							|  |  |  |       return | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       data = response.body | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2010-11-29 00:45:58 +08:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2022-04-06 14:33:33 +08:00
										 |  |  |   @commands = JSON.parse(data) | 
					
						
							| 
									
										
										
										
											2010-11-16 21:50:26 +08:00
										 |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-29 04:37:19 +08:00
										 |  |  | def generate_groups | 
					
						
							|  |  |  |   GROUPS.map do |n| | 
					
						
							|  |  |  |     "\"#{n}\"" | 
					
						
							|  |  |  |   end.join(",\n    "); | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-29 00:45:58 +08:00
										 |  |  | def generate_commands | 
					
						
							|  |  |  |   commands.to_a.sort do |x,y| | 
					
						
							|  |  |  |     x[0] <=> y[0] | 
					
						
							|  |  |  |   end.map do |key, command| | 
					
						
							| 
									
										
										
										
											2010-11-29 04:37:19 +08:00
										 |  |  |     group = GROUPS_BY_NAME[command["group"]] | 
					
						
							|  |  |  |     if group.nil? | 
					
						
							|  |  |  |       STDERR.puts "Please update groups array in #{__FILE__}" | 
					
						
							|  |  |  |       raise "Unknown group #{command["group"]}" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ret = <<-SPEC
 | 
					
						
							| 
									
										
										
										
											2010-11-29 00:45:58 +08:00
										 |  |  | { "#{key}", | 
					
						
							|  |  |  |     "#{arguments(command)}", | 
					
						
							|  |  |  |     "#{command["summary"]}", | 
					
						
							| 
									
										
										
										
											2010-11-29 04:37:19 +08:00
										 |  |  |     #{group}, | 
					
						
							| 
									
										
										
										
											2010-11-29 00:45:58 +08:00
										 |  |  |     "#{command["since"]}" } | 
					
						
							|  |  |  |     SPEC | 
					
						
							| 
									
										
										
										
											2010-11-29 04:37:19 +08:00
										 |  |  |     ret.strip | 
					
						
							|  |  |  |   end.join(",\n    ") | 
					
						
							| 
									
										
										
										
											2010-11-29 00:45:58 +08:00
										 |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Write to stdout | 
					
						
							| 
									
										
										
										
											2010-11-29 04:37:19 +08:00
										 |  |  | puts <<-HELP_H
 | 
					
						
							|  |  |  | /* Automatically generated by #{__FILE__}, do not edit. */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef __REDIS_HELP_H | 
					
						
							|  |  |  | #define __REDIS_HELP_H | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char *commandGroups[] = { | 
					
						
							|  |  |  |     #{generate_groups} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct commandHelp { | 
					
						
							|  |  |  |   char *name; | 
					
						
							|  |  |  |   char *params; | 
					
						
							|  |  |  |   char *summary; | 
					
						
							|  |  |  |   int group; | 
					
						
							|  |  |  |   char *since; | 
					
						
							|  |  |  | } commandHelp[] = { | 
					
						
							|  |  |  |     #{generate_commands} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif | 
					
						
							|  |  |  | HELP_H | 
					
						
							| 
									
										
										
										
											2010-11-29 00:45:58 +08:00
										 |  |  | 
 |