// SPDX-License-Identifier: MIT import { acquireResourceTokenFromLogin } from "../../azure/index.ts"; import { getDevOpsApiToken } from "../../devops/index.ts"; import { loadPublicConfig } from "../../index.ts"; import type { CommandValues } from "./types.ts"; export function usageGetToken(): string { return `Usage: sk-az-tools get-token --type|-t [global options] Options: --type, -t Token type: azurerm|devops`; } export async function runGetTokenCommand(values: CommandValues): Promise { const tokenType = (values.type ?? "").toString().trim().toLowerCase(); if (!tokenType) { throw new Error("--type is required for get-token (allowed: azurerm, devops)"); } const config = await loadPublicConfig(); if (!config.tenantId) { throw new Error("tenantId is required"); } if (!config.clientId) { throw new Error("clientId is required"); } if (tokenType === "azurerm") { const result = await acquireResourceTokenFromLogin({ tenantId: config.tenantId, clientId: config.clientId, resource: "arm", }); const accessToken = result?.accessToken; if (!accessToken) { throw new Error("Failed to obtain AzureRM token"); } return { tokenType, accessToken, }; } if (tokenType === "devops") { const accessToken = await getDevOpsApiToken(config.tenantId, config.clientId); return { tokenType, accessToken, }; } throw new Error(`Invalid --type '${values.type}'. Allowed: azurerm, devops`); }