tfv 3.2.4 → 4.0.0

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.
package/README.md CHANGED
@@ -75,6 +75,13 @@ https://user-images.githubusercontent.com/25563661/209584148-05a86ee1-f497-4c6d-
75
75
 
76
76
  - #### _INSTALL_
77
77
 
78
+ | Version | Description |
79
+ | ---------------- | ------------------------------------------ |
80
+ | x.x.x | Installs terraform version x.x.x |
81
+ | x^ | Installs latest version of release x |
82
+ | x.x.^ | Installs latest version of release x.x |
83
+ | latest | Installs latest version of terraform |
84
+
78
85
  ```sh
79
86
  tfv install <version>
80
87
  ```
@@ -82,31 +89,43 @@ tfv install <version>
82
89
  Run with alias
83
90
 
84
91
  ```sh
85
- tfv i <version>
92
+ tfv i <version> -a <system-architecture>
86
93
  ```
87
94
 
88
- | Version | Description |
89
- | ---------------- | ------------------------------------------ |
90
- | x.x.x | Installs terraform version x.x.x |
91
- | x^ | Installs latest version of release x |
92
- | x.x.^ | Installs latest version of release x.x |
93
- | latest | Installs latest version of terraform |
95
+ Run with system-architecture option
94
96
 
95
- - #### _USE_
97
+ ```sh
98
+ tfv install <version> --arch <system-architecture>
99
+ ```
100
+
101
+ EXAMPLE:
96
102
 
97
103
  ```sh
98
- tfv use <version>
104
+ tfv install 1.5.7 -arch amd64
99
105
  ```
100
106
 
107
+
108
+ - #### _USE_
109
+
101
110
  | Version | Description |
102
111
  | ---------------- | ----------------------------------------- |
103
112
  | x.x.x | use terraform version x.x.x |
104
113
  | latest | use latest version of terraform |
105
114
 
115
+ ```sh
116
+ tfv use <version>
117
+ ```
118
+
106
119
  > **_NOTE:_** If you're using windows OS, you would be prompted for admin privilege. Accept it. This is a one-time request to set terraform location in you system path. Unix machines would also get password prompt, as this requires permission to copy terraform to your bin directory.
107
120
 
108
121
  - #### _LIST_
109
122
 
123
+ | Option | Option Alias | Description |
124
+ | ---------------|---------------|--------------------------------------------------------------------------------|
125
+ | | | Defaults to listing terraform versions installed locally (in tfv store) |
126
+ | `--local` | `-l` | Lists all terraform versions installed locally |
127
+ | `--remote` | `-r` | Lists all terraform versions available remotely, on terraform server |
128
+
110
129
  ```sh
111
130
  tfv list [option]
112
131
  ```
@@ -117,30 +136,30 @@ Run with alias
117
136
  tfv ls [option]
118
137
  ```
119
138
 
120
- | Option | Option Alias | Description |
121
- | ---------------|---------------|--------------------------------------------------------------------------------|
122
- | | | Defaults to listing terraform versions installed locally (in tfv store) |
123
- | `--local` | `-l` | Lists all terraform versions installed locally |
124
- | `--remote` | `-r` | Lists all terraform versions available remotely, on terraform server |
125
-
126
139
  - #### _REMOVE_
127
140
 
141
+ Remove terraform versions managed by tfv
142
+
128
143
  ```sh
129
- tfv remove <version>
144
+ tfv remove <versions>
130
145
  ```
131
146
 
132
147
  Run with alias
133
148
 
134
149
  ```sh
135
- tfv rm <version>
150
+ tfv rm <versions>
136
151
  ```
137
152
 
138
- | Version | Description |
139
- | ---------------- | ----------------------------------------- |
140
- | x.x.x | remove terraform version x.x.x |
153
+ Example
154
+
155
+ ```sh
156
+ tfv rm x.y.z z.x.y
157
+ ```
141
158
 
142
159
  - #### _AUTO-SWITCH_
143
160
 
161
+ Auto-detects your project terraform version, downloads it if it's not in tfv store, and switch to the version
162
+
144
163
  ```sh
145
164
  tfv auto-switch
146
165
  ```
@@ -150,7 +169,3 @@ Run with alias
150
169
  ```sh
151
170
  tfv as
152
171
  ```
153
-
154
- | Description |
155
- | ---------------------------------------------------------------------------------------------------------------- |
156
- | auto-detects your project terraform version, downloads it if it's not in tfv store, and switch to the version |
@@ -5,17 +5,19 @@ const {install} = require('../modules/install');
5
5
 
6
6
  exports.command = 'install <version>'
7
7
  exports.aliases = ['i']
8
- exports.desc = 'Example: tfv install 1.0.11'
8
+ exports.desc = 'Install a terraform version'
9
9
  exports.builder = {
10
- 'verbose': {
11
- describe: 'Produce detailed output',
12
- alias: 'v',
13
- type: 'boolean',
10
+ arch: {
11
+ describe: 'Specify system architecture',
12
+ alias: 'a',
13
+ type: 'string',
14
+ default: ''
14
15
  }
15
16
  }
16
17
 
17
18
  exports.handler = async () => {
18
19
  const [,version] = yargs.argv._;
20
+ const {arch} = yargs.argv;
19
21
 
20
- await install(version);
22
+ await install(version, arch);
21
23
  }
@@ -5,17 +5,16 @@ const {remove} = require('../modules/remove');
5
5
 
6
6
  exports.command = 'remove <version>'
7
7
  exports.aliases = ['rm']
8
- exports.desc = 'Example: tfv rm 1.0.11'
8
+ exports.desc = 'Remove a list of versions managed by tfv'
9
9
  exports.builder = {
10
- 'verbose': {
10
+ verbose: {
11
11
  describe: 'Produce detailed output',
12
- alias: 'v',
13
12
  type: 'boolean',
14
13
  }
15
14
  }
16
15
 
17
16
  exports.handler = async () => {
18
- const [,version] = yargs.argv._;
17
+ const [,...version] = yargs.argv._;
19
18
 
20
19
  await remove(version);
21
20
  }
@@ -6,9 +6,8 @@ exports.command = 'auto-switch'
6
6
  exports.aliases = ['as']
7
7
  exports.desc = 'Example: tfv as'
8
8
  exports.builder = {
9
- 'verbose': {
9
+ verbose: {
10
10
  describe: 'Produce detailed output',
11
- alias: 'v',
12
11
  type: 'boolean'
13
12
  }
14
13
  }
@@ -7,7 +7,6 @@ exports.command = 'use <version>'
7
7
  exports.desc = 'Example: tfv use 1.0.11'
8
8
  exports.builder = {
9
9
  verbose: {
10
- alias: 'v',
11
10
  describe: 'Produce detailed output',
12
11
  type: 'boolean',
13
12
  }
@@ -1,6 +1,13 @@
1
1
  const https = require('https');
2
2
  const os = require('os');
3
- const fs = require('fs');
3
+ const {
4
+ chmodSync,
5
+ existsSync,
6
+ unlinkSync,
7
+ readFileSync,
8
+ writeFileSync,
9
+ createWriteStream
10
+ } = require('fs');
4
11
  const {join} = require('path');
5
12
  const {arch} = process;
6
13
  const unzip = require('decompress');
@@ -8,7 +15,7 @@ const {fetchAllVersions} = require('./remote');
8
15
  const {formatVersions} = require('../utils/formatVersions');
9
16
  const {P_END, P_ERROR, P_INFO, P_OK, P_WARN} = require('../utils/colors');
10
17
 
11
- exports.install = async (installVersion) => {
18
+ exports.install = async (installVersion, sysArchitecture) => {
12
19
  try {
13
20
  let version = installVersion;
14
21
  const store = join(__dirname, '../..', 'store');
@@ -36,10 +43,9 @@ exports.install = async (installVersion) => {
36
43
  process.exit(1);
37
44
  }
38
45
 
39
- const fileName = join(__dirname, '../..', `${version}.zip`);
40
46
  const getVersion = os.platform() === 'win32' ? `${version}.exe` : version;
41
47
 
42
- if (fs.existsSync(`${store}/${getVersion}`)) {
48
+ if (existsSync(`${store}/${getVersion}`)) {
43
49
  console.log(`${P_WARN}Terraform ${version} is already installed${P_END}`);
44
50
  return console.log(`To use this version Run: ${P_OK}tfv use ${version}${P_END}`)
45
51
  }
@@ -51,10 +57,22 @@ exports.install = async (installVersion) => {
51
57
  sysArch = 'amd64'
52
58
  }
53
59
 
54
- const url = `https://releases.hashicorp.com/terraform/${version}/terraform_${version}_${sysOs}_${sysArch}.zip`;
60
+ const archOption = sysArchitecture || sysArch;
61
+
62
+ const url = `https://releases.hashicorp.com/terraform/${version}/terraform_${version}_${sysOs}_${archOption}.zip`;
63
+
64
+ const manageTfArch = async (version) => {
65
+ const archMap = `${store}/arch.json`;
66
+ const archStore = readFileSync(`${archMap}`, 'utf8');
67
+ const parseArchFile = JSON.parse(archStore);
68
+ parseArchFile[version] = archOption;
69
+ writeFileSync(`${archMap}`, JSON.stringify(parseArchFile, null, 2));
70
+ }
71
+
72
+ const fileName = join(__dirname, '../..', `${version}.zip`);
55
73
 
56
- const clean = async (version) => {
57
- if (fs.existsSync(fileName)) {
74
+ const unzipFile = async (version) => {
75
+ if (existsSync(fileName)) {
58
76
  await unzip(fileName, store, {
59
77
  map: file => {
60
78
  const [, ext] = `${file.path}`.split('.');
@@ -63,13 +81,13 @@ exports.install = async (installVersion) => {
63
81
  }
64
82
  });
65
83
 
66
- fs.unlinkSync(fileName);
84
+ unlinkSync(fileName);
67
85
  }
68
86
  }
69
87
 
70
88
  const makeExecutable = async (version) => {
71
89
  if (sysOs !== 'windows') {
72
- fs.chmodSync(`${store}/${version}`, '755');
90
+ chmodSync(`${store}/${version}`, '755');
73
91
  }
74
92
  }
75
93
 
@@ -77,7 +95,7 @@ exports.install = async (installVersion) => {
77
95
  const req = https.get(url, (res) => {
78
96
  console.log(`${P_INFO}Installing terraform ${version}${P_END}`);
79
97
 
80
- const fileStream = fs.createWriteStream(fileName);
98
+ const fileStream = createWriteStream(fileName);
81
99
  res.pipe(fileStream);
82
100
 
83
101
  fileStream.on('error', (err) => {
@@ -85,7 +103,8 @@ exports.install = async (installVersion) => {
85
103
  });
86
104
 
87
105
  fileStream.on('close', async () => {
88
- await clean(version);
106
+ await unzipFile(version);
107
+ await manageTfArch(version);
89
108
  await makeExecutable(version);
90
109
  resolve(version);
91
110
  });
@@ -31,14 +31,28 @@ exports.list = async (local, remote) => {
31
31
  const pattern = /v\d+\.\d+\.\d+/;
32
32
  const versionOutput = tfVersion.stdout.match(pattern);
33
33
 
34
- versions = fs.readdirSync(store).map(f => {
35
- const versionNumber = f.replace('.exe', '');
36
-
37
- if (versionOutput && versionNumber === versionOutput[0].replace('v', '')) return `${versionNumber} 🚀`;
38
- else return versionNumber;
34
+ const archMap = `${store}/arch.json`;
35
+ const archStore = fs.readFileSync(`${archMap}`, 'utf8');
36
+ const parseArchFile = JSON.parse(archStore);
37
+ const installedVersions = [];
38
+
39
+ versions = fs.readdirSync(store).forEach(f => {
40
+ if (f !== 'arch.json') {
41
+ const versionNumber = f.replace('.exe', '');
42
+ const vnObj = {};
43
+
44
+ vnObj['Terraform Version'] = versionNumber;
45
+
46
+ if (versionOutput && versionNumber === versionOutput[0].replace('v', '')) {
47
+ vnObj['System Arch'] = `${parseArchFile[versionNumber]} 🚀`;
48
+ }
49
+ else vnObj['System Arch'] = parseArchFile[versionNumber];
50
+ installedVersions.push(vnObj);
51
+ }
39
52
  });
40
53
 
41
- return result('Terraform versions installed by tfv', versions);
54
+ console.log(`${P_OK}Terraform versions installed by tfv${P_END}`);
55
+ return console.table(installedVersions);
42
56
  }
43
57
  } catch ({message}) {
44
58
  console.log(message)
@@ -6,16 +6,24 @@ const {P_END, P_OK, P_ERROR} = require('../utils/colors');
6
6
  exports.remove = async (version) => {
7
7
  try {
8
8
  const store = join(__dirname, '../..', 'store');
9
- const tfVersion = os.platform() === 'win32' ? `${version}.exe` : version;
10
- const file = `${store}/${tfVersion}`;
9
+ const arch = JSON.parse(fs.readFileSync(`${store}/arch.json`, 'utf-8'));
11
10
 
12
- if (fs.existsSync(`${file}`)) {
13
- fs.unlinkSync(file);
14
- if (!fs.readdirSync(store).length) fs.rmdirSync(store);
15
- return console.log(`${P_OK}Terraform ${version} deleted from tfv store${P_END}`);
16
- } else {
17
- return console.log(`${P_ERROR}Terraform ${version} does not exist in tfv store${P_END}`);
18
- }
11
+ version.forEach(v => {
12
+ const tfVersion = os.platform() === 'win32' ? `${v}.exe` : v;
13
+ const file = `${store}/${tfVersion}`;
14
+
15
+ if (fs.existsSync(`${file}`)) {
16
+ fs.unlinkSync(file);
17
+
18
+ delete arch[v];
19
+
20
+ return console.log(`${P_OK}Terraform ${v} deleted from tfv store${P_END}`);
21
+ } else {
22
+ return console.log(`${P_ERROR}Terraform ${v} does not exist in tfv store${P_END}`);
23
+ }
24
+ })
25
+
26
+ fs.writeFileSync(`${store}/arch.json`, JSON.stringify(arch, null, 2));
19
27
  } catch ({message}) {
20
28
  console.log('ERROR:', message)
21
29
  }
@@ -43,7 +43,7 @@ exports.autoSwitch = async () => {
43
43
  const inStore = fs.readdirSync(store).find(v => v.startsWith(tfVersion));
44
44
 
45
45
  if (inStore) {
46
- await use(inStore);
46
+ await use(inStore.replace('.exe', ''));
47
47
  } else {
48
48
  const version = await install(`${tfVersion}^`);
49
49
  await use(version);
@@ -2,7 +2,7 @@ const fs = require('fs');
2
2
  const {P_END, P_OK, P_ERROR} = require('./colors');
3
3
 
4
4
  exports.checkStore = (store) => {
5
- if (!fs.existsSync(store)) {
5
+ if (fs.readdirSync(store).length === 1) {
6
6
  console.log(`${P_ERROR}You're yet to install terraform with tfv${P_END}`);
7
7
  console.log(`For guidance, run ${P_OK}tfv -h${P_END}`);
8
8
  process.exit(1);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tfv",
3
- "version": "3.2.4",
3
+ "version": "4.0.0",
4
4
  "description": "Terraform version manager",
5
5
  "main": "index.js",
6
6
  "directories": {
@@ -0,0 +1 @@
1
+ {}