diff options
Diffstat (limited to 'server/tools/cli.ts')
-rw-r--r-- | server/tools/cli.ts | 49 |
1 files changed, 35 insertions, 14 deletions
diff --git a/server/tools/cli.ts b/server/tools/cli.ts index e83a8a63c..6be558d7b 100644 --- a/server/tools/cli.ts +++ b/server/tools/cli.ts | |||
@@ -1,5 +1,12 @@ | |||
1 | const config = require('application-config')('PeerTube/CLI') | 1 | import { Netrc } from 'netrc-parser' |
2 | const netrc = require('netrc-parser').default | 2 | import { isTestInstance, getAppNumber } from '../helpers/core-utils' |
3 | import { join } from 'path' | ||
4 | import { root } from '../../shared/extra-utils' | ||
5 | |||
6 | let configName = 'PeerTube/CLI' | ||
7 | if (isTestInstance()) configName += `-${getAppNumber()}` | ||
8 | |||
9 | const config = require('application-config')(configName) | ||
3 | 10 | ||
4 | const version = require('../../../package.json').version | 11 | const version = require('../../../package.json').version |
5 | 12 | ||
@@ -12,7 +19,7 @@ function getSettings () { | |||
12 | return new Promise<Settings>((res, rej) => { | 19 | return new Promise<Settings>((res, rej) => { |
13 | const defaultSettings = { | 20 | const defaultSettings = { |
14 | remotes: [], | 21 | remotes: [], |
15 | default: 0 | 22 | default: -1 |
16 | } | 23 | } |
17 | 24 | ||
18 | config.read((err, data) => { | 25 | config.read((err, data) => { |
@@ -24,6 +31,12 @@ function getSettings () { | |||
24 | } | 31 | } |
25 | 32 | ||
26 | async function getNetrc () { | 33 | async function getNetrc () { |
34 | const Netrc = require('netrc-parser').Netrc | ||
35 | |||
36 | const netrc = isTestInstance() | ||
37 | ? new Netrc(join(root(), 'test' + getAppNumber(), 'netrc')) | ||
38 | : new Netrc() | ||
39 | |||
27 | await netrc.load() | 40 | await netrc.load() |
28 | 41 | ||
29 | return netrc | 42 | return netrc |
@@ -31,7 +44,17 @@ async function getNetrc () { | |||
31 | 44 | ||
32 | function writeSettings (settings) { | 45 | function writeSettings (settings) { |
33 | return new Promise((res, rej) => { | 46 | return new Promise((res, rej) => { |
34 | config.write(settings, function (err) { | 47 | config.write(settings, err => { |
48 | if (err) return rej(err) | ||
49 | |||
50 | return res() | ||
51 | }) | ||
52 | }) | ||
53 | } | ||
54 | |||
55 | function deleteSettings () { | ||
56 | return new Promise((res, rej) => { | ||
57 | config.trash((err) => { | ||
35 | if (err) return rej(err) | 58 | if (err) return rej(err) |
36 | 59 | ||
37 | return res() | 60 | return res() |
@@ -39,9 +62,9 @@ function writeSettings (settings) { | |||
39 | }) | 62 | }) |
40 | } | 63 | } |
41 | 64 | ||
42 | function getRemoteObjectOrDie (program: any, settings: Settings) { | 65 | function getRemoteObjectOrDie (program: any, settings: Settings, netrc: Netrc) { |
43 | if (!program['url'] || !program['username'] || !program['password']) { | 66 | if (!program['url'] || !program['username'] || !program['password']) { |
44 | // No remote and we don't have program parameters: throw | 67 | // No remote and we don't have program parameters: quit |
45 | if (settings.remotes.length === 0 || Object.keys(netrc.machines).length === 0) { | 68 | if (settings.remotes.length === 0 || Object.keys(netrc.machines).length === 0) { |
46 | if (!program[ 'url' ]) console.error('--url field is required.') | 69 | if (!program[ 'url' ]) console.error('--url field is required.') |
47 | if (!program[ 'username' ]) console.error('--username field is required.') | 70 | if (!program[ 'username' ]) console.error('--username field is required.') |
@@ -54,15 +77,12 @@ function getRemoteObjectOrDie (program: any, settings: Settings) { | |||
54 | let username: string = program['username'] | 77 | let username: string = program['username'] |
55 | let password: string = program['password'] | 78 | let password: string = program['password'] |
56 | 79 | ||
57 | if (!url) { | 80 | if (!url && settings.default !== -1) url = settings.remotes[settings.default] |
58 | url = settings.default !== -1 | ||
59 | ? settings.remotes[settings.default] | ||
60 | : settings.remotes[0] | ||
61 | } | ||
62 | 81 | ||
63 | const machine = netrc.machines[url] | 82 | const machine = netrc.machines[url] |
64 | if (!username) username = machine.login | 83 | |
65 | if (!password) password = machine.password | 84 | if (!username && machine) username = machine.login |
85 | if (!password && machine) password = machine.password | ||
66 | 86 | ||
67 | return { url, username, password } | 87 | return { url, username, password } |
68 | } | 88 | } |
@@ -82,5 +102,6 @@ export { | |||
82 | getSettings, | 102 | getSettings, |
83 | getNetrc, | 103 | getNetrc, |
84 | getRemoteObjectOrDie, | 104 | getRemoteObjectOrDie, |
85 | writeSettings | 105 | writeSettings, |
106 | deleteSettings | ||
86 | } | 107 | } |