1 | #!/usr/bin/env node
|
2 | "use strict";
|
3 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
4 | return new (P || (P = Promise))(function (resolve, reject) {
|
5 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
8 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9 | });
|
10 | };
|
11 | Object.defineProperty(exports, "__esModule", { value: true });
|
12 | const csvParser = require("csv-parser");
|
13 | const execPromise = require("exec-promise");
|
14 | const through2 = require("through2");
|
15 | const xo_lib_1 = require("xo-lib");
|
16 | const parseBoolean = (value, defaultValue) => {
|
17 | if (value === undefined || value === '') {
|
18 | return defaultValue;
|
19 | }
|
20 | const lcValue = value.toLocaleLowerCase();
|
21 | if (value === '0' || lcValue === 'false') {
|
22 | return false;
|
23 | }
|
24 | if (value === '1' || lcValue === 'true') {
|
25 | return true;
|
26 | }
|
27 | throw new Error(`invalid boolean value: ${value}`);
|
28 | };
|
29 | const requiredParam = (name) => {
|
30 | throw `missing param: ${name}
|
31 |
|
32 | Usage: xo-import-servers-csv $url $username $password < $csvFile`;
|
33 | };
|
34 | execPromise(([url = requiredParam('url'), username = requiredParam('username'), password = requiredParam('password')]) => __awaiter(this, void 0, void 0, function* () {
|
35 | const xo = new xo_lib_1.default({ url });
|
36 | yield xo.open();
|
37 | yield xo.signIn({ username, password });
|
38 | console.log('connected as', xo.user.email);
|
39 | const errors = [];
|
40 | const stream = process.stdin
|
41 | .pipe(csvParser())
|
42 | .pipe(through2.obj(({ allowUnauthorized, autoConnect, host, label, password, username, }, _, next) => {
|
43 | console.log('server', host);
|
44 | xo.call('server.add', {
|
45 | allowUnauthorized: parseBoolean(allowUnauthorized),
|
46 | autoConnect: parseBoolean(autoConnect, false),
|
47 | host,
|
48 | label,
|
49 | password,
|
50 | username
|
51 | }).then(() => next(), (error) => {
|
52 | errors.push({ host, error });
|
53 | return next();
|
54 | });
|
55 | }));
|
56 | yield new Promise((resolve, reject) => {
|
57 | stream.on('error', reject);
|
58 | stream.on('finish', resolve);
|
59 | });
|
60 | if (errors.length) {
|
61 | console.error(errors);
|
62 | }
|
63 | }));
|
64 |
|
\ | No newline at end of file |