-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
cli.js
101 lines (86 loc) · 2.9 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* eslint-disable no-console */
const debug = require('debug');
const ora = require('ora');
const { isValidNamespace, isValidName, isValidVersion, makeFileList, makeTarball } = require('../cli-helpers');
const tfModule = require('./module');
const owner = process.env.CITIZEN_MODULE_OWNER || '';
module.exports = async (namespace, name, provider, version, cmd) => {
const verbose = debug('citizen:client');
if (cmd.verbose) {
debug.enable('citizen:client');
}
const registryAddr = cmd.registry || process.env.CITIZEN_ADDR;
if (!registryAddr) {
console.error('Registry address must be specified.(with --registry or CITIZEN_ADDR)');
process.exit(1);
}
verbose(`Target citizen registry server: ${registryAddr}`);
if (!isValidNamespace(namespace)) {
console.error(
'The namespace only contain alphanumeric characters or single hyphens, and cannot begin or end with a hyphen.'
);
process.exit(1);
}
if (!isValidName(name)) {
console.error(
'The name only contain alphanumeric characters or single hyphens, and cannot begin or end with a hyphen.'
);
process.exit(1);
}
if (!isValidName(provider)) {
console.error(
'The provider only contain alphanumeric characters or single hyphens, and cannot begin or end with a hyphen.'
);
process.exit(1);
}
// It must be semantic version.
const moduleversion = isValidVersion(version);
if (!moduleversion) {
console.error(
'The version must be a semantic version that can optionally be prefixed with a v. Examples are v1.0.4 and 0.9.2.'
);
process.exit(1);
}
const targetDir = process.cwd();
const moduleFullPath = `${namespace}/${name}/${provider}/${moduleversion}`;
const spinner = ora();
// compressing step
let tarball;
try {
spinner.text = 'compress the terraform module';
spinner.color = 'yellow';
spinner.start();
const files = await makeFileList(targetDir);
verbose('files to compress: %O', files);
tarball = await makeTarball(targetDir, files);
spinner.succeed();
} catch (err) {
spinner.fail();
console.error(err);
process.exit(1);
}
// publishing step
try {
spinner.text = `publish ${moduleFullPath}`;
spinner.color = 'yellow';
spinner.start();
const res = await tfModule.publish(registryAddr, moduleFullPath, tarball, owner);
verbose(`response status code form registry: ${res.statusCode}`);
if (res.statusCode >= 400) {
const { errors } = JSON.parse(res.body);
spinner.fail();
console.error(`The registry server is something wrong: ${errors.map((msg) => `\n${msg}`)}`);
process.exit(1);
}
spinner.succeed();
} catch (err) {
if (err.code === 'ECONNREFUSED') {
spinner.fail();
console.error("The registry server doesn't response. Please check the registry.");
process.exit(1);
}
spinner.fail();
console.error(err);
process.exit(1);
}
};