Skip to content
Snippets Groups Projects
Commit bfa3332b authored by michael.minelli's avatar michael.minelli
Browse files

Completion => Remove bash|zsh|fish subcommands for create|update command

parent f58d64ad
Branches
Tags
No related merge requests found
Pipeline #30171 passed
import CommanderCommand from '../CommanderCommand'; import CommanderCommand from '../CommanderCommand';
import CompletionBashCommand from './subcommands/CompletionBashCommand'; import CompletionCreateUpdateCommand from './subcommands/CompletionCreateUpdateCommand';
import CompletionFishCommand from './subcommands/CompletionFishCommand'; import CompletionGetCommand from './subcommands/CompletionGetCommand';
import CompletionZshCommand from './subcommands/CompletionZshCommand'; import CompletionScriptCommand from './subcommands/CompletionScriptCommand';
import CompletionGetCommand from './subcommands/CompletionGetCommand';
import CompletionScriptCommand from './subcommands/CompletionScriptCommand';
class CompletionCommand extends CommanderCommand { class CompletionCommand extends CommanderCommand {
...@@ -15,10 +13,7 @@ class CompletionCommand extends CommanderCommand { ...@@ -15,10 +13,7 @@ class CompletionCommand extends CommanderCommand {
} }
protected defineSubCommands() { protected defineSubCommands() {
CompletionBashCommand.registerOnCommand(this.command); CompletionCreateUpdateCommand.registerOnCommand(this.command);
CompletionFishCommand.registerOnCommand(this.command);
CompletionZshCommand.registerOnCommand(this.command);
CompletionGetCommand.registerOnCommand(this.command); CompletionGetCommand.registerOnCommand(this.command);
CompletionScriptCommand.registerOnCommand(this.command); CompletionScriptCommand.registerOnCommand(this.command);
} }
......
import CommanderCommand from '../../CommanderCommand';
import path from 'path';
import os from 'os';
import { updateRcFile } from '../../../helpers/AutoCompletionHelper';
class CompletionBashCommand extends CommanderCommand {
protected commandName: string = 'bash';
private readonly bashrcPath = path.join(os.homedir(), '.bashrc');
private readonly completionCommand = `
# Added by DojoCLI
eval "$(dojo completion script bash)"
`;
protected defineCommand() {
this.command.description('generate bash completion')
.action(this.commandAction.bind(this));
}
protected async commandAction(): Promise<void> {
updateRcFile('bash', this.bashrcPath, this.completionCommand);
}
}
export default new CompletionBashCommand();
\ No newline at end of file
import { generateFishCompletion, getRoot, tryRenameFile } from '../../../helpers/AutoCompletionHelper'; import CommanderCommand from '../../CommanderCommand';
import CommanderCommand from '../../CommanderCommand'; import { Option } from 'commander';
import ora from 'ora'; import { generateFishCompletion, getRoot, tryRenameFile, updateRcFile } from '../../../helpers/AutoCompletionHelper';
import TextStyle from '../../../types/TextStyle'; import os, { homedir } from 'os';
import path from 'path'; import path from 'path';
import os from 'os'; import ora from 'ora';
import fs from 'fs-extra'; import fs from 'fs-extra';
import GlobalHelper from '../../../helpers/GlobalHelper'; import TextStyle from '../../../types/TextStyle';
class CompletionFishCommand extends CommanderCommand { class CompletionCreateUpdateCommand extends CommanderCommand {
protected commandName: string = 'fish'; protected commandName: string = 'create';
protected aliasNames: Array<string> = [ 'update' ];
private readonly installPath = path.join(os.homedir(), '.config/fish/completions/dojo.fish');
protected defineCommand() { protected defineCommand() {
GlobalHelper.completionCommandDefinition(this.command) this.command.description('generate shell completion')
.description('generate fish completion') .addOption(new Option('-s, --shell <shell>', 'shell type').choices([ 'bash', 'zsh', 'fish' ]).makeOptionMandatory(true))
.addOption(new Option('-f, --file <filename>', '(only for fish shell)').implies({ shell: 'fish' }))
.addOption(new Option('-y, --force', 'don\'t ask for file overwrite confirmation (only for fish shell)').implies({ shell: 'fish' }))
.action(this.commandAction.bind(this)); .action(this.commandAction.bind(this));
} }
private writeFile(filename: string, showInstructions: boolean) { private bash() {
const spinner: ora.Ora = ora(`Writing fish completion in ${ filename }...`).start(); const completionCommand = `
# Added by DojoCLI
try { eval "$(dojo completion script bash)"
fs.mkdirsSync(path.dirname(filename)); `;
updateRcFile('bash', path.join(os.homedir(), '.bashrc'), completionCommand);
fs.writeFileSync(filename, generateFishCompletion(getRoot(this.command)));
spinner.succeed(`Fish completion successfully written in ${ filename }.`);
if ( showInstructions ) {
const cpCommand = ` cp -i ${ filename } ~/.config/fish/completions # interactive cp to avoid accidents `;
console.log(`
The easiest way to install the completion is to copy the ${ TextStyle.CODE(filename) } into the ${ TextStyle.CODE('~/.config/fish/completions') } directory.
${ TextStyle.CODE(cpCommand) }`);
}
} catch ( error ) {
spinner.fail(`Fish completion error: ${ error }.`);
}
} }
private zsh() {
const completionCommand = `
# Added by DojoCLI
source <(dojo completion script zsh)
`;
updateRcFile('zsh', path.join(homedir(), '.zshrc'), completionCommand);
}
/* The completion command must do the following: /* The completion command must do the following:
- if a file is provided: - if a file is provided:
...@@ -56,15 +52,50 @@ ${ TextStyle.CODE(cpCommand) }`); ...@@ -56,15 +52,50 @@ ${ TextStyle.CODE(cpCommand) }`);
- else - else
- create the file containing the completion - create the file containing the completion
*/ */
protected async commandAction(options: { file: string, force: boolean }): Promise<void> { private async fish(options: { file: string, force: boolean }) {
const filePath = path.resolve(options.file ?? this.installPath); // change that if file is empty const filePath = path.resolve(options.file ?? path.join(os.homedir(), '.config/fish/completions/dojo.fish'));
const showInstructions = !!options.file; const showInstructions = !!options.file;
if ( !(await tryRenameFile(filePath, options.force)) ) { // means renaming was interrupted if ( !(await tryRenameFile(filePath, options.force)) ) { // means renaming was interrupted
return; return;
} }
this.writeFile(filePath, showInstructions);
const spinner: ora.Ora = ora(`Writing fish completion in ${ filePath }...`).start();
try {
fs.mkdirsSync(path.dirname(filePath));
fs.writeFileSync(filePath, generateFishCompletion(getRoot(this.command)));
spinner.succeed(`Fish completion successfully written in ${ filePath }.`);
if ( showInstructions ) {
const cpCommand = ` cp -i ${ filePath } ~/.config/fish/completions # interactive cp to avoid accidents `;
console.log(`
The easiest way to install the completion is to copy the ${ TextStyle.CODE(filePath) } into the ${ TextStyle.CODE('~/.config/fish/completions') } directory.
${ TextStyle.CODE(cpCommand) }`);
}
} catch ( error ) {
spinner.fail(`Fish completion error: ${ error }.`);
}
}
protected async commandAction(options: { shell: 'bash' | 'zsh' | 'fish', file: string, force: boolean }): Promise<void> {
switch ( options.shell ) {
case 'bash':
this.bash();
break;
case 'zsh':
this.zsh();
break;
case 'fish':
await this.fish(options);
break;
default:
console.error('Unsupported shell.');
break;
}
} }
} }
export default new CompletionFishCommand(); export default new CompletionCreateUpdateCommand();
\ No newline at end of file \ No newline at end of file
import CommanderCommand from '../../CommanderCommand';
import path from 'path';
import { homedir } from 'os';
import { updateRcFile } from '../../../helpers/AutoCompletionHelper';
class CompletionZshCommand extends CommanderCommand {
protected commandName: string = 'zsh';
private readonly zshrcPath: string = path.join(homedir(), '.zshrc');
private readonly completionCommand = `
# Added by DojoCLI
source <(dojo completion script zsh)
`;
protected defineCommand() {
this.command.description('generate zsh completion')
.action(this.commandAction.bind(this));
}
protected async commandAction(): Promise<void> {
updateRcFile('zsh', this.zshrcPath, this.completionCommand);
}
}
export default new CompletionZshCommand();
\ No newline at end of file
...@@ -5,18 +5,10 @@ import Config from '../config/Config'; ...@@ -5,18 +5,10 @@ import Config from '../config/Config';
class GlobalHelper { class GlobalHelper {
public runCommandDefinition(command: Command) { public runCommandDefinition(command: Command) {
command command
.option('-p, --path <value>', 'assignment path', Config.folders.defaultLocalExercise) .option('-p, --path <value>', 'assignment path', Config.folders.defaultLocalExercise)
.option('-v, --verbose', 'verbose mode - display principal container output in live') .option('-v, --verbose', 'verbose mode - display principal container output in live')
.addOption(new Option('-w, --super-verbose', 'verbose mode - display all docker compose logs (build included) in live').conflicts('verbose')) .addOption(new Option('-w, --super-verbose', 'verbose mode - display all docker compose logs (build included) in live').conflicts('verbose'))
.addOption(new Option('--verbose-ssj2').hideHelp().implies({ superVerbose: true })); .addOption(new Option('--verbose-ssj2').hideHelp().implies({ superVerbose: true }));
return command;
}
public completionCommandDefinition(command: Command) {
command
.option('-f, --file <filename>')
.option('-y, --force', 'don\'t ask for file overwrite confirmation');
return command; return command;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment