composer-cli: Add providers template command

This outputs a TOML template of the settings needed for setting the
upload credentials. It can be passed to 'upload start' and to 'compose
start', as well as used to set the profile for 'providers push'
This commit is contained in:
Brian C. Lane 2019-09-25 14:15:27 -07:00
parent 1969711d8c
commit 6a2f465217
2 changed files with 60 additions and 4 deletions

View File

@ -9,7 +9,7 @@ declare -A __composer_cli_cmds=(
[projects]="list info"
[sources]="list info add change delete"
[upload]="info start log cancel delete reset"
[providers]="list show push save delete"
[providers]="list show push save delete template"
[help]=""
)
@ -96,7 +96,7 @@ _composer_cli() {
compose:list)
COMPREPLY=($(compgen -W "waiting running finish failed" -- "${cur}"))
;;
providers:list)
providers:list|providers:template)
COMPREPLY=($(compgen -W "$(__composer_provider_list)" -- "${cur}"))
;;
*:list|*:help|compose:types)
@ -146,6 +146,20 @@ _composer_cli() {
compose:start)
if [ "$cmd_cword" == 3 ]; then
COMPREPLY=($(compgen -W "$(__composer_compose_types)" -- "${cur}"))
elif [ "$cmd_cword" == 5 ]; then
# If they have typed something looking like a path, use file completion
# otherwise suggest providers.
case "${cur}" in
*/*)
compopt -o filenames
COMPREPLY=($(compgen -f -- "${cur}"))
;;
*)
COMPREPLY=($(compgen -W "$(__composer_provider_list)" -- "${cur}"))
;;
esac
elif [ "$cmd_cword" == 6 ]; then
COMPREPLY=($(compgen -W "$(__composer_profile_list ${prev})" -- "${cur}"))
fi
;;
# TODO: blueprints:diff and blueprints:undo want commits
@ -157,7 +171,7 @@ _composer_cli() {
;;
upload:start)
if [ "$cmd_cword" == 4 ]; then
# If they have types something looking like a path, use file completion
# If they have typed something looking like a path, use file completion
# otherwise suggest providers.
case "${cur}" in
*/*)

View File

@ -40,7 +40,8 @@ def providers_cmd(opts):
"show": providers_show,
"push": providers_push,
"save": providers_save,
"delete": providers_delete
"delete": providers_delete,
"template": providers_template
}
if opts.args[1] == "help" or opts.args[1] == "--help":
print(providers_help)
@ -234,3 +235,44 @@ def providers_delete(socket_path, api_version, args, show_json=False, testmode=0
api_route = client.api_url(api_version, "/upload/providers/delete/%s/%s" % (args[0], args[1]))
result = client.delete_url_json(socket_path, api_route)
return handle_api_result(result, show_json)[0]
def providers_template(socket_path, api_version, args, show_json=False, testmode=0):
"""Return a TOML template for setting the provider's fields
:param socket_path: Path to the Unix socket to use for API communication
:type socket_path: str
:param api_version: Version of the API to talk to. eg. "0"
:type api_version: str
:param args: List of remaining arguments from the cmdline
:type args: list of str
:param show_json: Set to True to show the JSON output instead of the human readable output
:type show_json: bool
:param testmode: unused in this function
:type testmode: int
providers template <provider>
"""
if len(args) == 0:
log.error("template is missing the provider name")
return 1
api_route = client.api_url(api_version, "/upload/providers")
r = client.get_url_json(socket_path, api_route)
results = r["providers"]
if not results:
return 0
if show_json:
print(json.dumps(results, indent=4))
return 0
if args[0] not in results:
log.error("%s is not a valid provider", args[0])
return 1
template = {"provider": args[0]}
settings = results[args[0]]["settings-info"]
template["settings"] = dict([(k, settings[k]["display"]) for k in settings])
print(toml.dumps(template))
return 0