aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tools/cli.ts
blob: 67755022c182f4c7ae173cfe7e6106e0fd06d7e6 (plain) (blame)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { Netrc } from 'netrc-parser'
import { getAppNumber, isTestInstance } from '../helpers/core-utils'
import { join } from 'path'
import { root } from '../../shared/extra-utils/miscs/miscs'
import { getVideoChannel } from '../../shared/extra-utils/videos/video-channels'
import { Command } from 'commander'
import { VideoChannel, VideoPrivacy } from '../../shared/models/videos'

let configName = 'PeerTube/CLI'
if (isTestInstance()) configName += `-${getAppNumber()}`

const config = require('application-config')(configName)

const version = require('../../../package.json').version

interface Settings {
  remotes: any[],
  default: number
}

function getSettings () {
  return new Promise<Settings>((res, rej) => {
    const defaultSettings = {
      remotes: [],
      default: -1
    }

    config.read((err, data) => {
      if (err) return rej(err)

      return res(Object.keys(data).length === 0 ? defaultSettings : data)
    })
  })
}

async function getNetrc () {
  const Netrc = require('netrc-parser').Netrc

  const netrc = isTestInstance()
    ? new Netrc(join(root(), 'test' + getAppNumber(), 'netrc'))
    : new Netrc()

  await netrc.load()

  return netrc
}

function writeSettings (settings) {
  return new Promise((res, rej) => {
    config.write(settings, err => {
      if (err) return rej(err)

      return res()
    })
  })
}

function deleteSettings () {
  return new Promise((res, rej) => {
    config.trash((err) => {
      if (err) return rej(err)

      return res()
    })
  })
}

function getRemoteObjectOrDie (
  program: any,
  settings: Settings,
  netrc: Netrc
): { url: string, username: string, password: string } {
  if (!program['url'] || !program['username'] || !program['password']) {
    // No remote and we don't have program parameters: quit
    if (settings.remotes.length === 0 || Object.keys(netrc.machines).length === 0) {
      if (!program[ 'url' ]) console.error('--url field is required.')
      if (!program[ 'username' ]) console.error('--username field is required.')
      if (!program[ 'password' ]) console.error('--password field is required.')

      return process.exit(-1)
    }

    let url: string = program['url']
    let username: string = program['username']
    let password: string = program['password']

    if (!url && settings.default !== -1) url = settings.remotes[settings.default]

    const machine = netrc.machines[url]

    if (!username && machine) username = machine.login
    if (!password && machine) password = machine.password

    return { url, username, password }
  }

  return {
    url: program[ 'url' ],
    username: program[ 'username' ],
    password: program[ 'password' ]
  }
}

function buildCommonVideoOptions (command: Command) {
  function list (val) {
    return val.split(',')
  }

  return command
    .option('-n, --video-name <name>', 'Video name')
    .option('-c, --category <category_number>', 'Category number')
    .option('-l, --licence <licence_number>', 'Licence number')
    .option('-L, --language <language_code>', 'Language ISO 639 code (fr or en...)')
    .option('-t, --tags <tags>', 'Video tags', list)
    .option('-N, --nsfw', 'Video is Not Safe For Work')
    .option('-d, --video-description <description>', 'Video description')
    .option('-P, --privacy <privacy_number>', 'Privacy')
    .option('-C, --channel-name <channel_name>', 'Channel name')
    .option('-m, --comments-enabled', 'Enable comments')
    .option('-s, --support <support>', 'Video support text')
    .option('-w, --wait-transcoding', 'Wait transcoding before publishing the video')
}

async function buildVideoAttributesFromCommander (url: string, command: Command, defaultAttributes: any) {
  const defaultBooleanAttributes = {
    nsfw: false,
    commentsEnabled: true,
    downloadEnabled: true,
    waitTranscoding: true
  }

  const booleanAttributes: { [id in keyof typeof defaultBooleanAttributes]: boolean } | {} = {}

  for (const key of Object.keys(defaultBooleanAttributes)) {
    if (command[ key ] !== undefined) {
      booleanAttributes[key] = command[ key ]
    } else if (defaultAttributes[key] !== undefined) {
      booleanAttributes[key] = defaultAttributes[key]
    } else {
      booleanAttributes[key] = defaultBooleanAttributes[key]
    }
  }

  const videoAttributes = {
    name: command[ 'videoName' ] || defaultAttributes.name,
    category: command[ 'category' ] || defaultAttributes.category || undefined,
    licence: command[ 'licence' ] || defaultAttributes.licence || undefined,
    language: command[ 'language' ] || defaultAttributes.language || undefined,
    privacy: command[ 'privacy' ] || defaultAttributes.privacy || VideoPrivacy.PUBLIC,
    support: command[ 'support' ] || defaultAttributes.support || undefined
  }

  Object.assign(videoAttributes, booleanAttributes)

  if (command[ 'channelName' ]) {
    const res = await getVideoChannel(url, command['channelName'])
    const videoChannel: VideoChannel = res.body

    Object.assign(videoAttributes, { channelId: videoChannel.id })

    if (!videoAttributes.support && videoChannel.support) {
      Object.assign(videoAttributes, { support: videoChannel.support })
    }
  }

  return videoAttributes
}

function getServerCredentials (program: any) {
  return Promise.all([ getSettings(), getNetrc() ])
         .then(([ settings, netrc ]) => {
           return getRemoteObjectOrDie(program, settings, netrc)
         })
}

// ---------------------------------------------------------------------------

export {
  version,
  config,
  getSettings,
  getNetrc,
  getRemoteObjectOrDie,
  writeSettings,
  deleteSettings,

  getServerCredentials,

  buildCommonVideoOptions,
  buildVideoAttributesFromCommander
}