forked from laurent22/joplin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Server: Moved CLI commands to separate files
- Loading branch information
Showing
11 changed files
with
22,450 additions
and
25,783 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Options, PositionalOptions } from 'yargs'; | ||
import { DbConnection } from '../db'; | ||
import { Models } from '../models/factory'; | ||
|
||
export interface RunContext { | ||
db: DbConnection; | ||
models: Models; | ||
} | ||
|
||
export default abstract class BaseCommand { | ||
|
||
public commandName(): string { | ||
const splitted = this.command().split(' '); | ||
if (!splitted.length) throw new Error(`Invalid command: ${this.command()}`); | ||
return splitted[0]; | ||
} | ||
|
||
public command(): string { | ||
throw new Error('Not implemented'); | ||
} | ||
|
||
public description(): string { | ||
throw new Error('Not implemented'); | ||
} | ||
|
||
public positionals(): Record<string, PositionalOptions> { | ||
return {}; | ||
} | ||
|
||
public options(): Record<string, Options> { | ||
return {}; | ||
} | ||
|
||
public abstract run(argv: any, context: RunContext): Promise<void>; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { PositionalOptions } from 'yargs'; | ||
import config from '../config'; | ||
import { connectDb, disconnectDb, dropTables } from '../db'; | ||
import BaseCommand from './BaseCommand'; | ||
import { createDb } from '../tools/dbTools'; | ||
|
||
enum ArgvCommand { | ||
DropTables = 'dropTables', | ||
Create = 'create', | ||
} | ||
|
||
interface Argv { | ||
command: ArgvCommand; | ||
} | ||
|
||
export default class DbCommand extends BaseCommand { | ||
|
||
public command() { | ||
return 'db <command>'; | ||
} | ||
|
||
public description() { | ||
return 'execute a database command'; | ||
} | ||
|
||
public positionals(): Record<string, PositionalOptions> { | ||
return { | ||
'command': { | ||
description: 'command to execute', | ||
choices: [ | ||
ArgvCommand.Create, | ||
ArgvCommand.DropTables, | ||
], | ||
}, | ||
}; | ||
} | ||
|
||
public async run(argv: Argv): Promise<void> { | ||
|
||
|
||
const commands: Record<ArgvCommand, Function> = { | ||
create: async () => { | ||
await createDb(config().database); | ||
}, | ||
dropTables: async () => { | ||
const db = await connectDb(config().database); | ||
await dropTables(db); | ||
await disconnectDb(db); | ||
}, | ||
}; | ||
|
||
if (!commands[argv.command]) throw new Error(`Invalid command: ${argv.command}`); | ||
|
||
await commands[argv.command](); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Options } from 'yargs'; | ||
import { Day } from '../utils/time'; | ||
import BaseCommand, { RunContext } from './BaseCommand'; | ||
|
||
interface Argv { | ||
ttl: number; | ||
} | ||
|
||
export default class DeleteOldChangesCommand extends BaseCommand { | ||
|
||
public command() { | ||
return 'deleteOldChanges'; | ||
} | ||
|
||
public description() { | ||
return 'deletes old changes'; | ||
} | ||
|
||
public options(): Record<string, Options> { | ||
return { | ||
'ttl': { | ||
type: 'number', | ||
description: 'TTL in days', | ||
}, | ||
}; | ||
} | ||
|
||
public async run(argv: Argv, runContext: RunContext): Promise<void> { | ||
await runContext.models.change().deleteOldChanges(argv.ttl ? argv.ttl * Day : null); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { PositionalOptions, Options } from 'yargs'; | ||
import Logger from '@joplin/lib/Logger'; | ||
import { disconnectDb, migrateDown, migrateLatest, migrateList, migrateUnlock, migrateUp } from '../db'; | ||
import BaseCommand, { RunContext } from './BaseCommand'; | ||
|
||
const logger = Logger.create('MigrateCommand'); | ||
|
||
enum ArgvCommand { | ||
Up = 'up', | ||
Down = 'down', | ||
Latest = 'latest', | ||
List = 'list', | ||
Unlock = 'unlock', | ||
} | ||
|
||
interface Argv { | ||
command: ArgvCommand; | ||
} | ||
|
||
export default class MigrateCommand extends BaseCommand { | ||
|
||
public command() { | ||
return 'migrate <command>'; | ||
} | ||
|
||
public description() { | ||
return 'execute a database migration'; | ||
} | ||
|
||
public positionals(): Record<string, PositionalOptions> { | ||
return { | ||
'command': { | ||
description: 'command to execute', | ||
choices: [ | ||
ArgvCommand.Up, | ||
ArgvCommand.Down, | ||
ArgvCommand.Latest, | ||
ArgvCommand.List, | ||
ArgvCommand.Unlock, | ||
], | ||
}, | ||
}; | ||
} | ||
|
||
public options(): Record<string, Options> { | ||
return { | ||
'disable-transactions': { | ||
type: 'boolean', | ||
}, | ||
}; | ||
} | ||
|
||
public async run(argv: Argv, runContext: RunContext): Promise<void> { | ||
const commands: Record<ArgvCommand, Function> = { | ||
up: async () => { | ||
await migrateUp(runContext.db); | ||
}, | ||
down: async () => { | ||
await migrateDown(runContext.db); | ||
}, | ||
latest: async () => { | ||
await migrateLatest(runContext.db); | ||
}, | ||
list: async () => { | ||
const s = (await migrateList(runContext.db)) as string; | ||
s.split('\n').forEach(l => logger.info(l)); | ||
}, | ||
unlock: async () => { | ||
await migrateUnlock(runContext.db); | ||
}, | ||
}; | ||
|
||
if (!commands[argv.command]) throw new Error(`Invalid command: ${argv.command}`); | ||
|
||
await commands[argv.command](); | ||
|
||
await disconnectDb(runContext.db); | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.