|
| 1 | +import { Command } from 'commander'; |
| 2 | +import { checkGcloudInstalled, getActiveAccount } from '../core/gcloud.js'; |
| 3 | +import { logger } from '../core/logger.js'; |
| 4 | +import { readConfig, writeConfig } from '../core/config.js'; |
| 5 | +import { PATHS } from '../system/paths.js'; |
| 6 | +import fs from 'fs-extra'; |
| 7 | +import path from 'path'; |
| 8 | +import axios from 'axios'; |
| 9 | +import { execa } from 'execa'; |
| 10 | + |
| 11 | +// Official Google Cloud SDK download URL pattern |
| 12 | +const GCLOUD_DOWNLOAD_URL = 'https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-cli-windows-x86_64.zip'; |
| 13 | + |
| 14 | +export const gcloudCommand = new Command('gcloud') |
| 15 | + .description('Manage Google Cloud CLI'); |
| 16 | + |
| 17 | +gcloudCommand.command('status') |
| 18 | + .description('Check gcloud CLI status') |
| 19 | + .action(async () => { |
| 20 | + const config = await readConfig(); |
| 21 | + logger.info(`Configured gcloud path: ${config.gcloudPath || '(default)'}`); |
| 22 | + |
| 23 | + const installed = await checkGcloudInstalled(); |
| 24 | + if (installed) { |
| 25 | + logger.info('✅ gcloud CLI is installed and reachable.'); |
| 26 | + const account = await getActiveAccount(); |
| 27 | + logger.info(`Active account: ${account || 'None'}`); |
| 28 | + } else { |
| 29 | + logger.warn('❌ gcloud CLI is NOT installed or not found.'); |
| 30 | + } |
| 31 | + }); |
| 32 | + |
| 33 | +gcloudCommand.command('install') |
| 34 | + .description('Install portable Google Cloud CLI') |
| 35 | + .action(async () => { |
| 36 | + try { |
| 37 | + logger.info('Installing portable Google Cloud CLI...'); |
| 38 | + await fs.ensureDir(PATHS.GCLOUD_DIR); |
| 39 | + |
| 40 | + const zipPath = path.join(PATHS.TEMP, 'google-cloud-sdk.zip'); |
| 41 | + await fs.ensureDir(PATHS.TEMP); |
| 42 | + |
| 43 | + logger.info(`Downloading from ${GCLOUD_DOWNLOAD_URL}...`); |
| 44 | + const response = await axios({ |
| 45 | + url: GCLOUD_DOWNLOAD_URL, |
| 46 | + method: 'GET', |
| 47 | + responseType: 'stream' |
| 48 | + }); |
| 49 | + |
| 50 | + const writer = fs.createWriteStream(zipPath); |
| 51 | + response.data.pipe(writer); |
| 52 | + |
| 53 | + await new Promise<void>((resolve, reject) => { |
| 54 | + writer.on('finish', () => resolve()); |
| 55 | + writer.on('error', reject); |
| 56 | + }); |
| 57 | + |
| 58 | + logger.info('Extracting...'); |
| 59 | + // Use PowerShell to extract |
| 60 | + await execa('powershell', ['-Command', `Expand-Archive -Path "${zipPath}" -DestinationPath "${PATHS.GCLOUD_DIR}" -Force`]); |
| 61 | + |
| 62 | + const gcloudExe = path.join(PATHS.GCLOUD_DIR, 'google-cloud-sdk', 'bin', 'gcloud.cmd'); |
| 63 | + |
| 64 | + if (await fs.pathExists(gcloudExe)) { |
| 65 | + logger.info(`gcloud installed to ${gcloudExe}`); |
| 66 | + await writeConfig({ gcloudPath: gcloudExe }); |
| 67 | + logger.info('Configuration updated.'); |
| 68 | + } else { |
| 69 | + throw new Error('gcloud.cmd not found after extraction'); |
| 70 | + } |
| 71 | + |
| 72 | + // Cleanup |
| 73 | + await fs.remove(zipPath); |
| 74 | + |
| 75 | + } catch (error) { |
| 76 | + logger.error('Failed to install gcloud', error); |
| 77 | + process.exit(1); |
| 78 | + } |
| 79 | + }); |
0 commit comments