aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tools/cli.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/tools/cli.ts')
-rw-r--r--server/tools/cli.ts72
1 files changed, 52 insertions, 20 deletions
diff --git a/server/tools/cli.ts b/server/tools/cli.ts
index 108c44218..59e9fcfc4 100644
--- a/server/tools/cli.ts
+++ b/server/tools/cli.ts
@@ -3,51 +3,83 @@ const netrc = require('netrc-parser').default
3 3
4const version = require('../../../package.json').version 4const version = require('../../../package.json').version
5 5
6let settings = {
7 remotes: [],
8 default: 0
9}
10
11interface Settings { 6interface Settings {
12 remotes: any[], 7 remotes: any[],
13 default: number 8 default: number
14} 9}
15 10
16async function getSettings () { 11function getSettings () {
17 return new Promise<Settings>((res, rej) => { 12 return new Promise<Settings>((res, rej) => {
18 let settings = { 13 const defaultSettings = {
19 remotes: [], 14 remotes: [],
20 default: 0 15 default: 0
21 } as Settings 16 }
17
22 config.read((err, data) => { 18 config.read((err, data) => {
23 if (err) { 19 if (err) return rej(err)
24 return rej(err) 20
25 } 21 return res(Object.keys(data).length === 0 ? defaultSettings : data)
26 return res(Object.keys(data).length === 0 ? settings : data)
27 }) 22 })
28 }) 23 })
29} 24}
30 25
31async function writeSettings (settings) { 26async function getNetrc () {
27 await netrc.load()
28
29 return netrc
30}
31
32function writeSettings (settings) {
32 return new Promise((res, rej) => { 33 return new Promise((res, rej) => {
33 config.write(settings, function (err) { 34 config.write(settings, function (err) {
34 if (err) { 35 if (err) return rej(err)
35 return rej(err) 36
36 }
37 return res() 37 return res()
38 }) 38 })
39 }) 39 })
40} 40}
41 41
42netrc.loadSync() 42function getRemoteObjectOrDie (program: any, settings: Settings) {
43 if (!program['url'] || !program['username'] || !program['password']) {
44 // No remote and we don't have program parameters: throw
45 if (settings.remotes.length === 0) {
46 if (!program[ 'url' ]) console.error('--url field is required.')
47 if (!program[ 'username' ]) console.error('--username field is required.')
48 if (!program[ 'password' ]) console.error('--password field is required.')
49
50 return process.exit(-1)
51 }
52
53 let url: string = program['url']
54 let username: string = program['username']
55 let password: string = program['password']
56
57 if (!url) {
58 url = settings.default !== -1
59 ? settings.remotes[settings.default]
60 : settings.remotes[0]
61 }
62
63 if (!username) username = netrc.machines[url].login
64 if (!password) password = netrc.machines[url].password
65
66 return { url, username, password }
67 }
68
69 return {
70 url: program[ 'url' ],
71 username: program[ 'username' ],
72 password: program[ 'password' ]
73 }
74}
43 75
44// --------------------------------------------------------------------------- 76// ---------------------------------------------------------------------------
45 77
46export { 78export {
47 version, 79 version,
48 config, 80 config,
49 settings,
50 getSettings, 81 getSettings,
51 writeSettings, 82 getNetrc,
52 netrc 83 getRemoteObjectOrDie,
84 writeSettings
53} 85}