Skip to content

Commit

Permalink
Start usage documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
ggodlewski committed Oct 10, 2023
1 parent d280cd1 commit 095bfd7
Show file tree
Hide file tree
Showing 12 changed files with 228 additions and 176 deletions.
63 changes: 63 additions & 0 deletions doc/wikigdrive_usage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Usage

## wikigdrive usage

$ wikigdrive <command> [args] [<options>]

Main commands:

wikigdrive config
--client_id
--client_secret
--service_account=./private_key.json

wikigdrive service

wikigdrive add [folder_id_or_url]
--drive [shared drive url]
--workdir (current working folder)
--link_mode [mdURLs|dirURLs|uglyURLs]

wikigdrive pull [URL to specific file]

wikigdrive watch (keep scanning for changes, ie: daemon)

Other commands:

wikigdrive status [ID of document] - Show status of the document or stats of the entire path.
wikigdrive drives
wikigdrive sync
wikigdrive download
wikigdrive transform

Options:
--workdir (current working folder)

Examples:
$ wikigdrive init
$ wikigdrive add https://google.drive...

## wikigdrive drives usage

$ wikigdrive drives [<options>]

Options:

--client_id GOOGLE_DRIVE_API CLIENT_ID
--client_secret GOOGLE_DRIVE_API CLIENT_SECRET
--service_account GOOGLE_DRIVE_API SERVICE_ACCOUNT_JSON file location

Examples:

wikigdrive drives --client_id=AAA --client_secret=BBB
wikigdrive drives --service_account=./private_key.json

## All commands

Other commands:

wikigdrive status
wikigdrive drives
wikigdrive sync
wikigdrive download
wikigdrive transform
3 changes: 2 additions & 1 deletion src/cli/getAuthConfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {FileContentService} from '../utils/FileContentService';
import {AuthConfig} from '../model/AccountJson';
import {UsageError} from "./usage";

interface Params {
client_id?: string;
Expand All @@ -25,7 +26,7 @@ export async function getAuthConfig(params: Params, mainFileService: FileContent
} else {
const authConfig = await mainFileService.readJson('auth_config.json');
if (!authConfig) {
throw new Error('No authentication credentials provided');
throw new UsageError('No authentication credentials provided');
}
return authConfig;
}
Expand Down
22 changes: 22 additions & 0 deletions src/cli/initEngine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {EventEmitter} from 'events';
import {FileContentService} from '../utils/FileContentService';
import {createLogger} from '../utils/logger/logger';
import {ContainerEngine} from '../ContainerEngine';

export async function initEngine(workdir: string) {
const mainFileService = new FileContentService(workdir);
await mainFileService.mkdir('/');

const eventBus = new EventEmitter();
eventBus.setMaxListeners(0);
eventBus.on('panic:invalid_grant', () => {
process.exit(1);
});
eventBus.on('panic', (error) => {
throw error;
});

const logger = createLogger(workdir, eventBus);
const containerEngine = new ContainerEngine(logger, mainFileService);
return {mainFileService, containerEngine, logger};
}
65 changes: 33 additions & 32 deletions src/cli/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,46 @@ import {fileURLToPath} from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

export async function usage(filename: string) {
const pkg = JSON.parse(fs.readFileSync(path.resolve(__dirname, '..', '..', 'package.json')).toString());

console.log(
`version: ${pkg.version}, ${process.env.GIT_SHA}${`
Usage:
$ wikigdrive <command> [args] [<options>]
Main commands:
export class UsageError extends Error {
public isUsageError = true;
}

wikigdrive config
--client_id
--client_secret
--service_account=./private_key.json
function locateUsage(usageMarkdown: string, sectionPrefix: string): string {
let inSection = false;
const retVal = [];

const lines = usageMarkdown.split('\n');
for (const line of lines) {
if (line.startsWith('## ' + sectionPrefix)) {
inSection = true;
} else if (line.startsWith('## ')) {
inSection = false;
} else {
if (inSection) {
retVal.push(line);
}
}
}

return retVal.join('\n');
}

wikigdrive service
export async function usage(filename: string) {
const pkg = JSON.parse(new TextDecoder().decode(fs.readFileSync(path.resolve(__dirname, '..', '..', 'package.json'))));

wikigdrive add [folder_id_or_url]
--drive [shared drive url]
--workdir (current working folder)
--link_mode [mdURLs|dirURLs|uglyURLs]
filename = path.basename(filename);

wikigdrive pull [URL to specific file]
const execName = filename.replace(/-.*$/, '');
const sectionName = filename.replace(/^.*-(.*).ts/, '$1');

wikigdrive watch (keep scanning for changes, ie: daemon)

Other commands:
const mdFilename = execName + '_usage.md';

wikigdrive status [ID of document] - Show status of the document or stats of the entire path.
wikigdrive drives
wikigdrive sync
wikigdrive download
wikigdrive transform
const usageMarkdown = new TextDecoder().decode(fs.readFileSync(path.resolve(__dirname, '..', '..', 'doc', mdFilename)));

Options:
--workdir (current working folder)
const commandUsage = locateUsage(usageMarkdown, `${execName} ${sectionName}`) || locateUsage(usageMarkdown, `${execName} usage`);
const allCommands = locateUsage(usageMarkdown, 'All commands');

Examples:
$ wikigdrive init
$ wikigdrive add https://google.drive...
`}`);
console.log(
`${pkg.name} version: ${pkg.version}, ${process.env.GIT_SHA}\n\nUsage:\n${commandUsage.trim()}\n\n${allCommands.trim()}`);
}
18 changes: 11 additions & 7 deletions src/cli/wikigdrive-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,15 @@ async function main() {
await mainFileService.writeJson('auth_config.json', authConfig);
}

main()
.then(() => {
process.exit(0);
})
.catch((err) => {
try {
await main();
process.exit(0);
} catch (err) {
if (err.isUsageError) {
console.error(err.message);
await usage(__filename);
} else {
console.error(err);
process.exit(1);
});
}
process.exit(1);
}
27 changes: 16 additions & 11 deletions src/cli/wikigdrive-drives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import {fileURLToPath} from 'url';

import {addTelemetry} from '../telemetry';
import {GoogleApiContainer} from '../containers/google_api/GoogleApiContainer';
import {FileContentService} from '../utils/FileContentService';
import {getAuthConfig} from './getAuthConfig';
import {usage} from './usage';
import {initEngine} from './initEngine';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
Expand All @@ -32,31 +32,36 @@ async function main() {

const workdir = argv['workdir'] || process.env.WIKIGDRIVE_WORKDIR || '/data';

const mainFileService = new FileContentService(workdir);
await mainFileService.mkdir('/');
const {mainFileService, containerEngine} = await initEngine(workdir);

const params = {
client_id: argv['client_id'] || process.env.CLIENT_ID,
client_secret: argv['client_secret'] || process.env.CLIENT_SECRET,
service_account: argv['service_account'] || null,
};

const authConfig = await getAuthConfig(params, mainFileService);

const apiContainer = new GoogleApiContainer({ name: 'google_api' }, authConfig);
await apiContainer.mount(await mainFileService);
await containerEngine.registerContainer(apiContainer);
await apiContainer.run();

const drives = await apiContainer.listDrives();
console.log('Available drives:');
console.table(drives);

containerEngine.flushData();
}

main()
.then(() => {
process.exit(0);
})
.catch((err) => {
try {
await main();
process.exit(0);
} catch (err) {
if (err.isUsageError) {
console.error(err.message);
await usage(__filename);
} else {
console.error(err);
process.exit(1);
});
}
process.exit(1);
}
29 changes: 14 additions & 15 deletions src/cli/wikigdrive-prune.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ import {EventEmitter} from 'events';

import {addTelemetry} from '../telemetry';
import {GoogleApiContainer} from '../containers/google_api/GoogleApiContainer';
import {FileContentService} from '../utils/FileContentService';
import {getAuthConfig} from './getAuthConfig';
import {urlToFolderId} from '../utils/idParsers';
import {FolderRegistryContainer} from '../containers/folder_registry/FolderRegistryContainer';
import {ContainerEngine} from '../ContainerEngine';
import {createLogger} from '../utils/logger/logger';
import {usage} from './usage';
import {initEngine} from './initEngine';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
Expand All @@ -39,8 +37,7 @@ async function main() {

const workdir = argv['workdir'] || process.env.WIKIGDRIVE_WORKDIR || '/data';

const mainFileService = new FileContentService(workdir);
await mainFileService.mkdir('/');
const {mainFileService, containerEngine} = await initEngine(workdir);

const params = {
client_id: argv['client_id'] || process.env.CLIENT_ID,
Expand All @@ -52,6 +49,7 @@ async function main() {

const apiContainer = new GoogleApiContainer({ name: 'google_api' }, authConfig);
await apiContainer.mount(await mainFileService);
await containerEngine.registerContainer(apiContainer);
await apiContainer.run();

const folderId = urlToFolderId(args[0]);
Expand All @@ -68,9 +66,6 @@ async function main() {
throw error;
});

const logger = createLogger(workdir, eventBus);
const containerEngine = new ContainerEngine(logger, mainFileService);

const folderRegistryContainer = new FolderRegistryContainer({ name: 'folder_registry' });
await folderRegistryContainer.mount(await mainFileService);
await containerEngine.registerContainer(folderRegistryContainer);
Expand All @@ -80,11 +75,15 @@ async function main() {
await containerEngine.flushData();
}

main()
.then(() => {
process.exit(0);
})
.catch((err) => {
try {
await main();
process.exit(0);
} catch (err) {
if (err.isUsageError) {
console.error(err.message);
await usage(__filename);
} else {
console.error(err);
process.exit(1);
});
}
process.exit(1);
}
Loading

0 comments on commit 095bfd7

Please sign in to comment.