@mytmpvpn/mytmpvpn-cli 2.0.1 → 3.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/dist/mytmpvpn.js CHANGED
@@ -9,10 +9,9 @@ const commander_1 = require("commander");
9
9
  const log = require("loglevel");
10
10
  log.setDefaultLevel("info");
11
11
  const vpnlib = require("@mytmpvpn/mytmpvpn-common/models/vpn");
12
- const peanuts = require("@mytmpvpn/mytmpvpn-common/models/peanuts");
13
12
  const appconfig_1 = require("@mytmpvpn/mytmpvpn-client/appconfig");
14
13
  const userconfig_1 = require("@mytmpvpn/mytmpvpn-client/userconfig");
15
- const auth = require("@mytmpvpn/mytmpvpn-client/auth");
14
+ const auth = require("@mytmpvpn/mytmpvpn-client/index");
16
15
  const client_1 = require("@mytmpvpn/mytmpvpn-client/client");
17
16
  const program = new commander_1.Command();
18
17
  function handleError(error, verbose = false) {
@@ -51,12 +50,12 @@ program.on('option:verbose', function () {
51
50
  log.setDefaultLevel("trace");
52
51
  });
53
52
  program.command('list-peanuts-packs')
54
- .description(`Returns the list of peanuts packs you can purchase. A peanuts pack contains a given number of peanuts. You need a minimum of ${peanuts.PEANUTS_CONFIG.min} peanuts to create a vpn`)
53
+ .description(`Returns the list of peanuts packs you can purchase. A peanuts pack contains a given number of peanuts.`)
55
54
  .action((_, command) => {
56
55
  const options = command.optsWithGlobals();
57
56
  const appConfig = (0, appconfig_1.loadAppConfig)(options.appConfig);
58
57
  // We don't need authenticated user to call this API
59
- const client = new client_1.MyTmpVpnClient(appConfig.apiUrl);
58
+ const client = new client_1.MyTmpVpnClientImpl(appConfig.apiUrl);
60
59
  client.listPeanutsPacks()
61
60
  .then(((packs) => {
62
61
  log.info(JSON.stringify(packs, null, 2));
@@ -68,16 +67,18 @@ program.command('get-peanuts-balance')
68
67
  .action((_, command) => {
69
68
  const options = command.optsWithGlobals();
70
69
  log.debug(`Get peanuts balance`);
71
- auth.getLoggedInClientFromFiles({ appConfigFile: options.appConfig, userConfigFile: options.userConfig, profileName: options.profile }, (err, client) => {
72
- if (err) {
73
- handleError(err, options.verbose);
74
- return;
75
- }
70
+ auth.getLoggedInClientFromFiles({
71
+ appConfigFile: options.appConfig,
72
+ userConfigFile: options.userConfig,
73
+ profileName: options.profile
74
+ }).then(client => {
76
75
  client.getPeanutsBalance().then(balance => {
77
76
  log.info(balance);
78
77
  }).catch(err => {
79
78
  handleError(err);
80
79
  });
80
+ }).catch(err => {
81
+ handleError(err, options.verbose);
81
82
  });
82
83
  });
83
84
  program.command('list-regions')
@@ -86,7 +87,7 @@ program.command('list-regions')
86
87
  const options = command.optsWithGlobals();
87
88
  const appConfig = (0, appconfig_1.loadAppConfig)(options.appConfig);
88
89
  // We don't need authenticated user to call this API
89
- const client = new client_1.MyTmpVpnClient(appConfig.apiUrl);
90
+ const client = new client_1.MyTmpVpnClientImpl(appConfig.apiUrl);
90
91
  client.listRegions()
91
92
  .then(((regions) => {
92
93
  log.info(JSON.stringify(regions, null, 2));
@@ -99,7 +100,7 @@ program.command('list-regions-detailed')
99
100
  const options = command.optsWithGlobals();
100
101
  const appConfig = (0, appconfig_1.loadAppConfig)(options.appConfig);
101
102
  // We don't need authenticated user to call this API
102
- const client = new client_1.MyTmpVpnClient(appConfig.apiUrl);
103
+ const client = new client_1.MyTmpVpnClientImpl(appConfig.apiUrl);
103
104
  client.listRegionsDetailed()
104
105
  .then(((regions) => {
105
106
  log.info(JSON.stringify(regions, null, 2));
@@ -120,12 +121,15 @@ program.command('create')
120
121
  const syncStr = options.sync ? "synchronously" : "asynchronously";
121
122
  const deleteAfter = options.deleteAfter ? options.deleteAfter : undefined;
122
123
  log.debug(`Creating new ${options.type} vpn into ${region} ${syncStr}`);
123
- auth.getLoggedInClientFromFiles({ appConfigFile: options.appConfig, userConfigFile: options.userConfig, profileName: options.profile }, (err, client) => {
124
- if (err) {
125
- handleError(err, options.verbose);
126
- return;
127
- }
128
- client.createVpn(region, { type: options.type, maxPeanuts: options.peanuts, deleteAfter }).then(vpn => {
124
+ auth.getLoggedInClientFromFiles({
125
+ appConfigFile: options.appConfig,
126
+ userConfigFile: options.userConfig,
127
+ profileName: options.profile
128
+ }).then(client => {
129
+ client.createVpn(region, {
130
+ type: options.type,
131
+ maxPeanuts: options.peanuts, deleteAfter
132
+ }).then(vpn => {
129
133
  if (!options.sync) {
130
134
  log.info(vpn);
131
135
  return;
@@ -138,6 +142,8 @@ program.command('create')
138
142
  }).catch(err => {
139
143
  handleError(err);
140
144
  });
145
+ }).catch(err => {
146
+ handleError(err, options.verbose);
141
147
  });
142
148
  });
143
149
  program.command('delete')
@@ -148,17 +154,18 @@ program.command('delete')
148
154
  const options = command.optsWithGlobals();
149
155
  const syncStr = options.sync ? "synchronously" : "asynchronously";
150
156
  log.debug(`Deleting vpn ${vpnId} ${syncStr}`);
151
- auth.getLoggedInClientFromFiles({ appConfigFile: options.appConfig, userConfigFile: options.userConfig, profileName: options.profile }, (err, client) => {
152
- if (err) {
153
- handleError(err.message || JSON.stringify(err));
154
- return;
155
- }
157
+ auth.getLoggedInClientFromFiles({
158
+ appConfigFile: options.appConfig,
159
+ userConfigFile: options.userConfig,
160
+ profileName: options.profile
161
+ }).then(client => {
156
162
  client.deleteVpn(vpnId).then(vpn => {
157
163
  if (!options.sync) {
158
164
  log.info(JSON.stringify(vpn));
159
165
  return;
160
166
  }
161
- client.waitUntilVpnStateIs(vpnId, vpnlib.VpnState.Deleted).then(updatedVpn => {
167
+ client.waitUntilVpnStateIs(vpnId, vpnlib.VpnState.Deleted)
168
+ .then(updatedVpn => {
162
169
  log.info(updatedVpn);
163
170
  return;
164
171
  }).catch(err => {
@@ -174,14 +181,16 @@ program.command('get')
174
181
  .argument('<vpnId>', 'vpnId to get information from')
175
182
  .action((vpnId, _, command) => {
176
183
  const options = command.optsWithGlobals();
177
- auth.getLoggedInClientFromFiles({ appConfigFile: options.appConfig, userConfigFile: options.userConfig, profileName: options.profile }, (err, client) => {
178
- if (err) {
179
- handleError(err);
180
- return;
181
- }
184
+ auth.getLoggedInClientFromFiles({
185
+ appConfigFile: options.appConfig,
186
+ userConfigFile: options.userConfig,
187
+ profileName: options.profile
188
+ }).then(client => {
182
189
  client.getVpn(vpnId)
183
190
  .then(result => log.info(result))
184
191
  .catch(err => handleError(err));
192
+ }).catch(err => {
193
+ handleError(err);
185
194
  });
186
195
  });
187
196
  program.command('download-config')
@@ -193,11 +202,11 @@ program.command('download-config')
193
202
  const options = command.optsWithGlobals();
194
203
  const file = options.file ? options.file : `${vpnId}.conf`;
195
204
  const fullpath = path.join(options.path, file);
196
- auth.getLoggedInClientFromFiles({ appConfigFile: options.appConfig, userConfigFile: options.userConfig, profileName: options.profile }, (err, client) => {
197
- if (err) {
198
- handleError(err);
199
- return;
200
- }
205
+ auth.getLoggedInClientFromFiles({
206
+ appConfigFile: options.appConfig,
207
+ userConfigFile: options.userConfig,
208
+ profileName: options.profile
209
+ }).then(client => {
201
210
  client.getVpnConfig(vpnId)
202
211
  .then(b64 => {
203
212
  const vpnConfig = Buffer.from(b64, 'base64');
@@ -211,24 +220,38 @@ program.command('download-config')
211
220
  log.info(`Config file written to ${fullpath}`);
212
221
  });
213
222
  }).catch(err => handleError(err));
223
+ }).catch(err => {
224
+ handleError(err);
214
225
  });
215
226
  });
216
227
  program.command('list')
217
228
  .description('List all vpns')
218
229
  .option('--region <region>', 'region to list vpns from')
219
- .option('--exclude-state <state>', 'state to exclude from the list', 'DELETED')
230
+ .option('--include-state [<state>, ...]', 'Comma separated list of states to include', Object.values(vpnlib.VpnState))
231
+ .option('--exclude-state [<state>, ...]', 'Comma separated list of states to exclude from', [vpnlib.VpnState.Deleted])
220
232
  .action((_, command) => {
221
233
  const options = command.optsWithGlobals();
222
- auth.getLoggedInClientFromFiles({ appConfigFile: options.appConfig, userConfigFile: options.userConfig, profileName: options.profile }, (err, client) => {
223
- if (err) {
224
- handleError(err);
225
- return;
226
- }
234
+ auth.getLoggedInClientFromFiles({
235
+ appConfigFile: options.appConfig,
236
+ userConfigFile: options.userConfig,
237
+ profileName: options.profile
238
+ }).then(client => {
227
239
  client.listVpns(options.state)
228
240
  .then(vpns => {
241
+ if (options.region) {
242
+ vpns = vpns.filter((vpn) => vpn.region === options.region);
243
+ }
244
+ if (options.includeState) {
245
+ vpns = vpns.filter((vpn) => options.includeState.includes(vpn.state));
246
+ }
247
+ if (options.excludeState) {
248
+ vpns = vpns.filter((vpn) => !options.excludeState.includes(vpn.state));
249
+ }
229
250
  log.info(JSON.stringify(vpns, null, 2));
230
251
  })
231
252
  .catch(err => handleError(err));
253
+ }).catch(err => {
254
+ handleError(err);
232
255
  });
233
256
  });
234
257
  program.command('wait')
@@ -243,16 +266,18 @@ program.command('wait')
243
266
  return;
244
267
  }
245
268
  log.debug(`Waiting for ${vpnId} state to be (at least) ${state}`);
246
- auth.getLoggedInClientFromFiles({ appConfigFile: options.appConfig, userConfigFile: options.userConfig, profileName: options.profile }, (err, client) => {
247
- if (err) {
248
- handleError(err);
249
- return;
250
- }
269
+ auth.getLoggedInClientFromFiles({
270
+ appConfigFile: options.appConfig,
271
+ userConfigFile: options.userConfig,
272
+ profileName: options.profile
273
+ }).then(client => {
251
274
  client.waitUntilVpnStateIs(vpnId, actualState)
252
275
  .then(vpn => {
253
276
  log.info(vpn);
254
277
  })
255
278
  .catch(err => handleError(err));
279
+ }).catch(err => {
280
+ handleError(err);
256
281
  });
257
282
  });
258
283
  program.command('register')
@@ -284,11 +309,8 @@ program.command('register')
284
309
  }
285
310
  };
286
311
  }
287
- auth.registerUserFromFiles(options.appConfig, username, password, (err, result) => {
288
- if (err) {
289
- handleError(err);
290
- return;
291
- }
312
+ auth.registerUser(options.appConfig, { username, password })
313
+ .then(user => {
292
314
  log.info(`Please confirm your identity with the code sent to ${username}`);
293
315
  fs.mkdir(path.dirname(options.userConfig), { recursive: true }, (err, path) => {
294
316
  if (err) {
@@ -304,6 +326,8 @@ program.command('register')
304
326
  });
305
327
  log.info(`A new profile has been created for ${options.profile} in ${options.userConfig}`);
306
328
  });
329
+ }).catch(err => {
330
+ handleError(err);
307
331
  });
308
332
  });
309
333
  program.command('confirm-registration')
@@ -312,13 +336,14 @@ program.command('confirm-registration')
312
336
  .argument('<code>', 'the code received')
313
337
  .action((username, code, _, command) => {
314
338
  const options = command.optsWithGlobals();
315
- auth.confirmUserFromFiles(options.appConfig, username, code, (err, result) => {
316
- if (err) {
317
- handleError(err);
318
- return;
319
- }
339
+ auth
340
+ .confirmUser(options.appConfig, username, code)
341
+ .then(result => {
320
342
  log.debug(result);
321
343
  log.info(`User ${username} confirmed!`);
344
+ })
345
+ .catch(err => {
346
+ handleError(err);
322
347
  });
323
348
  });
324
349
  program.addHelpText('after', `
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mytmpvpn/mytmpvpn-cli",
3
- "version": "2.0.1",
3
+ "version": "3.0.0",
4
4
  "description": "MyTmpVpn CLI",
5
5
  "main": "./dist/mytmpvpn.js",
6
6
  "bin": {
@@ -11,11 +11,14 @@
11
11
  "build": "tsc --build && chmod +x dist/mytmpvpn.js",
12
12
  "lint": "eslint",
13
13
  "watch": "tsc -w",
14
- "test": "jest",
14
+ "test": "./dist/mytmpvpn.js --help",
15
15
  "clean": "rm -rf dist/",
16
16
  "link-deps": "(cd ../mytmpvpn-common && npm link) && (cd ../mytmpvpn-client && npm link) && npm link ../mytmpvpn-common ../mytmpvpn-client",
17
17
  "build-all": "./build-all-dependencies",
18
- "upgrade": "ncu --target semver --upgrade"
18
+ "upgrade": "ncu --target semver --upgrade",
19
+ "prepublishOnly": "npm ci && npm run build && npm run test",
20
+ "version": "git add -A",
21
+ "postversion": "git push && git push --tags && npm publish"
19
22
  },
20
23
  "keywords": [
21
24
  "VPN"
@@ -27,8 +30,8 @@
27
30
  },
28
31
  "license": "GNU GPL",
29
32
  "dependencies": {
30
- "@mytmpvpn/mytmpvpn-client": "^2.4.2",
31
- "@mytmpvpn/mytmpvpn-common": "^4.0.1",
33
+ "@mytmpvpn/mytmpvpn-client": "^4.0.1",
34
+ "@mytmpvpn/mytmpvpn-common": "^6.0.2",
32
35
  "@typescript-eslint/eslint-plugin": "^6.21.0",
33
36
  "commander": "^9.5.0",
34
37
  "eslint": "^8.57.1",
@@ -40,9 +43,9 @@
40
43
  },
41
44
  "devDependencies": {
42
45
  "@types/jest": "^29.5.14",
43
- "@types/node": "^18.19.68",
46
+ "@types/node": "^18.19.100",
44
47
  "jest": "^29.7.0",
45
- "ts-jest": "^29.2.5",
48
+ "ts-jest": "^29.3.4",
46
49
  "typescript": "^4.9.5"
47
50
  }
48
51
  }
package/src/mytmpvpn.ts CHANGED
@@ -11,8 +11,8 @@ import * as vpnlib from '@mytmpvpn/mytmpvpn-common/models/vpn'
11
11
  import * as peanuts from '@mytmpvpn/mytmpvpn-common/models/peanuts'
12
12
  import { getDefaultAppConfigFile, loadAppConfig } from '@mytmpvpn/mytmpvpn-client/appconfig'
13
13
  import { getDefaultUserConfigFile, getDefaultUserProfile, getDefaultUserConfigDir, loadUserConfig, UserConfig } from '@mytmpvpn/mytmpvpn-client/userconfig'
14
- import * as auth from '@mytmpvpn/mytmpvpn-client/auth'
15
- import { MyTmpVpnClient } from '@mytmpvpn/mytmpvpn-client/client'
14
+ import * as auth from '@mytmpvpn/mytmpvpn-client/index'
15
+ import { MyTmpVpnClientImpl } from '@mytmpvpn/mytmpvpn-client/client'
16
16
 
17
17
  const program = new Command()
18
18
 
@@ -53,12 +53,12 @@ program.on('option:verbose', function () {
53
53
  })
54
54
 
55
55
  program.command('list-peanuts-packs')
56
- .description(`Returns the list of peanuts packs you can purchase. A peanuts pack contains a given number of peanuts. You need a minimum of ${peanuts.PEANUTS_CONFIG.min} peanuts to create a vpn`)
56
+ .description(`Returns the list of peanuts packs you can purchase. A peanuts pack contains a given number of peanuts.`)
57
57
  .action((_, command) => {
58
58
  const options = command.optsWithGlobals()
59
59
  const appConfig = loadAppConfig(options.appConfig)
60
60
  // We don't need authenticated user to call this API
61
- const client = new MyTmpVpnClient(appConfig.apiUrl)
61
+ const client = new MyTmpVpnClientImpl(appConfig.apiUrl)
62
62
  client.listPeanutsPacks()
63
63
  .then(((packs: peanuts.PeanutsPack[]) => {
64
64
  log.info(JSON.stringify(packs, null, 2))
@@ -71,16 +71,18 @@ program.command('get-peanuts-balance')
71
71
  .action((_, command) => {
72
72
  const options = command.optsWithGlobals()
73
73
  log.debug(`Get peanuts balance`)
74
- auth.getLoggedInClientFromFiles({ appConfigFile: options.appConfig, userConfigFile: options.userConfig, profileName: options.profile }, (err, client) => {
75
- if (err) {
76
- handleError(err, options.verbose)
77
- return
78
- }
74
+ auth.getLoggedInClientFromFiles({
75
+ appConfigFile: options.appConfig,
76
+ userConfigFile: options.userConfig,
77
+ profileName: options.profile
78
+ }).then(client => {
79
79
  client.getPeanutsBalance().then(balance => {
80
80
  log.info(balance)
81
81
  }).catch(err => {
82
82
  handleError(err)
83
83
  })
84
+ }).catch(err => {
85
+ handleError(err, options.verbose)
84
86
  })
85
87
  })
86
88
 
@@ -90,7 +92,7 @@ program.command('list-regions')
90
92
  const options = command.optsWithGlobals()
91
93
  const appConfig = loadAppConfig(options.appConfig)
92
94
  // We don't need authenticated user to call this API
93
- const client = new MyTmpVpnClient(appConfig.apiUrl)
95
+ const client = new MyTmpVpnClientImpl(appConfig.apiUrl)
94
96
  client.listRegions()
95
97
  .then(((regions: any) => {
96
98
  log.info(JSON.stringify(regions, null, 2))
@@ -104,7 +106,7 @@ program.command('list-regions-detailed')
104
106
  const options = command.optsWithGlobals()
105
107
  const appConfig = loadAppConfig(options.appConfig)
106
108
  // We don't need authenticated user to call this API
107
- const client = new MyTmpVpnClient(appConfig.apiUrl)
109
+ const client = new MyTmpVpnClientImpl(appConfig.apiUrl)
108
110
  client.listRegionsDetailed()
109
111
  .then(((regions: any) => {
110
112
  log.info(JSON.stringify(regions, null, 2))
@@ -124,14 +126,17 @@ program.command('create')
124
126
  .action((region: string, _, command) => {
125
127
  const options = command.optsWithGlobals()
126
128
  const syncStr = options.sync ? "synchronously" : "asynchronously"
127
- const deleteAfter = options.deleteAfter ? options.deleteAfter : undefined
129
+ const deleteAfter = options.deleteAfter ? options.deleteAfter : undefined
128
130
  log.debug(`Creating new ${options.type} vpn into ${region} ${syncStr}`)
129
- auth.getLoggedInClientFromFiles({ appConfigFile: options.appConfig, userConfigFile: options.userConfig, profileName: options.profile }, (err, client) => {
130
- if (err) {
131
- handleError(err, options.verbose)
132
- return
133
- }
134
- client.createVpn(region, { type: options.type, maxPeanuts: options.peanuts, deleteAfter }).then(vpn => {
131
+ auth.getLoggedInClientFromFiles({
132
+ appConfigFile: options.appConfig,
133
+ userConfigFile: options.userConfig,
134
+ profileName: options.profile
135
+ }).then(client => {
136
+ client.createVpn(region, {
137
+ type: options.type,
138
+ maxPeanuts: options.peanuts, deleteAfter
139
+ }).then(vpn => {
135
140
  if (!options.sync) {
136
141
  log.info(vpn)
137
142
  return
@@ -144,10 +149,11 @@ program.command('create')
144
149
  }).catch(err => {
145
150
  handleError(err)
146
151
  })
152
+ }).catch(err => {
153
+ handleError(err, options.verbose)
147
154
  })
148
155
  })
149
156
 
150
-
151
157
  program.command('delete')
152
158
  .description('Delete a vpn')
153
159
  .argument('<vpnId>', 'vpnId to delete')
@@ -156,22 +162,23 @@ program.command('delete')
156
162
  const options = command.optsWithGlobals()
157
163
  const syncStr = options.sync ? "synchronously" : "asynchronously"
158
164
  log.debug(`Deleting vpn ${vpnId} ${syncStr}`)
159
- auth.getLoggedInClientFromFiles({ appConfigFile: options.appConfig, userConfigFile: options.userConfig, profileName: options.profile }, (err, client) => {
160
- if (err) {
161
- handleError(err.message || JSON.stringify(err))
162
- return
163
- }
165
+ auth.getLoggedInClientFromFiles({
166
+ appConfigFile: options.appConfig,
167
+ userConfigFile: options.userConfig,
168
+ profileName: options.profile
169
+ }).then(client => {
164
170
  client.deleteVpn(vpnId).then(vpn => {
165
171
  if (!options.sync) {
166
172
  log.info(JSON.stringify(vpn))
167
173
  return
168
174
  }
169
- client.waitUntilVpnStateIs(vpnId, vpnlib.VpnState.Deleted).then(updatedVpn => {
170
- log.info(updatedVpn)
171
- return
172
- }).catch(err => {
173
- handleError(err.response?.data || JSON.stringify(err))
174
- })
175
+ client.waitUntilVpnStateIs(vpnId, vpnlib.VpnState.Deleted)
176
+ .then(updatedVpn => {
177
+ log.info(updatedVpn)
178
+ return
179
+ }).catch(err => {
180
+ handleError(err.response?.data || JSON.stringify(err))
181
+ })
175
182
  }).catch(err => {
176
183
  handleError(err.response?.data || JSON.stringify(err))
177
184
  })
@@ -183,14 +190,16 @@ program.command('get')
183
190
  .argument('<vpnId>', 'vpnId to get information from')
184
191
  .action((vpnId, _, command) => {
185
192
  const options = command.optsWithGlobals()
186
- auth.getLoggedInClientFromFiles({ appConfigFile: options.appConfig, userConfigFile: options.userConfig, profileName: options.profile }, (err, client) => {
187
- if (err) {
188
- handleError(err)
189
- return
190
- }
193
+ auth.getLoggedInClientFromFiles({
194
+ appConfigFile: options.appConfig,
195
+ userConfigFile: options.userConfig,
196
+ profileName: options.profile
197
+ }).then(client => {
191
198
  client.getVpn(vpnId)
192
199
  .then(result => log.info(result))
193
200
  .catch(err => handleError(err))
201
+ }).catch(err => {
202
+ handleError(err)
194
203
  })
195
204
  })
196
205
 
@@ -203,11 +212,11 @@ program.command('download-config')
203
212
  const options = command.optsWithGlobals()
204
213
  const file = options.file ? options.file : `${vpnId}.conf`
205
214
  const fullpath = path.join(options.path, file)
206
- auth.getLoggedInClientFromFiles({ appConfigFile: options.appConfig, userConfigFile: options.userConfig, profileName: options.profile }, (err, client) => {
207
- if (err) {
208
- handleError(err)
209
- return
210
- }
215
+ auth.getLoggedInClientFromFiles({
216
+ appConfigFile: options.appConfig,
217
+ userConfigFile: options.userConfig,
218
+ profileName: options.profile
219
+ }).then(client => {
211
220
  client.getVpnConfig(vpnId)
212
221
  .then(b64 => {
213
222
  const vpnConfig = Buffer.from(b64, 'base64')
@@ -221,25 +230,39 @@ program.command('download-config')
221
230
  log.info(`Config file written to ${fullpath}`)
222
231
  })
223
232
  }).catch(err => handleError(err))
233
+ }).catch(err => {
234
+ handleError(err)
224
235
  })
225
236
  })
226
237
 
227
238
  program.command('list')
228
239
  .description('List all vpns')
229
240
  .option('--region <region>', 'region to list vpns from')
230
- .option('--exclude-state <state>', 'state to exclude from the list', 'DELETED')
241
+ .option('--include-state [<state>, ...]', 'Comma separated list of states to include', Object.values(vpnlib.VpnState))
242
+ .option('--exclude-state [<state>, ...]', 'Comma separated list of states to exclude from', [vpnlib.VpnState.Deleted])
231
243
  .action((_, command) => {
232
244
  const options = command.optsWithGlobals()
233
- auth.getLoggedInClientFromFiles({ appConfigFile: options.appConfig, userConfigFile: options.userConfig, profileName: options.profile }, (err, client) => {
234
- if (err) {
235
- handleError(err)
236
- return
237
- }
245
+ auth.getLoggedInClientFromFiles({
246
+ appConfigFile: options.appConfig,
247
+ userConfigFile: options.userConfig,
248
+ profileName: options.profile
249
+ }).then(client => {
238
250
  client.listVpns(options.state)
239
251
  .then(vpns => {
252
+ if (options.region) {
253
+ vpns = vpns.filter((vpn: vpnlib.Vpn) => vpn.region === options.region)
254
+ }
255
+ if (options.includeState) {
256
+ vpns = vpns.filter((vpn: vpnlib.Vpn) => options.includeState.includes(vpn.state))
257
+ }
258
+ if (options.excludeState) {
259
+ vpns = vpns.filter((vpn: vpnlib.Vpn) => !options.excludeState.includes(vpn.state))
260
+ }
240
261
  log.info(JSON.stringify(vpns, null, 2))
241
262
  })
242
263
  .catch(err => handleError(err))
264
+ }).catch(err => {
265
+ handleError(err)
243
266
  })
244
267
  })
245
268
 
@@ -255,16 +278,18 @@ program.command('wait')
255
278
  return
256
279
  }
257
280
  log.debug(`Waiting for ${vpnId} state to be (at least) ${state}`)
258
- auth.getLoggedInClientFromFiles({ appConfigFile: options.appConfig, userConfigFile: options.userConfig, profileName: options.profile }, (err, client) => {
259
- if (err) {
260
- handleError(err)
261
- return
262
- }
281
+ auth.getLoggedInClientFromFiles({
282
+ appConfigFile: options.appConfig,
283
+ userConfigFile: options.userConfig,
284
+ profileName: options.profile
285
+ }).then(client => {
263
286
  client.waitUntilVpnStateIs(vpnId, actualState)
264
287
  .then(vpn => {
265
288
  log.info(vpn)
266
289
  })
267
290
  .catch(err => handleError(err))
291
+ }).catch(err => {
292
+ handleError(err)
268
293
  })
269
294
  })
270
295
 
@@ -296,43 +321,45 @@ program.command('register')
296
321
  }
297
322
  }
298
323
  }
299
- auth.registerUserFromFiles(options.appConfig, username, password, (err, result) => {
300
- if (err) {
301
- handleError(err)
302
- return
303
- }
304
- log.info(`Please confirm your identity with the code sent to ${username}`)
305
- fs.mkdir(path.dirname(options.userConfig), { recursive: true }, (err, path?) => {
306
- if (err) {
307
- handleError(err)
308
- return
309
- }
310
- log.debug(`Directory: ${path} created`)
311
- fs.writeFile(options.userConfig, JSON.stringify(userConfig, null, 2), (err) => {
324
+ auth.registerUser(options.appConfig,
325
+ { username, password })
326
+ .then(user => {
327
+ log.info(`Please confirm your identity with the code sent to ${username}`)
328
+ fs.mkdir(path.dirname(options.userConfig), { recursive: true }, (err, path?) => {
312
329
  if (err) {
313
330
  handleError(err)
314
331
  return
315
332
  }
333
+ log.debug(`Directory: ${path} created`)
334
+ fs.writeFile(options.userConfig, JSON.stringify(userConfig, null, 2), (err) => {
335
+ if (err) {
336
+ handleError(err)
337
+ return
338
+ }
339
+ })
340
+ log.info(`A new profile has been created for ${options.profile} in ${options.userConfig}`)
316
341
  })
317
- log.info(`A new profile has been created for ${options.profile} in ${options.userConfig}`)
342
+ }).catch(err => {
343
+ handleError(err)
318
344
  })
319
- })
320
345
  })
321
346
 
347
+
322
348
  program.command('confirm-registration')
323
349
  .description('Confirm registration to the MyTmpVpn application using the code that was sent to email/phone')
324
350
  .argument('<username>', 'the email/phone that identify your account')
325
351
  .argument('<code>', 'the code received')
326
352
  .action((username, code, _, command) => {
327
353
  const options = command.optsWithGlobals()
328
- auth.confirmUserFromFiles(options.appConfig, username, code, (err, result) => {
329
- if (err) {
354
+ auth
355
+ .confirmUser(options.appConfig, username, code)
356
+ .then(result => {
357
+ log.debug(result)
358
+ log.info(`User ${username} confirmed!`)
359
+ })
360
+ .catch(err => {
330
361
  handleError(err)
331
- return
332
- }
333
- log.debug(result)
334
- log.info(`User ${username} confirmed!`)
335
- })
362
+ })
336
363
  })
337
364
 
338
365
  program.addHelpText('after', `