|
| 1 | +/* |
| 2 | + * Copyright (C) 2018-2025 Garden Technologies, Inc. <info@garden.io> |
| 3 | + * |
| 4 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 5 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 7 | + */ |
| 8 | + |
| 9 | +import type { CommandParams, CommandResult } from "../base.js" |
| 10 | +import { Command } from "../base.js" |
| 11 | +import { printEmoji, printHeader } from "../../logger/util.js" |
| 12 | +import { dedent, renderTable } from "../../util/string.js" |
| 13 | +import { styles } from "../../logger/styles.js" |
| 14 | +import { joi, joiArray } from "../../config/common.js" |
| 15 | +import { ConfigurationError } from "../../exceptions.js" |
| 16 | +import { getCloudListCommandBaseDescription, noApiMsg, throwIfLegacyCloud } from "../helpers.js" |
| 17 | +import { makeDocsLinkPlain } from "../../docs/common.js" |
| 18 | +import { getVarlistIdsFromRemoteVarsConfig } from "../../config/project.js" |
| 19 | +import type { RouterOutput } from "../../cloud/api/trpc.js" |
| 20 | +import type { EmptyObject } from "type-fest" |
| 21 | + |
| 22 | +const getRemoteVariablesOpts = {} |
| 23 | + |
| 24 | +type Opts = typeof getRemoteVariablesOpts |
| 25 | + |
| 26 | +interface RemoteVariable { |
| 27 | + name: string |
| 28 | + id: string |
| 29 | + value: string |
| 30 | + isSecret: boolean |
| 31 | + variableListName: string |
| 32 | + scopedToEnvironment: string |
| 33 | + scopedToUser: string |
| 34 | + expiresAt: string |
| 35 | + description: string |
| 36 | + scopedAccountId: string | null |
| 37 | + scopedEnvironmentId: string | null |
| 38 | +} |
| 39 | + |
| 40 | +export class GetRemoteVariablesCommand extends Command<EmptyObject, Opts> { |
| 41 | + name = "remote-variables" |
| 42 | + help = "Get remote variables from Garden Cloud" |
| 43 | + emoji = "☁️" |
| 44 | + |
| 45 | + override aliases = ["cloud-variables"] |
| 46 | + |
| 47 | + override description = dedent` |
| 48 | + ${getCloudListCommandBaseDescription("remote variables")} |
| 49 | +
|
| 50 | + List all remote variables for the variable lists configured in this project. This is useful for |
| 51 | + seeing the IDs of remote variables (e.g. for use with the \`garden delete remote-variables\` command) |
| 52 | + and for viewing cloud-specific information such as scoping and expiration. |
| 53 | +
|
| 54 | + Examples: |
| 55 | + garden get remote-variables # list remote variables and pretty print results |
| 56 | + garden get remote-variables --output json # returns remote variables as a JSON object, useful for scripting |
| 57 | +
|
| 58 | + See the [Variables and Templating guide](${makeDocsLinkPlain`features/variables-and-templating`}) for more information. |
| 59 | +
|
| 60 | + ` |
| 61 | + |
| 62 | + override options = getRemoteVariablesOpts |
| 63 | + |
| 64 | + override printHeader({ log }) { |
| 65 | + printHeader(log, "Get remote variables", "☁️") |
| 66 | + } |
| 67 | + |
| 68 | + override outputsSchema = () => |
| 69 | + joi.object().keys({ |
| 70 | + variables: joiArray( |
| 71 | + joi.object().keys({ |
| 72 | + name: joi.string(), |
| 73 | + id: joi.string(), |
| 74 | + value: joi.string(), |
| 75 | + isSecret: joi.boolean(), |
| 76 | + variableListName: joi.string(), |
| 77 | + environmentScope: joi.string(), |
| 78 | + userScope: joi.string(), |
| 79 | + expiresAt: joi.string().allow(""), |
| 80 | + description: joi.string(), |
| 81 | + }) |
| 82 | + ).description("A list of remote variables"), |
| 83 | + }) |
| 84 | + |
| 85 | + async action({ |
| 86 | + garden, |
| 87 | + log, |
| 88 | + }: CommandParams<EmptyObject, Opts>): Promise<CommandResult<{ variables: RemoteVariable[] }>> { |
| 89 | + throwIfLegacyCloud(garden, "garden cloud variables list") |
| 90 | + |
| 91 | + if (!garden.cloudApi) { |
| 92 | + throw new ConfigurationError({ message: noApiMsg("get", "cloud variables") }) |
| 93 | + } |
| 94 | + |
| 95 | + const config = await garden.dumpConfigWithInteralFields({ |
| 96 | + log, |
| 97 | + includeDisabled: false, |
| 98 | + resolveGraph: false, |
| 99 | + resolveProviders: false, |
| 100 | + resolveWorkflows: false, |
| 101 | + }) |
| 102 | + |
| 103 | + const variableListIds = getVarlistIdsFromRemoteVarsConfig(config.importVariables) |
| 104 | + |
| 105 | + if (variableListIds.length === 0) { |
| 106 | + log.info("No variable lists configured in this project.") |
| 107 | + return { result: { variables: [] } } |
| 108 | + } |
| 109 | + |
| 110 | + const allVariables: RouterOutput["variableList"]["listVariables"]["items"] = [] |
| 111 | + |
| 112 | + for (const variableListId of variableListIds) { |
| 113 | + let cursor: number | undefined = undefined |
| 114 | + |
| 115 | + do { |
| 116 | + log.debug(`Fetching variables for variable list ${variableListId}`) |
| 117 | + const response = await garden.cloudApi.trpc.variableList.listVariables.query({ |
| 118 | + organizationId: garden.cloudApi.organizationId, |
| 119 | + variableListId, |
| 120 | + ...(cursor && { cursor }), |
| 121 | + }) |
| 122 | + |
| 123 | + allVariables.push(...response.items) |
| 124 | + cursor = response.nextCursor |
| 125 | + } while (cursor) |
| 126 | + } |
| 127 | + |
| 128 | + const variables: RemoteVariable[] = allVariables.map((v) => ({ |
| 129 | + name: v.name, |
| 130 | + id: v.id, |
| 131 | + value: v.isSecret ? "<secret>" : v.value, |
| 132 | + isSecret: v.isSecret, |
| 133 | + variableListName: v.variableListName || "N/A", |
| 134 | + scopedToEnvironment: v.scopedGardenEnvironmentName || "None", |
| 135 | + scopedToUser: v.scopedAccountName || "None", |
| 136 | + expiresAt: v.expiresAt ? new Date(v.expiresAt).toISOString() : "Never", |
| 137 | + description: v.description || "", |
| 138 | + scopedAccountId: v.scopedAccountId, |
| 139 | + scopedEnvironmentId: v.scopedGardenEnvironmentId, |
| 140 | + })) |
| 141 | + |
| 142 | + const heading = [ |
| 143 | + "Name", |
| 144 | + "ID", |
| 145 | + "Value", |
| 146 | + "Variable List", |
| 147 | + "Environment Scope", |
| 148 | + "User Scope", |
| 149 | + "Expires At", |
| 150 | + "Secret", |
| 151 | + ].map((s) => styles.bold(s)) |
| 152 | + |
| 153 | + const rows: string[][] = variables.map((v) => { |
| 154 | + return [ |
| 155 | + styles.highlight.bold(v.name), |
| 156 | + v.id, |
| 157 | + v.value, |
| 158 | + v.variableListName, |
| 159 | + v.scopedToEnvironment, |
| 160 | + v.scopedToUser, |
| 161 | + v.expiresAt, |
| 162 | + v.isSecret ? "Yes" : "No", |
| 163 | + ] |
| 164 | + }) |
| 165 | + |
| 166 | + log.info("") |
| 167 | + log.info(renderTable([heading].concat(rows))) |
| 168 | + log.info(styles.success("OK") + " " + printEmoji("✔️", log)) |
| 169 | + |
| 170 | + return { result: { variables } } |
| 171 | + } |
| 172 | +} |
0 commit comments