2016-07-29 00:46:37 +08:00
|
|
|
## The contents of this file are subject to the Mozilla Public License
|
|
|
|
## Version 1.1 (the "License"); you may not use this file except in
|
|
|
|
## compliance with the License. You may obtain a copy of the License
|
|
|
|
## at http://www.mozilla.org/MPL/
|
|
|
|
##
|
|
|
|
## Software distributed under the License is distributed on an "AS IS"
|
|
|
|
## basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
|
|
|
|
## the License for the specific language governing rights and
|
|
|
|
## limitations under the License.
|
|
|
|
##
|
|
|
|
## The Original Code is RabbitMQ.
|
|
|
|
##
|
|
|
|
## The Initial Developer of the Original Code is GoPivotal, Inc.
|
2017-01-10 22:42:40 +08:00
|
|
|
## Copyright (c) 2007-2017 Pivotal Software, Inc. All rights reserved.
|
2016-07-29 00:46:37 +08:00
|
|
|
|
|
|
|
|
2016-11-15 01:52:08 +08:00
|
|
|
defmodule Rabbitmq.CLI.AutoComplete do
|
|
|
|
alias RabbitMQ.CLI.Core.Parser, as: Parser
|
|
|
|
alias RabbitMQ.CLI.Core.CommandModules, as: CommandModules
|
2016-07-29 00:46:37 +08:00
|
|
|
|
2017-01-17 01:56:17 +08:00
|
|
|
@spec complete(String.t) :: [String.t]
|
2017-01-24 20:30:12 +08:00
|
|
|
def complete(script_name, str) do
|
2016-07-29 00:46:37 +08:00
|
|
|
tokens = String.split(str, " ", trim: true)
|
|
|
|
case List.last(tokens) do
|
2017-01-24 20:30:12 +08:00
|
|
|
nil -> [];
|
|
|
|
_last ->
|
|
|
|
case Parser.parse_global(tokens) do
|
|
|
|
%{script_name: _args_script_name} ->
|
|
|
|
complete(tokens);
|
|
|
|
_ ->
|
|
|
|
complete(["--script-name", script_name | tokens])
|
2016-07-29 00:46:37 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-24 20:30:12 +08:00
|
|
|
defp complete(tokens) do
|
|
|
|
{command, command_name, _, _, _} = Parser.parse(tokens)
|
|
|
|
last_token = List.last(tokens)
|
|
|
|
case {command, command_name} do
|
|
|
|
## No command provided
|
|
|
|
{_, ""} -> complete_default_opts(last_token);
|
|
|
|
## Command is not found/incomplete
|
|
|
|
{:no_command, command_name} ->
|
|
|
|
complete_command_name(command_name);
|
|
|
|
{{:suggest, _}, command_name} ->
|
|
|
|
complete_command_name(command_name);
|
|
|
|
## Command is found
|
|
|
|
{command, _} ->
|
|
|
|
complete_command_opts(command, last_token)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp complete_default_opts(opt) do
|
2016-07-29 00:46:37 +08:00
|
|
|
Parser.default_switches
|
|
|
|
|> Keyword.keys
|
|
|
|
|> Enum.map(fn(sw) -> "--" <> to_string(sw) end)
|
|
|
|
|> select_starts_with(opt)
|
2017-01-17 01:56:17 +08:00
|
|
|
|> format_options
|
2016-07-29 00:46:37 +08:00
|
|
|
end
|
|
|
|
|
2017-01-24 20:30:12 +08:00
|
|
|
defp complete_command_name(command_name) do
|
2017-01-17 01:56:17 +08:00
|
|
|
module_map = CommandModules.module_map
|
|
|
|
case module_map[command_name] do
|
|
|
|
nil ->
|
|
|
|
module_map
|
2016-07-29 00:46:37 +08:00
|
|
|
|> Map.keys
|
2017-01-17 01:56:17 +08:00
|
|
|
|> select_starts_with(command_name)
|
|
|
|
_ ->
|
|
|
|
command_name
|
2016-07-29 00:46:37 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-24 20:30:12 +08:00
|
|
|
defp complete_command_opts(command, <<"-", _ :: binary>> = opt) do
|
2016-07-29 00:46:37 +08:00
|
|
|
switches = command.switches
|
|
|
|
|> Keyword.keys
|
|
|
|
|> Enum.map(fn(sw) -> "--" <> to_string(sw) end)
|
|
|
|
|
|
|
|
# aliases = command.aliases
|
|
|
|
# |> Keyword.keys
|
|
|
|
# |> Enum.map(fn(al) -> "-" <> to_string(al) end)
|
|
|
|
select_starts_with(switches, opt)
|
2017-01-17 01:56:17 +08:00
|
|
|
|> format_options
|
2016-07-29 00:46:37 +08:00
|
|
|
end
|
2017-01-24 20:30:12 +08:00
|
|
|
defp complete_command_opts(_, _) do
|
2016-07-29 00:46:37 +08:00
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
|
|
|
defp select_starts_with(list, prefix) do
|
|
|
|
Enum.filter(list, fn(el) -> String.starts_with?(el, prefix) end)
|
|
|
|
end
|
2017-01-17 01:56:17 +08:00
|
|
|
|
|
|
|
defp format_options(options) do
|
|
|
|
options
|
|
|
|
|> Enum.map(fn(opt) -> String.replace(opt, "_", "-") end)
|
|
|
|
end
|
2017-01-10 22:42:40 +08:00
|
|
|
end
|