@fredlackey/devutils 0.0.14 → 0.0.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,778 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * @fileoverview Install tfenv (Terraform Version Manager).
5
+ * @module installs/tfenv
6
+ *
7
+ * tfenv is a version manager for Terraform, inspired by rbenv. It allows you to
8
+ * install, switch between, and manage multiple versions of Terraform on the same
9
+ * machine. This is essential for developers and DevOps engineers who work on
10
+ * projects requiring different Terraform versions, ensuring compatibility and
11
+ * reducing the risk of applying infrastructure changes with an incorrect Terraform
12
+ * binary.
13
+ *
14
+ * Key capabilities include:
15
+ * - Version switching: Easily install and switch between multiple Terraform versions
16
+ * - Automatic version selection: Use .terraform-version files to auto-select the
17
+ * correct version per project
18
+ * - Hash verification: Automatically validates downloads against HashiCorp's
19
+ * published SHA256 hashes
20
+ * - Signature verification: Optionally verify PGP signatures using Keybase or GnuPG
21
+ *
22
+ * IMPORTANT PLATFORM DISTINCTION:
23
+ * - macOS: Uses Homebrew to install tfenv (recommended)
24
+ * - Ubuntu/Debian/WSL/Raspbian: Git clone to ~/.tfenv with PATH configuration
25
+ * - Amazon Linux: Git clone to ~/.tfenv with PATH configuration
26
+ * - Windows (native): tfenv has experimental/failing support - marked as not supported
27
+ * - Git Bash: tfenv has experimental/failing support - marked as not supported
28
+ *
29
+ * POST-INSTALLATION NOTES:
30
+ * - Unix-like systems require sourcing the shell configuration or opening a new terminal
31
+ * - tfenv is installed per-user and does not require sudo for Terraform version management
32
+ * - Use .terraform-version files in project roots for automatic version selection
33
+ */
34
+
35
+ const os = require('../utils/common/os');
36
+ const shell = require('../utils/common/shell');
37
+ const brew = require('../utils/macos/brew');
38
+ const fs = require('fs');
39
+ const path = require('path');
40
+
41
+ /**
42
+ * The Homebrew formula name for tfenv on macOS.
43
+ */
44
+ const HOMEBREW_FORMULA_NAME = 'tfenv';
45
+
46
+ /**
47
+ * The GitHub repository URL for tfenv.
48
+ * This is used for git clone installation on Linux systems.
49
+ */
50
+ const TFENV_REPO_URL = 'https://github.com/tfutils/tfenv.git';
51
+
52
+ /**
53
+ * The default installation directory for tfenv on Unix-like systems.
54
+ * Installed to the user's home directory for per-user isolation.
55
+ */
56
+ const TFENV_INSTALL_DIR = '.tfenv';
57
+
58
+ /**
59
+ * Check if tfenv is installed by verifying the tfenv command exists in PATH.
60
+ *
61
+ * tfenv installs a shell script, so we can use shell.commandExists() to verify.
62
+ * For git-based installations, we also check for the ~/.tfenv directory.
63
+ *
64
+ * @returns {boolean} True if tfenv is installed, false otherwise
65
+ */
66
+ function isTfenvInstalled() {
67
+ // First check if tfenv is in PATH
68
+ if (shell.commandExists('tfenv')) {
69
+ return true;
70
+ }
71
+
72
+ // Also check for the existence of the tfenv directory (git clone installation)
73
+ const homeDir = os.getHomeDir();
74
+ const tfenvDir = path.join(homeDir, TFENV_INSTALL_DIR);
75
+ const tfenvBin = path.join(tfenvDir, 'bin', 'tfenv');
76
+
77
+ return fs.existsSync(tfenvBin);
78
+ }
79
+
80
+ /**
81
+ * Get the installed tfenv version.
82
+ *
83
+ * Executes 'tfenv --version' to get the version string.
84
+ * For git-based installations, we source the PATH first.
85
+ *
86
+ * @returns {Promise<string|null>} tfenv version string, or null if not installed
87
+ */
88
+ async function getTfenvVersion() {
89
+ // First try directly if in PATH
90
+ if (shell.commandExists('tfenv')) {
91
+ const result = await shell.exec('tfenv --version');
92
+ if (result.code === 0 && result.stdout) {
93
+ return result.stdout.trim();
94
+ }
95
+ }
96
+
97
+ // For git clone installations, source PATH and try again
98
+ const homeDir = os.getHomeDir();
99
+ const tfenvDir = path.join(homeDir, TFENV_INSTALL_DIR);
100
+
101
+ if (fs.existsSync(path.join(tfenvDir, 'bin', 'tfenv'))) {
102
+ const result = await shell.exec(
103
+ `bash -c 'export PATH="$HOME/${TFENV_INSTALL_DIR}/bin:$PATH" && tfenv --version'`
104
+ );
105
+ if (result.code === 0 && result.stdout) {
106
+ return result.stdout.trim();
107
+ }
108
+ }
109
+
110
+ return null;
111
+ }
112
+
113
+ /**
114
+ * Detect the user's default shell on Unix-like systems.
115
+ *
116
+ * This is used to determine which shell configuration file to update
117
+ * after tfenv installation.
118
+ *
119
+ * @returns {string} The shell name ('bash', 'zsh', or 'sh')
120
+ */
121
+ function detectShell() {
122
+ const shellEnv = process.env.SHELL || '';
123
+
124
+ if (shellEnv.includes('zsh')) {
125
+ return 'zsh';
126
+ } else if (shellEnv.includes('bash')) {
127
+ return 'bash';
128
+ }
129
+ return 'bash'; // Default to bash
130
+ }
131
+
132
+ /**
133
+ * Get the shell configuration file path for the current user.
134
+ *
135
+ * Returns the appropriate rc file based on the detected shell.
136
+ *
137
+ * @returns {string} Path to the shell configuration file
138
+ */
139
+ function getShellConfigFile() {
140
+ const homeDir = os.getHomeDir();
141
+ const userShell = detectShell();
142
+
143
+ if (userShell === 'zsh') {
144
+ return path.join(homeDir, '.zshrc');
145
+ }
146
+ return path.join(homeDir, '.bashrc');
147
+ }
148
+
149
+ /**
150
+ * Check if tfenv PATH configuration already exists in a shell config file.
151
+ *
152
+ * Searches for the tfenv PATH export line to determine if tfenv has already
153
+ * been configured in the given file.
154
+ *
155
+ * @param {string} filePath - Path to the shell configuration file
156
+ * @returns {boolean} True if tfenv configuration is present, false otherwise
157
+ */
158
+ function hasTfenvConfig(filePath) {
159
+ if (!fs.existsSync(filePath)) {
160
+ return false;
161
+ }
162
+
163
+ try {
164
+ const content = fs.readFileSync(filePath, 'utf8');
165
+ // Check for tfenv PATH configuration
166
+ return content.includes('.tfenv/bin') || content.includes('tfenv');
167
+ } catch (err) {
168
+ return false;
169
+ }
170
+ }
171
+
172
+ /**
173
+ * Add tfenv PATH configuration to the shell config file.
174
+ *
175
+ * Appends the necessary PATH export to the user's shell configuration file
176
+ * so that tfenv is available in new terminal sessions.
177
+ *
178
+ * @param {string} configFile - Path to the shell configuration file
179
+ * @returns {boolean} True if configuration was added successfully
180
+ */
181
+ function addTfenvToPath(configFile) {
182
+ // tfenv configuration block
183
+ const tfenvConfig = `
184
+ # tfenv configuration
185
+ export PATH="$HOME/.tfenv/bin:$PATH"
186
+ `;
187
+
188
+ try {
189
+ fs.appendFileSync(configFile, tfenvConfig);
190
+ return true;
191
+ } catch (err) {
192
+ console.log(`Warning: Could not update ${configFile}: ${err.message}`);
193
+ return false;
194
+ }
195
+ }
196
+
197
+ /**
198
+ * Install required dependencies for tfenv on Debian-based systems.
199
+ *
200
+ * tfenv requires git for installation and updates, curl or wget for downloading
201
+ * Terraform binaries, and unzip for extracting them.
202
+ *
203
+ * @returns {Promise<boolean>} True if dependencies were installed successfully
204
+ */
205
+ async function installDebianDependencies() {
206
+ console.log('Installing required dependencies (git, curl, unzip)...');
207
+
208
+ const result = await shell.exec(
209
+ 'sudo DEBIAN_FRONTEND=noninteractive apt-get update -y && ' +
210
+ 'sudo DEBIAN_FRONTEND=noninteractive apt-get install -y git curl unzip'
211
+ );
212
+
213
+ if (result.code !== 0) {
214
+ console.log('Warning: Failed to install some dependencies.');
215
+ console.log(result.stderr || result.stdout);
216
+ return false;
217
+ }
218
+
219
+ return true;
220
+ }
221
+
222
+ /**
223
+ * Install required dependencies for tfenv on Amazon Linux systems.
224
+ *
225
+ * Uses dnf (AL2023) or yum (AL2) to install git, curl, and unzip.
226
+ *
227
+ * @returns {Promise<boolean>} True if dependencies were installed successfully
228
+ */
229
+ async function installAmazonLinuxDependencies() {
230
+ console.log('Installing required dependencies (git, curl, unzip)...');
231
+
232
+ // Detect package manager (dnf for AL2023, yum for AL2)
233
+ const hasDnf = shell.commandExists('dnf');
234
+ const packageManager = hasDnf ? 'dnf' : 'yum';
235
+
236
+ const result = await shell.exec(
237
+ `sudo ${packageManager} install -y git curl unzip`
238
+ );
239
+
240
+ if (result.code !== 0) {
241
+ console.log('Warning: Failed to install some dependencies.');
242
+ console.log(result.stderr || result.stdout);
243
+ return false;
244
+ }
245
+
246
+ return true;
247
+ }
248
+
249
+ /**
250
+ * Clone the tfenv repository to the user's home directory.
251
+ *
252
+ * Uses a shallow clone (--depth=1) for faster download.
253
+ * The repository is cloned to ~/.tfenv.
254
+ *
255
+ * @returns {Promise<boolean>} True if clone was successful
256
+ */
257
+ async function cloneTfenvRepository() {
258
+ const homeDir = os.getHomeDir();
259
+ const tfenvDir = path.join(homeDir, TFENV_INSTALL_DIR);
260
+
261
+ // Check if directory already exists
262
+ if (fs.existsSync(tfenvDir)) {
263
+ console.log(`Directory ${tfenvDir} already exists.`);
264
+ console.log('Attempting to update existing installation...');
265
+
266
+ // Try to update existing installation
267
+ const pullResult = await shell.exec(`git -C "${tfenvDir}" pull`);
268
+ if (pullResult.code === 0) {
269
+ console.log('Successfully updated existing tfenv installation.');
270
+ return true;
271
+ }
272
+
273
+ console.log('Could not update existing installation.');
274
+ console.log('To reinstall, remove the directory and run again:');
275
+ console.log(` rm -rf ${tfenvDir}`);
276
+ return false;
277
+ }
278
+
279
+ // Clone the repository
280
+ console.log('Cloning tfenv repository...');
281
+ const cloneResult = await shell.exec(
282
+ `git clone --depth=1 ${TFENV_REPO_URL} "${tfenvDir}"`
283
+ );
284
+
285
+ if (cloneResult.code !== 0) {
286
+ console.log('Failed to clone tfenv repository.');
287
+ console.log(cloneResult.stderr || cloneResult.stdout);
288
+ console.log('');
289
+ console.log('Troubleshooting:');
290
+ console.log(' 1. Check your internet connection');
291
+ console.log(' 2. Ensure git is installed: sudo apt-get install git');
292
+ console.log(' 3. Try cloning manually:');
293
+ console.log(` git clone ${TFENV_REPO_URL} ~/.tfenv`);
294
+ return false;
295
+ }
296
+
297
+ return true;
298
+ }
299
+
300
+ /**
301
+ * Install tfenv on macOS using Homebrew.
302
+ *
303
+ * Prerequisites:
304
+ * - macOS 10.15 (Catalina) or later
305
+ * - Homebrew package manager installed
306
+ * - zsh shell (default on macOS 10.15+) or bash
307
+ *
308
+ * This function installs tfenv via Homebrew which handles PATH configuration
309
+ * automatically. If the conflicting 'tenv' package is installed, it should
310
+ * be removed first.
311
+ *
312
+ * @returns {Promise<void>}
313
+ */
314
+ async function install_macos() {
315
+ console.log('Checking if tfenv is already installed...');
316
+
317
+ // Check if tfenv is already installed via Homebrew formula
318
+ const isBrewTfenvInstalled = await brew.isFormulaInstalled(HOMEBREW_FORMULA_NAME);
319
+ if (isBrewTfenvInstalled) {
320
+ const version = await getTfenvVersion();
321
+ console.log(`tfenv ${version || 'unknown version'} is already installed via Homebrew, skipping...`);
322
+ console.log('');
323
+ console.log('To manage Terraform versions:');
324
+ console.log(' tfenv install latest # Install latest Terraform');
325
+ console.log(' tfenv use latest # Switch to latest version');
326
+ console.log(' terraform --version # Verify installation');
327
+ return;
328
+ }
329
+
330
+ // Also check if tfenv is installed via other means (e.g., git clone)
331
+ if (isTfenvInstalled()) {
332
+ const version = await getTfenvVersion();
333
+ console.log(`tfenv ${version || 'unknown version'} is already installed, skipping...`);
334
+ console.log('');
335
+ console.log('Note: tfenv was not installed via Homebrew.');
336
+ console.log('If you want to manage it with Homebrew, first uninstall the existing tfenv.');
337
+ return;
338
+ }
339
+
340
+ // Verify Homebrew is available
341
+ if (!brew.isInstalled()) {
342
+ console.log('Homebrew is not installed. Please install Homebrew first.');
343
+ console.log('Run: dev install homebrew');
344
+ return;
345
+ }
346
+
347
+ // Install tfenv using Homebrew with --quiet flag for cleaner output
348
+ console.log('Installing tfenv via Homebrew...');
349
+ const result = await shell.exec('brew install --quiet tfenv');
350
+
351
+ if (result.code !== 0) {
352
+ console.log('Failed to install tfenv via Homebrew.');
353
+ console.log(result.stderr || result.stdout);
354
+ console.log('');
355
+ console.log('Troubleshooting:');
356
+ console.log(' 1. If you have tenv installed, remove it first:');
357
+ console.log(' brew uninstall tenv');
358
+ console.log(' 2. Try updating Homebrew: brew update');
359
+ console.log(' 3. Try manual installation: brew install tfenv');
360
+ return;
361
+ }
362
+
363
+ // Verify the installation succeeded
364
+ const verified = await brew.isFormulaInstalled(HOMEBREW_FORMULA_NAME);
365
+ if (!verified) {
366
+ console.log('Installation may have failed: tfenv formula not found after install.');
367
+ return;
368
+ }
369
+
370
+ const version = await getTfenvVersion();
371
+ console.log('');
372
+ console.log(`tfenv ${version || ''} installed successfully via Homebrew.`);
373
+ console.log('');
374
+ console.log('IMPORTANT: To complete setup:');
375
+ console.log(' 1. Open a new terminal (Homebrew adds tfenv to PATH automatically)');
376
+ console.log(' 2. Verify tfenv is working: tfenv --version');
377
+ console.log(' 3. Install Terraform: tfenv install latest');
378
+ console.log(' 4. Use Terraform: tfenv use latest');
379
+ console.log(' 5. Verify Terraform: terraform --version');
380
+ }
381
+
382
+ /**
383
+ * Install tfenv on Ubuntu/Debian using git clone.
384
+ *
385
+ * Prerequisites:
386
+ * - Ubuntu 20.04 or later, or Debian 10 or later
387
+ * - sudo privileges (for installing git, curl, unzip if not present)
388
+ * - git, curl, and unzip installed
389
+ *
390
+ * This function clones the tfenv repository to ~/.tfenv and configures
391
+ * the PATH in the user's shell configuration file.
392
+ *
393
+ * Note: tfenv is not available in the official Ubuntu/Debian APT repositories.
394
+ *
395
+ * @returns {Promise<void>}
396
+ */
397
+ async function install_ubuntu() {
398
+ console.log('Checking if tfenv is already installed...');
399
+
400
+ // Check if tfenv is already installed
401
+ if (isTfenvInstalled()) {
402
+ const version = await getTfenvVersion();
403
+ console.log(`tfenv ${version || 'unknown version'} is already installed, skipping...`);
404
+ console.log('');
405
+ console.log('To use tfenv, open a new terminal or run: source ~/.bashrc');
406
+ return;
407
+ }
408
+
409
+ // Install dependencies
410
+ await installDebianDependencies();
411
+
412
+ // Clone the tfenv repository
413
+ const cloneSuccess = await cloneTfenvRepository();
414
+ if (!cloneSuccess) {
415
+ return;
416
+ }
417
+
418
+ // Add tfenv to PATH in shell config
419
+ const configFile = getShellConfigFile();
420
+ if (!hasTfenvConfig(configFile)) {
421
+ console.log(`Adding tfenv to PATH in ${configFile}...`);
422
+ addTfenvToPath(configFile);
423
+ } else {
424
+ console.log('tfenv PATH configuration already exists in shell config file.');
425
+ }
426
+
427
+ // Get the installed version
428
+ const version = await getTfenvVersion();
429
+
430
+ console.log('');
431
+ console.log(`tfenv ${version || ''} installed successfully.`);
432
+ console.log('');
433
+ console.log('IMPORTANT: To complete setup:');
434
+ console.log(' 1. Open a new terminal, OR run: source ~/.bashrc');
435
+ console.log(' 2. Verify tfenv is working: tfenv --version');
436
+ console.log(' 3. Install Terraform: tfenv install latest');
437
+ console.log(' 4. Use Terraform: tfenv use latest');
438
+ console.log(' 5. Verify Terraform: terraform --version');
439
+ }
440
+
441
+ /**
442
+ * Install tfenv on Ubuntu running in WSL (Windows Subsystem for Linux).
443
+ *
444
+ * Prerequisites:
445
+ * - Windows 10 version 2004 or higher, or Windows 11
446
+ * - WSL 2 enabled with Ubuntu distribution installed
447
+ * - sudo privileges within WSL
448
+ *
449
+ * WSL runs a full Linux environment, so tfenv installation follows the same
450
+ * process as native Ubuntu using git clone.
451
+ *
452
+ * NOTE: tfenv installed in WSL is separate from any Windows Terraform installation.
453
+ *
454
+ * @returns {Promise<void>}
455
+ */
456
+ async function install_ubuntu_wsl() {
457
+ console.log('Detected Ubuntu running in WSL (Windows Subsystem for Linux).');
458
+ console.log('');
459
+
460
+ // Check if tfenv is already installed
461
+ if (isTfenvInstalled()) {
462
+ const version = await getTfenvVersion();
463
+ console.log(`tfenv ${version || 'unknown version'} is already installed in WSL, skipping...`);
464
+ console.log('');
465
+ console.log('To use tfenv, open a new terminal or run: source ~/.bashrc');
466
+ return;
467
+ }
468
+
469
+ // Install dependencies
470
+ await installDebianDependencies();
471
+
472
+ // Clone the tfenv repository
473
+ const cloneSuccess = await cloneTfenvRepository();
474
+ if (!cloneSuccess) {
475
+ return;
476
+ }
477
+
478
+ // Add tfenv to PATH in shell config
479
+ const configFile = getShellConfigFile();
480
+ if (!hasTfenvConfig(configFile)) {
481
+ console.log(`Adding tfenv to PATH in ${configFile}...`);
482
+ addTfenvToPath(configFile);
483
+ }
484
+
485
+ const version = await getTfenvVersion();
486
+
487
+ console.log('');
488
+ console.log(`tfenv ${version || ''} installed successfully in WSL.`);
489
+ console.log('');
490
+ console.log('IMPORTANT: To complete setup:');
491
+ console.log(' 1. Open a new terminal, OR run: source ~/.bashrc');
492
+ console.log(' 2. Verify tfenv is working: tfenv --version');
493
+ console.log(' 3. Install Terraform: tfenv install latest');
494
+ console.log(' 4. Use Terraform: tfenv use latest');
495
+ console.log(' 5. Verify Terraform: terraform --version');
496
+ console.log('');
497
+ console.log('WSL Note:');
498
+ console.log(' tfenv in WSL is separate from any Windows Terraform installation.');
499
+ console.log(' For Windows native Terraform, install it separately using Chocolatey.');
500
+ }
501
+
502
+ /**
503
+ * Install tfenv on Raspberry Pi OS using git clone.
504
+ *
505
+ * Prerequisites:
506
+ * - Raspberry Pi OS (Bookworm, Bullseye, or Buster) - 64-bit or 32-bit
507
+ * - Raspberry Pi 2 or later
508
+ * - sudo privileges
509
+ * - git, curl, and unzip installed
510
+ *
511
+ * tfenv automatically detects ARM architecture and downloads the appropriate
512
+ * Terraform binary. For 64-bit Raspberry Pi OS, it uses linux_arm64 binaries.
513
+ * For 32-bit, it uses linux_arm binaries.
514
+ *
515
+ * Note: Raspberry Pi Zero/1 (armv6l) have limited Terraform version support.
516
+ *
517
+ * @returns {Promise<void>}
518
+ */
519
+ async function install_raspbian() {
520
+ console.log('Checking if tfenv is already installed...');
521
+
522
+ // Check if tfenv is already installed
523
+ if (isTfenvInstalled()) {
524
+ const version = await getTfenvVersion();
525
+ console.log(`tfenv ${version || 'unknown version'} is already installed, skipping...`);
526
+ console.log('');
527
+ console.log('To use tfenv, open a new terminal or run: source ~/.bashrc');
528
+ return;
529
+ }
530
+
531
+ // Check and report architecture for informational purposes
532
+ const archResult = await shell.exec('uname -m');
533
+ const arch = archResult.stdout.trim();
534
+ console.log(`Detected architecture: ${arch}`);
535
+
536
+ // Provide guidance for armv6l users
537
+ if (arch === 'armv6l') {
538
+ console.log('');
539
+ console.log('NOTE: ARMv6 (Raspberry Pi Zero/1) is detected.');
540
+ console.log('tfenv will install successfully, but some newer Terraform versions');
541
+ console.log('may not have ARMv6 builds available.');
542
+ console.log('');
543
+ }
544
+
545
+ // Install dependencies
546
+ await installDebianDependencies();
547
+
548
+ // Clone the tfenv repository
549
+ const cloneSuccess = await cloneTfenvRepository();
550
+ if (!cloneSuccess) {
551
+ return;
552
+ }
553
+
554
+ // Add tfenv to PATH in shell config
555
+ const configFile = getShellConfigFile();
556
+ if (!hasTfenvConfig(configFile)) {
557
+ console.log(`Adding tfenv to PATH in ${configFile}...`);
558
+ addTfenvToPath(configFile);
559
+ }
560
+
561
+ const version = await getTfenvVersion();
562
+
563
+ console.log('');
564
+ console.log(`tfenv ${version || ''} installed successfully.`);
565
+ console.log('');
566
+ console.log('IMPORTANT: To complete setup:');
567
+ console.log(' 1. Open a new terminal, OR run: source ~/.bashrc');
568
+ console.log(' 2. Verify tfenv is working: tfenv --version');
569
+ console.log(' 3. Install Terraform: tfenv install latest');
570
+ console.log(' 4. Use Terraform: tfenv use latest');
571
+ console.log(' 5. Verify Terraform: terraform --version');
572
+
573
+ // Extra guidance for ARM architecture
574
+ if (arch === 'aarch64') {
575
+ console.log('');
576
+ console.log('ARM64 Note: tfenv will download linux_arm64 Terraform binaries.');
577
+ } else if (arch === 'armv7l' || arch === 'armv6l') {
578
+ console.log('');
579
+ console.log('ARM32 Note: tfenv will download linux_arm Terraform binaries.');
580
+ console.log('Some older Terraform versions may not have ARM32 builds.');
581
+ }
582
+ }
583
+
584
+ /**
585
+ * Install tfenv on Amazon Linux using git clone.
586
+ *
587
+ * Prerequisites:
588
+ * - Amazon Linux 2023 (AL2023) or Amazon Linux 2 (AL2)
589
+ * - sudo privileges (typically ec2-user on EC2 instances)
590
+ * - git, curl, and unzip installed
591
+ *
592
+ * This is a common setup for managing Terraform versions on AWS EC2 instances.
593
+ *
594
+ * @returns {Promise<void>}
595
+ */
596
+ async function install_amazon_linux() {
597
+ console.log('Checking if tfenv is already installed...');
598
+
599
+ // Check if tfenv is already installed
600
+ if (isTfenvInstalled()) {
601
+ const version = await getTfenvVersion();
602
+ console.log(`tfenv ${version || 'unknown version'} is already installed, skipping...`);
603
+ console.log('');
604
+ console.log('To use tfenv, open a new terminal or run: source ~/.bashrc');
605
+ return;
606
+ }
607
+
608
+ // Install dependencies
609
+ await installAmazonLinuxDependencies();
610
+
611
+ // Clone the tfenv repository
612
+ const cloneSuccess = await cloneTfenvRepository();
613
+ if (!cloneSuccess) {
614
+ return;
615
+ }
616
+
617
+ // Add tfenv to PATH in shell config
618
+ const configFile = getShellConfigFile();
619
+ if (!hasTfenvConfig(configFile)) {
620
+ console.log(`Adding tfenv to PATH in ${configFile}...`);
621
+ addTfenvToPath(configFile);
622
+ }
623
+
624
+ const version = await getTfenvVersion();
625
+
626
+ console.log('');
627
+ console.log(`tfenv ${version || ''} installed successfully.`);
628
+ console.log('');
629
+ console.log('IMPORTANT: To complete setup:');
630
+ console.log(' 1. Open a new terminal, OR run: source ~/.bashrc');
631
+ console.log(' 2. Verify tfenv is working: tfenv --version');
632
+ console.log(' 3. Install Terraform: tfenv install latest');
633
+ console.log(' 4. Use Terraform: tfenv use latest');
634
+ console.log(' 5. Verify Terraform: terraform --version');
635
+ console.log('');
636
+ console.log('EC2 Note:');
637
+ console.log(' tfenv is user-specific. If creating an AMI, the tfenv installation');
638
+ console.log(' will persist, but users must source ~/.bashrc or open a new shell.');
639
+ }
640
+
641
+ /**
642
+ * Install tfenv on Windows.
643
+ *
644
+ * tfenv does not run natively on Windows (PowerShell or Command Prompt).
645
+ * Windows support is experimental and has known symlink issues.
646
+ *
647
+ * For Windows users, the recommended approach is to use WSL with Ubuntu
648
+ * and install tfenv there, or use Chocolatey to install Terraform directly:
649
+ * choco install terraform --version=X.Y.Z -y
650
+ *
651
+ * @returns {Promise<void>}
652
+ */
653
+ async function install_windows() {
654
+ console.log('tfenv is not available for Windows.');
655
+ return;
656
+ }
657
+
658
+ /**
659
+ * Install tfenv on Git Bash (Windows).
660
+ *
661
+ * tfenv on Windows Git Bash is experimental and has known symlink issues.
662
+ * The tfenv maintainers note that Windows (64-bit) is "only tested in git-bash
663
+ * and is currently presumed failing due to symlink issues."
664
+ *
665
+ * For reliable Terraform version management on Windows, consider:
666
+ * - Using WSL with Ubuntu where tfenv works reliably
667
+ * - Using Chocolatey to install Terraform directly:
668
+ * choco install terraform --version=X.Y.Z -y
669
+ *
670
+ * @returns {Promise<void>}
671
+ */
672
+ async function install_gitbash() {
673
+ console.log('tfenv is not available for Git Bash.');
674
+ return;
675
+ }
676
+
677
+ /**
678
+ * Check if tfenv is installed on the current platform.
679
+ *
680
+ * @returns {Promise<boolean>} True if installed, false otherwise
681
+ */
682
+ async function isInstalled() {
683
+ return isTfenvInstalled();
684
+ }
685
+
686
+ /**
687
+ * Check if this installer is supported on the current platform.
688
+ *
689
+ * tfenv is supported on POSIX-compliant systems:
690
+ * - macOS (via Homebrew)
691
+ * - Ubuntu/Debian (via git clone)
692
+ * - Raspberry Pi OS (via git clone)
693
+ * - Amazon Linux/RHEL/Fedora (via git clone)
694
+ * - WSL (via git clone)
695
+ *
696
+ * tfenv is NOT supported on:
697
+ * - Windows (native) - experimental with known issues
698
+ * - Git Bash - experimental with known symlink issues
699
+ *
700
+ * @returns {boolean} True if installation is supported on this platform
701
+ */
702
+ function isEligible() {
703
+ const platform = os.detect();
704
+ // Supported platforms (POSIX-compliant systems)
705
+ return ['macos', 'ubuntu', 'debian', 'wsl', 'raspbian', 'amazon_linux', 'fedora', 'rhel'].includes(platform.type);
706
+ }
707
+
708
+ /**
709
+ * Main installation entry point - detects platform and runs appropriate installer.
710
+ *
711
+ * This function detects the current operating system and dispatches to the
712
+ * appropriate platform-specific installer function.
713
+ *
714
+ * Supported platforms:
715
+ * - macOS: tfenv via Homebrew
716
+ * - Ubuntu/Debian: tfenv via git clone to ~/.tfenv
717
+ * - Ubuntu on WSL: tfenv via git clone to ~/.tfenv
718
+ * - Raspberry Pi OS: tfenv via git clone to ~/.tfenv
719
+ * - Amazon Linux/RHEL/Fedora: tfenv via git clone to ~/.tfenv
720
+ *
721
+ * Unsupported platforms (graceful message):
722
+ * - Windows (native): experimental, marked as not available
723
+ * - Git Bash: experimental, marked as not available
724
+ *
725
+ * @returns {Promise<void>}
726
+ */
727
+ async function install() {
728
+ const platform = os.detect();
729
+
730
+ // Map platform types to their corresponding installer functions
731
+ // Multiple platform types can map to the same installer
732
+ const installers = {
733
+ 'macos': install_macos,
734
+ 'ubuntu': install_ubuntu,
735
+ 'debian': install_ubuntu,
736
+ 'wsl': install_ubuntu_wsl,
737
+ 'raspbian': install_raspbian,
738
+ 'amazon_linux': install_amazon_linux,
739
+ 'fedora': install_amazon_linux,
740
+ 'rhel': install_amazon_linux,
741
+ 'windows': install_windows,
742
+ 'gitbash': install_gitbash,
743
+ };
744
+
745
+ // Look up the installer for the detected platform
746
+ const installer = installers[platform.type];
747
+
748
+ // If no installer exists for this platform, inform the user gracefully
749
+ if (!installer) {
750
+ console.log(`tfenv is not available for ${platform.type}.`);
751
+ return;
752
+ }
753
+
754
+ // Run the platform-specific installer
755
+ await installer();
756
+ }
757
+
758
+ // Export all functions for use as a module and for testing
759
+ module.exports = {
760
+ install,
761
+ isInstalled,
762
+ isEligible,
763
+ install_macos,
764
+ install_ubuntu,
765
+ install_ubuntu_wsl,
766
+ install_raspbian,
767
+ install_amazon_linux,
768
+ install_windows,
769
+ install_gitbash,
770
+ };
771
+
772
+ // Allow direct execution: node tfenv.js
773
+ if (require.main === module) {
774
+ install().catch(err => {
775
+ console.error(err.message);
776
+ process.exit(1);
777
+ });
778
+ }