Skip to content

Commit f9c4a98

Browse files
committed
feat(docker): commands with optional args as options
Move optional arguments into an options argument at the end. Signed-off-by: Max <[email protected]>
1 parent c91ff54 commit f9c4a98

File tree

1 file changed

+48
-33
lines changed

1 file changed

+48
-33
lines changed

lib/docker.ts

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ interface StartOptions {
7777
*
7878
* @param {string|undefined} branch server branch to use (default 'master')
7979
* @param {boolean|string|undefined} mountApp bind mount app within server (`true` for autodetect, `false` to disable, or a string to force a path) (default true)
80-
* @param {StartOptions|undefined} options Optional parameters to configre the container creation
80+
* @param {StartOptions|undefined} options Optional parameters to configure the container creation
8181
* @return Promise resolving to the IP address of the server
8282
* @throws {Error} If Nextcloud container could not be started
8383
*/
@@ -189,8 +189,8 @@ export async function startNextcloud(branch = 'master', mountApp: boolean|string
189189
await container.start()
190190

191191
// Set proper permissions for the data folder
192-
await runExec(container, ['chown', '-R', 'www-data:www-data', '/var/www/html/data'], false, 'root')
193-
await runExec(container, ['chmod', '0770', '/var/www/html/data'], false, 'root')
192+
await runExec(['chown', '-R', 'www-data:www-data', '/var/www/html/data'], { container, user: 'root' })
193+
await runExec(['chmod', '0770', '/var/www/html/data'], { container, user: 'root' })
194194

195195
// Get container's IP
196196
const ip = await getContainerIP(container)
@@ -219,20 +219,20 @@ export const configureNextcloud = async function(apps = ['viewer'], vendoredBran
219219

220220
console.log('\nConfiguring Nextcloud…')
221221
container = container ?? getContainer()
222-
await runOcc(container, ['--version'], true)
222+
await runOcc(['--version'], { container, verbose: true })
223223

224224
// Be consistent for screenshots
225-
await setSystemConfig(container, 'default_language', 'en')
226-
await setSystemConfig(container, 'force_language', 'en')
227-
await setSystemConfig(container, 'default_locale', 'en_US')
228-
await setSystemConfig(container, 'force_locale', 'en_US')
229-
await setSystemConfig(container, 'enforce_theme', 'light')
225+
await setSystemConfig('default_language', 'en', { container })
226+
await setSystemConfig('force_language', 'en', { container })
227+
await setSystemConfig('default_locale', 'en_US', { container })
228+
await setSystemConfig('force_locale', 'en_US', { container })
229+
await setSystemConfig('enforce_theme', 'light', { container })
230230

231231
// Checking apcu
232232
console.log('├─ Checking APCu configuration... 👀')
233-
const distributed = await getSystemConfig(container, 'memcache.distributed')
234-
const local = await getSystemConfig(container, 'memcache.local')
235-
const hashing = await getSystemConfig(container, 'hashing_default_password')
233+
const distributed = await getSystemConfig('memcache.distributed', { container })
234+
const local = await getSystemConfig('memcache.local', { container })
235+
const hashing = await getSystemConfig('hashing_default_password', { container })
236236
if (!distributed.includes('Memcache\\APCu')
237237
|| !local.includes('Memcache\\APCu')
238238
|| !hashing.includes('true')) {
@@ -242,7 +242,7 @@ export const configureNextcloud = async function(apps = ['viewer'], vendoredBran
242242
console.log('│ └─ OK !')
243243

244244
// Build app list
245-
const json = await runOcc(container, ['app:list', '--output', 'json'], false)
245+
const json = await runOcc(['app:list', '--output', 'json'], { container })
246246
// fix dockerode bug returning invalid leading characters
247247
const applist = JSON.parse(json.substring(json.indexOf('{')))
248248

@@ -252,14 +252,14 @@ export const configureNextcloud = async function(apps = ['viewer'], vendoredBran
252252
console.log(`├─ ${app} version ${applist.enabled[app]} already installed and enabled`)
253253
} else if (app in applist.disabled) {
254254
// built in or mounted already as the app under development
255-
await runOcc(container, ['app:enable', '--force', app], true)
255+
await runOcc(['app:enable', '--force', app], { container, verbose: true })
256256
} else if (app in VENDOR_APPS) {
257257
// apps that are vendored but still missing (i.e. not build in or mounted already)
258-
await runExec(container, ['git', 'clone', '--depth=1', `--branch=${vendoredBranch}`, VENDOR_APPS[app], `apps/${app}`], true)
259-
await runOcc(container, ['app:enable', '--force', app], true)
258+
await runExec(['git', 'clone', '--depth=1', `--branch=${vendoredBranch}`, VENDOR_APPS[app], `apps/${app}`], { container, verbose: true })
259+
await runOcc(['app:enable', '--force', app], { container, verbose: true })
260260
} else {
261261
// try appstore
262-
await runOcc(container, ['app:install', '--force', app], true)
262+
await runOcc(['app:install', '--force', app], { container, verbose: true })
263263
}
264264
}
265265
console.log('└─ Nextcloud is now ready to use 🎉')
@@ -274,7 +274,7 @@ export const setupUsers = async function(container?: Container) {
274274
console.log('\nCreating test users… 👤')
275275
const users = ['test1', 'test2', 'test3', 'test4', 'test5']
276276
for (const user of users) {
277-
await runExec(container ?? getContainer(), ['php', 'occ', 'user:add', user, '--password-from-env'], true, 'www-data', ['OC_PASS=' + user])
277+
await runExec(['php', 'occ', 'user:add', user, '--password-from-env'], { container, verbose: true, env: ['OC_PASS=' + user] })
278278
}
279279
console.log('└─ Done')
280280
}
@@ -288,7 +288,7 @@ export const setupUsers = async function(container?: Container) {
288288
export const createSnapshot = async function(snapshot?: string, container?: Container): Promise<string> {
289289
const hash = new Date().toISOString().replace(/[^0-9]/g, '')
290290
console.log('\nCreating init DB snapshot…')
291-
await runExec(container ?? getContainer(), ['cp', '/var/www/html/data/owncloud.db', `/var/www/html/data/owncloud.db-${snapshot ?? hash}`], true)
291+
await runExec(['cp', '/var/www/html/data/owncloud.db', `/var/www/html/data/owncloud.db-${snapshot ?? hash}`], { container, verbose: true })
292292
console.log('└─ Done')
293293
return snapshot ?? hash
294294
}
@@ -300,7 +300,7 @@ export const createSnapshot = async function(snapshot?: string, container?: Cont
300300
*/
301301
export const restoreSnapshot = async function(snapshot = 'init', container?: Container) {
302302
console.log('\nRestoring DB snapshot…')
303-
await runExec(container ?? getContainer(), ['cp', `/var/www/html/data/owncloud.db-${snapshot}`, '/var/www/html/data/owncloud.db'], true)
303+
await runExec(['cp', `/var/www/html/data/owncloud.db-${snapshot}`, '/var/www/html/data/owncloud.db'], { container, verbose: true })
304304
console.log('└─ Done')
305305
}
306306

@@ -356,13 +356,21 @@ export const waitOnNextcloud = async function(ip: string) {
356356
console.log('└─ Done')
357357
}
358358

359+
interface RunExecOptions {
360+
container: Docker.Container;
361+
user: string;
362+
env: string[];
363+
verbose: boolean;
364+
}
365+
366+
/**
367+
* Execute a command in the container
368+
*/
359369
export const runExec = async function(
360-
container: Docker.Container,
361370
command: string[],
362-
verbose = false,
363-
user = 'www-data',
364-
env: string[] = [],
371+
{ container, user='www-data', verbose=false, env=[] }: Partial<RunExecOptions> = {},
365372
) {
373+
container = container || getContainer()
366374
const exec = await container.exec({
367375
Cmd: command,
368376
AttachStdout: true,
@@ -397,28 +405,35 @@ export const runExec = async function(
397405
})
398406
}
399407

408+
/**
409+
* Execute an occ command in the container
410+
*/
400411
export const runOcc = function(
401-
container: Docker.Container,
402-
command: string[],
403-
verbose = false,
404-
env: string[] = [],
412+
occCommand: string[],
413+
{ container, env=[], verbose=false }: Partial<Omit<RunExecOptions, 'user'>> = {},
405414
) {
406-
return runExec(container, ['php', 'occ', ...command], verbose, 'www-data', env)
415+
return runExec(['php', 'occ', ...occCommand], { container, verbose, env })
407416
}
408417

418+
/**
419+
* Set a Nextcloud system config in the container.
420+
*/
409421
export const setSystemConfig = function(
410-
container: Docker.Container,
411422
key: string,
412423
value: string,
424+
{ container }: { container?: Docker.Container } = {},
413425
) {
414-
return runOcc(container, ['config:system:set', key, '--value', value], true)
426+
return runOcc(['config:system:set', key, '--value', value], { container, verbose: true })
415427
}
416428

429+
/**
430+
* Get a Nextcloud system config value from the container.
431+
*/
417432
export const getSystemConfig = function(
418-
container: Docker.Container,
419433
key: string,
434+
{ container }: { container?: Docker.Container } = {},
420435
) {
421-
return runOcc(container, ['config:system:get', key])
436+
return runOcc(['config:system:get', key], { container })
422437
}
423438

424439
const sleep = function(milliseconds: number) {

0 commit comments

Comments
 (0)