Skip to content

Commit b74abdb

Browse files
committed
introduce subcommands scripts (sv & sav)
+ put convenience functions in a separate file (utils.js)
1 parent b3370fe commit b74abdb

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed

src/glci-sav.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env node
2+
3+
import program from 'commander';
4+
import { getConf, getProperties } from './lib/utils';
5+
import gitlabCI from './lib/gitlab-ci';
6+
7+
async function execute(cmd) {
8+
const conf = await getConf();
9+
10+
const properties = getProperties();
11+
const handler = gitlabCI(conf.url, conf.token);
12+
const resp = await handler.setVariables(properties, !cmd.doNotForce);
13+
14+
console.log('Completed setting variables on Gitlab CI.');
15+
return resp;
16+
}
17+
18+
program
19+
.description('Read all key/value pairs under gitlab.env.yml on the current directory and sets them as environment variables on Gitlab CI')
20+
.option(
21+
'--url <url>',
22+
'Your Gitlab project URL, e.g. https://gitlab.com/gitlab-org/gitlab-ce',
23+
)
24+
.option(
25+
'--token <token>',
26+
'Your Gitlab token.',
27+
)
28+
.option(
29+
'--do-not-force',
30+
'Ignore variables if they already exist on gitlab CI. By default all variables are overridden',
31+
)
32+
.parse(process.argv);
33+
34+
execute(program);

src/glci-sv.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env node
2+
3+
import program from 'commander';
4+
import { getConf } from './lib/utils';
5+
import gitlabCI from './lib/gitlab-ci';
6+
7+
async function execute(cmd) {
8+
let resp = null;
9+
const conf = await getConf();
10+
11+
if (!conf.key || !conf.value) {
12+
console.error('No key/value pair given.');
13+
process.exit(1);
14+
}
15+
16+
const handler = gitlabCI(conf.url, conf.token);
17+
18+
const existingKeys = (await handler.listVariables()).map(variable => variable.key);
19+
20+
const keyExists = existingKeys.includes(conf.key);
21+
22+
if (keyExists && cmd.doNotForce) {
23+
console.log(`Skipping ${conf.key}, already set.`);
24+
return undefined;
25+
}
26+
27+
if (keyExists) {
28+
resp = await handler.updateVariable(conf.key, conf.value);
29+
} else {
30+
resp = await handler.createVariable(conf.key, conf.value);
31+
}
32+
33+
console.log('Completed setting variable on Gitlab CI.');
34+
return resp;
35+
}
36+
37+
program
38+
.description('Set given key/value pair as environment variables on Gitlab CI')
39+
.option(
40+
'--url <url>',
41+
'Your Gitlab project URL, e.g. https://gitlab.com/gitlab-org/gitlab-ce',
42+
)
43+
.option(
44+
'--token <token>',
45+
'Your Gitlab token.',
46+
)
47+
.option(
48+
'--key <key>',
49+
'Your Gitlab CI variable.',
50+
)
51+
.option(
52+
'--value <value>',
53+
'Your Gitlab CI value.',
54+
)
55+
.option(
56+
'--do-not-force',
57+
'Ignore variable if it already exists on gitlab CI. By default variable is overridden',
58+
)
59+
.parse(process.argv);
60+
61+
execute(program);

src/lib/utils.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import rc from 'rc';
2+
import fs from 'fs';
3+
import gitRemoteOriginUrl from 'git-remote-origin-url';
4+
import getUrlFromGitRemote from './git';
5+
import loadPropertiesFile from './properties-file';
6+
7+
const gitlabEnvFileName = 'gitlab.env.yml';
8+
9+
async function getConf() {
10+
const conf = rc('gitlab');
11+
const errors = [];
12+
13+
// Check for token
14+
if (!conf.token) {
15+
if (!process.env.GITLAB_TOKEN) {
16+
errors.push('No Gitlab token given.');
17+
} else {
18+
conf.token = process.env.GITLAB_TOKEN;
19+
console.log('Using token from environment variable GITLAB_TOKEN.');
20+
}
21+
}
22+
23+
if (conf.config) {
24+
console.log(`Using token from ${conf.config}.`);
25+
}
26+
27+
// If there is no url provided, get it!
28+
if (!conf.url) {
29+
try {
30+
conf.url = await getUrlFromGitRemote(gitRemoteOriginUrl);
31+
console.log('No URL specified, using git remote `origin`.');
32+
} catch (err) {
33+
errors.push('No Gitlab project URL given.');
34+
}
35+
}
36+
37+
if (errors.length > 0) {
38+
console.error(errors.join('\n'));
39+
process.exit(1);
40+
}
41+
42+
return conf;
43+
}
44+
45+
function getProperties() {
46+
const cwd = process.cwd();
47+
const path = `${cwd}/${gitlabEnvFileName}`;
48+
49+
if (!fs.existsSync(path)) {
50+
console.error(`${gitlabEnvFileName} does not exist`);
51+
process.exit(1);
52+
}
53+
54+
return loadPropertiesFile(path);
55+
}
56+
57+
export {
58+
getConf,
59+
getProperties,
60+
};

0 commit comments

Comments
 (0)