]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tools/cli.ts
Translated using Weblate (Polish)
[github/Chocobozzz/PeerTube.git] / server / tools / cli.ts
index 4aa3d9ce84fec0ff02f223d58bca3f9be14bc3dc..58e2445ac47f0c40e9f1e4f2cafa1ff8c860ca26 100644 (file)
@@ -1,9 +1,11 @@
 import { Netrc } from 'netrc-parser'
 import { getAppNumber, isTestInstance } from '../helpers/core-utils'
 import { join } from 'path'
-import { getVideoChannel, root } from '../../shared/extra-utils'
+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'
+import { createLogger, format, transports } from 'winston'
 
 let configName = 'PeerTube/CLI'
 if (isTestInstance()) configName += `-${getAppNumber()}`
@@ -64,7 +66,11 @@ function deleteSettings () {
   })
 }
 
-function getRemoteObjectOrDie (program: any, settings: Settings, netrc: Netrc) {
+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) {
@@ -114,18 +120,26 @@ function buildCommonVideoOptions (command: Command) {
     .option('-m, --comments-enabled', 'Enable comments')
     .option('-s, --support <support>', 'Video support text')
     .option('-w, --wait-transcoding', 'Wait transcoding before publishing the video')
+    .option('-v, --verbose <verbose>', 'Verbosity, from 0/\'error\' to 4/\'debug\'', 'info')
 }
 
-async function buildVideoAttributesFromCommander (url: string, command: Command, defaultAttributes: any) {
-  const booleanAttributes: { [id: string]: boolean } = {}
+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 [ 'nsfw', 'commentsEnabled', 'downloadEnabled', 'waitTranscoding' ]) {
+  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] = false
+      booleanAttributes[key] = defaultBooleanAttributes[key]
     }
   }
 
@@ -135,7 +149,9 @@ async function buildVideoAttributesFromCommander (url: string, command: Command,
     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
+    support: command[ 'support' ] || defaultAttributes.support || undefined,
+    description: command[ 'videoDescription' ] || defaultAttributes.description || undefined,
+    tags: command[ 'tags' ] || defaultAttributes.tags || undefined
   }
 
   Object.assign(videoAttributes, booleanAttributes)
@@ -154,17 +170,57 @@ async function buildVideoAttributesFromCommander (url: string, command: Command,
   return videoAttributes
 }
 
+function getServerCredentials (program: any) {
+  return Promise.all([ getSettings(), getNetrc() ])
+         .then(([ settings, netrc ]) => {
+           return getRemoteObjectOrDie(program, settings, netrc)
+         })
+}
+
+function getLogger (logLevel = 'info') {
+  const logLevels = {
+    0: 0,
+    error: 0,
+    1: 1,
+    warn: 1,
+    2: 2,
+    info: 2,
+    3: 3,
+    verbose: 3,
+    4: 4,
+    debug: 4
+  }
+
+  const logger = createLogger({
+    levels: logLevels,
+    format: format.combine(
+      format.splat(),
+      format.simple()
+    ),
+    transports: [
+      new (transports.Console)({
+        level: logLevel
+      })
+    ]
+  })
+
+  return logger
+}
+
 // ---------------------------------------------------------------------------
 
 export {
   version,
   config,
+  getLogger,
   getSettings,
   getNetrc,
   getRemoteObjectOrDie,
   writeSettings,
   deleteSettings,
 
+  getServerCredentials,
+
   buildCommonVideoOptions,
   buildVideoAttributesFromCommander
 }