|
| 1 | +from typing import Optional |
| 2 | + |
| 3 | +import click |
| 4 | + |
| 5 | +import commodore.cli.options as options |
| 6 | + |
| 7 | +from commodore import tools |
| 8 | +from commodore.config import Config |
| 9 | + |
| 10 | + |
| 11 | +@click.group( |
| 12 | + name="tool", |
| 13 | + short_help="Manage required external tools", |
| 14 | +) |
| 15 | +@options.verbosity |
| 16 | +@options.pass_config |
| 17 | +@options.github_token |
| 18 | +def tool_group(config: Config, verbose: int, github_token: str): |
| 19 | + """Commands to manage required external tools. |
| 20 | +
|
| 21 | + Currently, `helm`, jsonnet-bundler/`jb`, and `kustomize` are required |
| 22 | + external tools. |
| 23 | + """ |
| 24 | + config.update_verbosity(verbose) |
| 25 | + config.managed_tools = tools.load_state() |
| 26 | + config.github_token = github_token |
| 27 | + |
| 28 | + |
| 29 | +@tool_group.command(name="list", short_help="List external tools") |
| 30 | +@options.verbosity |
| 31 | +@options.pass_config |
| 32 | +@options.github_token |
| 33 | +@click.option( |
| 34 | + "--version-check/--skip-version-check", |
| 35 | + " / -V", |
| 36 | + default=True, |
| 37 | + help="Query GitHub API to get latest version", |
| 38 | +) |
| 39 | +def tool_list(config: Config, verbose: int, github_token: str, version_check: bool): |
| 40 | + """List the version, location and management state of the required external tools. |
| 41 | +
|
| 42 | + Currently, `helm`, jsonnet-bundler/`jb`, and `kustomize` are required |
| 43 | + external tools. By default, the command also queries the GitHub API to |
| 44 | + determine the latest available version of each tool and indicates whether an |
| 45 | + update is available. |
| 46 | +
|
| 47 | + Optionally, the command accepts a GitHub personal access token (PAT) to |
| 48 | + avoid running into the fairly strict unauthenticated GitHub rate limits. |
| 49 | + """ |
| 50 | + config.update_verbosity(verbose) |
| 51 | + config.github_token = github_token |
| 52 | + tools.list_tools(config, version_check) |
| 53 | + |
| 54 | + |
| 55 | +@tool_group.command(name="install", short_help="Install external tools") |
| 56 | +@options.verbosity |
| 57 | +@options.pass_config |
| 58 | +@options.github_token |
| 59 | +@click.option( |
| 60 | + "--version", |
| 61 | + default=None, |
| 62 | + metavar="VERSION", |
| 63 | + help="A version to install for the requested tool. " |
| 64 | + + "By default, the latest version is installed.", |
| 65 | +) |
| 66 | +@click.option( |
| 67 | + "--missing", |
| 68 | + is_flag=True, |
| 69 | + default=False, |
| 70 | + help="Install the latest version for all currently unmanaged tools. " |
| 71 | + + "This flag and providing a tool name on the command line are mutually exclusive.", |
| 72 | +) |
| 73 | +@click.argument("tool", required=False, default="") |
| 74 | +def tool_install( |
| 75 | + config: Config, |
| 76 | + verbose: int, |
| 77 | + tool: str, |
| 78 | + version: Optional[str], |
| 79 | + github_token: str, |
| 80 | + missing: bool, |
| 81 | +): |
| 82 | + """Install one of the required tools in `$XDG_CACHE_DIR/commodore/tools`. |
| 83 | +
|
| 84 | + The command will fail for tools which are already managed by Commodore. |
| 85 | +
|
| 86 | + By default, the command will install the latest available tool version. For |
| 87 | + `helm` and `kustomize`, the command downloads the official installation |
| 88 | + scripts and executes them with appropriate arguments. For `jb`, the command |
| 89 | + directly downloads the requested version from the GitHub release page. |
| 90 | +
|
| 91 | + Optionally, the command accepts a tool version to install. The command |
| 92 | + accepts versions prefixed with "v" and unprefixed versions. |
| 93 | +
|
| 94 | + Alternatively, the command can install the latest version for all required |
| 95 | + tools which aren't managed by Commodore yet by passing flag `--missing`. |
| 96 | + This flag is mutually exclusive with providing a tool name. |
| 97 | + """ |
| 98 | + config.update_verbosity(verbose) |
| 99 | + config.github_token = github_token |
| 100 | + |
| 101 | + if not tool and not missing or tool and missing: |
| 102 | + raise click.ClickException( |
| 103 | + "`commodore tool install` expects to be called with either a tool name or the `--missing` flag." |
| 104 | + ) |
| 105 | + if missing and version: |
| 106 | + click.secho( |
| 107 | + "Flag `--version` has no effect when calling the command with `--missing`.", |
| 108 | + fg="yellow", |
| 109 | + ) |
| 110 | + |
| 111 | + if missing: |
| 112 | + tools.install_missing_tools(config) |
| 113 | + else: |
| 114 | + tools.install_tool(config, tool, version) |
| 115 | + |
| 116 | + |
| 117 | +@tool_group.command(name="upgrade", short_help="Upgrade external tools") |
| 118 | +@options.verbosity |
| 119 | +@options.github_token |
| 120 | +@options.pass_config |
| 121 | +@click.option( |
| 122 | + "--version", |
| 123 | + default=None, |
| 124 | + metavar="VERSION", |
| 125 | + help="A version to upgrade (or downgrade) to for the requested tool. " |
| 126 | + + "By default, the tool is upgraded to the latest version.", |
| 127 | +) |
| 128 | +@click.option( |
| 129 | + "--all", |
| 130 | + is_flag=True, |
| 131 | + default=False, |
| 132 | + help="Upgrade all currently managed tools to their latest versions. " |
| 133 | + + "This flag and providing a tool name on the command line are mutually exclusive.", |
| 134 | +) |
| 135 | +@click.argument("tool", required=False, default="") |
| 136 | +def tool_upgrade( |
| 137 | + config: Config, |
| 138 | + verbose: int, |
| 139 | + tool: str, |
| 140 | + version: Optional[str], |
| 141 | + github_token: str, |
| 142 | + all: bool, |
| 143 | +): |
| 144 | + """Upgrade (or downgrade) one of the required tools in `$XDG_CACHE_DIR/commodore/tools`. |
| 145 | +
|
| 146 | + The command will fail for tools which aren't managed by Commodore yet. |
| 147 | +
|
| 148 | + By default, the command will upgrade the tool to the latest available |
| 149 | + version. For `helm` and `kustomize`, the command downloads the official |
| 150 | + installation scripts and executes them with appropriate arguments. For `jb`, |
| 151 | + the command directly downloads the requested version from the GitHub release |
| 152 | + page. |
| 153 | +
|
| 154 | + Optionally, the command accepts a tool version to upgrade (or downgrade) to. |
| 155 | + The command accepts versions prefixed with "v" and unprefixed versions. |
| 156 | +
|
| 157 | + Alternatively, the command can upgrade all managed tools to their latest |
| 158 | + versions by passing flag `--all`. This flag is mutually exclusive with |
| 159 | + providing a tool name. |
| 160 | + """ |
| 161 | + config.update_verbosity(verbose) |
| 162 | + config.github_token = github_token |
| 163 | + |
| 164 | + if not tool and not all or tool and all: |
| 165 | + raise click.ClickException( |
| 166 | + "`commodore tool upgrade` expects to be called with either a tool name or the `--all` flag." |
| 167 | + ) |
| 168 | + if all and version: |
| 169 | + click.secho( |
| 170 | + "Flag `--version` has no effect when calling the command with `--all`.", |
| 171 | + fg="yellow", |
| 172 | + ) |
| 173 | + if all: |
| 174 | + tools.upgrade_all_tools(config) |
| 175 | + else: |
| 176 | + tools.upgrade_tool(config, tool, version) |
0 commit comments