1
0
mirror of https://github.com/bitwarden/browser synced 2024-12-23 00:25:04 +01:00
bitwarden-estensione-browser/apps/cli/src/commands/completion.command.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

120 lines
3.0 KiB
TypeScript
Raw Normal View History

2020-05-26 15:34:44 +02:00
import * as program from "commander";
2022-06-14 17:10:53 +02:00
import { Response } from "@bitwarden/node/cli/models/response";
import { MessageResponse } from "@bitwarden/node/cli/models/response/messageResponse";
2020-05-26 15:34:44 +02:00
interface IOption {
Add send to cli (#222) * Add list all sends and filter by search term * Add get send templates * Add AccessUrl to send responses * Add Send to Get command * Add missing command options to login These options are already coded to work in the command, but commander did not know about the options. * Upgrade Commander to 7.0.0 This is needed to enable the subcommand chaining required by Send. This commit also adds get send and send receive functionality. get send will be moved to send get along with send list and any other send commands. * Use api url for send access url * Move send commands to send subcommands * Use webvault access url everywhere Production instances all have api url located at `baseUrl/api`. Receive command will parse the webvault url and alter it to an api url. * Move create and receive commands to send directory * Separate program concerns program holds authentication/general program concerns vault.program holds commands related to the vault send.program holds commands related to Bitwarden Send * Fix up imports and lint items * Add edit command * Use browser-hrtime * Add send examples to help text * Clean up receive help text * correct help text * Add delete command * Code review Cleanup * Scheme on send receive help text * PR review items Move buffer to array buffer to jslib delete with server some formatting fixes * Add remove password command This is the simplest way to enable removing passwords without resorting to weird type parsing of piped in Send JSONs in edit * Default hidden to false like web * Do not allow password updates that aren't strings or are empty * Delete appveyor.yml.flagged-for-delete * Correctly order imports and include tslint rule * fix npm globbing problem https://stackoverflow.com/a/34594501 globs work differently in package.json. Encasing the globs in single quotes expands them in shell rather than in npm * Remove double slash in path * Trigger github rebuild
2021-02-03 18:44:33 +01:00
long?: string;
short?: string;
description: string;
2020-05-26 15:34:44 +02:00
}
2020-05-26 15:34:44 +02:00
interface ICommand {
commands?: ICommand[];
options?: IOption[];
_name: string;
_description: string;
2020-05-26 15:34:44 +02:00
}
const validShells = ["zsh"];
export class CompletionCommand {
Add send to cli (#222) * Add list all sends and filter by search term * Add get send templates * Add AccessUrl to send responses * Add Send to Get command * Add missing command options to login These options are already coded to work in the command, but commander did not know about the options. * Upgrade Commander to 7.0.0 This is needed to enable the subcommand chaining required by Send. This commit also adds get send and send receive functionality. get send will be moved to send get along with send list and any other send commands. * Use api url for send access url * Move send commands to send subcommands * Use webvault access url everywhere Production instances all have api url located at `baseUrl/api`. Receive command will parse the webvault url and alter it to an api url. * Move create and receive commands to send directory * Separate program concerns program holds authentication/general program concerns vault.program holds commands related to the vault send.program holds commands related to Bitwarden Send * Fix up imports and lint items * Add edit command * Use browser-hrtime * Add send examples to help text * Clean up receive help text * correct help text * Add delete command * Code review Cleanup * Scheme on send receive help text * PR review items Move buffer to array buffer to jslib delete with server some formatting fixes * Add remove password command This is the simplest way to enable removing passwords without resorting to weird type parsing of piped in Send JSONs in edit * Default hidden to false like web * Do not allow password updates that aren't strings or are empty * Delete appveyor.yml.flagged-for-delete * Correctly order imports and include tslint rule * fix npm globbing problem https://stackoverflow.com/a/34594501 globs work differently in package.json. Encasing the globs in single quotes expands them in shell rather than in npm * Remove double slash in path * Trigger github rebuild
2021-02-03 18:44:33 +01:00
async run(options: program.OptionValues) {
const shell: typeof validShells[number] = options.shell;
2020-05-26 15:34:44 +02:00
if (!shell) {
2022-01-19 16:45:14 +01:00
return Response.badRequest("`shell` option was not provided.");
2021-12-20 18:04:00 +01:00
}
2020-05-26 15:34:44 +02:00
if (!validShells.includes(shell)) {
return Response.badRequest("Unsupported shell.");
}
let content = "";
2021-12-20 18:04:00 +01:00
2020-05-26 15:34:44 +02:00
if (shell === "zsh") {
content = this.zshCompletion("bw", program as any as ICommand).render();
}
const res = new MessageResponse(content, null);
return Response.success(res);
2021-12-20 18:04:00 +01:00
}
2020-05-26 15:34:44 +02:00
private zshCompletion(rootName: string, rootCommand: ICommand) {
2021-12-20 18:04:00 +01:00
return {
2020-05-26 15:34:44 +02:00
render: () => {
2021-12-20 18:04:00 +01:00
return [
2020-05-26 15:34:44 +02:00
`#compdef _${rootName} ${rootName}`,
2021-12-20 18:04:00 +01:00
"",
2020-05-26 15:34:44 +02:00
this.renderCommandBlock(rootName, rootCommand),
].join("\n");
2021-12-20 18:04:00 +01:00
},
};
}
2020-05-26 15:34:44 +02:00
private renderCommandBlock(name: string, command: ICommand): string {
const { commands = [], options = [] } = command;
const hasOptions = options.length > 0;
const hasCommands = commands.length > 0;
2021-12-20 18:04:00 +01:00
2020-05-26 15:34:44 +02:00
const args = options
.map(({ long, short, description }) => {
2020-05-26 15:34:44 +02:00
const aliases = [short, long].filter(Boolean);
const opts = aliases.join(",");
const desc = `[${description.replace(`'`, `'"'"'`)}]`;
return aliases.length > 1
? `'(${aliases.join(" ")})'{${opts}}'${desc}'`
: `'${opts}${desc}'`;
2021-12-20 18:04:00 +01:00
})
.concat(
2020-05-26 15:34:44 +02:00
`'(-h --help)'{-h,--help}'[output usage information]'`,
hasCommands ? '"1: :->cmnds"' : null,
'"*::arg:->args"'
2021-12-20 18:04:00 +01:00
)
2020-05-26 15:34:44 +02:00
.filter(Boolean);
Add send to cli (#222) * Add list all sends and filter by search term * Add get send templates * Add AccessUrl to send responses * Add Send to Get command * Add missing command options to login These options are already coded to work in the command, but commander did not know about the options. * Upgrade Commander to 7.0.0 This is needed to enable the subcommand chaining required by Send. This commit also adds get send and send receive functionality. get send will be moved to send get along with send list and any other send commands. * Use api url for send access url * Move send commands to send subcommands * Use webvault access url everywhere Production instances all have api url located at `baseUrl/api`. Receive command will parse the webvault url and alter it to an api url. * Move create and receive commands to send directory * Separate program concerns program holds authentication/general program concerns vault.program holds commands related to the vault send.program holds commands related to Bitwarden Send * Fix up imports and lint items * Add edit command * Use browser-hrtime * Add send examples to help text * Clean up receive help text * correct help text * Add delete command * Code review Cleanup * Scheme on send receive help text * PR review items Move buffer to array buffer to jslib delete with server some formatting fixes * Add remove password command This is the simplest way to enable removing passwords without resorting to weird type parsing of piped in Send JSONs in edit * Default hidden to false like web * Do not allow password updates that aren't strings or are empty * Delete appveyor.yml.flagged-for-delete * Correctly order imports and include tslint rule * fix npm globbing problem https://stackoverflow.com/a/34594501 globs work differently in package.json. Encasing the globs in single quotes expands them in shell rather than in npm * Remove double slash in path * Trigger github rebuild
2021-02-03 18:44:33 +01:00
const commandBlockFunctionParts = [];
2020-05-26 15:34:44 +02:00
if (hasCommands) {
2020-05-26 15:34:44 +02:00
commandBlockFunctionParts.push("local -a commands");
}
2020-05-26 15:34:44 +02:00
if (hasOptions) {
commandBlockFunctionParts.push(`_arguments -C \\\n ${args.join(` \\\n `)}`);
}
if (hasCommands) {
2020-05-26 15:34:44 +02:00
commandBlockFunctionParts.push(
`case $state in
cmnds)
commands=(
2020-05-26 15:34:44 +02:00
${commands
.map(({ _name, _description }) => `"${_name}:${_description}"`)
.join("\n ")}
)
_describe "command" commands
;;
esac
case "$words[1]" in
2020-05-26 15:34:44 +02:00
${commands
.map(({ _name }) => [`${_name})`, `_${name}_${_name}`, ";;"].join("\n "))
.join("\n ")}
esac`
);
}
2021-12-20 18:04:00 +01:00
const commandBlocParts = [
2020-05-26 15:34:44 +02:00
`function _${name} {\n ${commandBlockFunctionParts.join("\n\n ")}\n}`,
2021-12-20 18:04:00 +01:00
];
if (hasCommands) {
commandBlocParts.push(
commands.map((c) => this.renderCommandBlock(`${name}_${c._name}`, c)).join("\n\n")
2021-12-20 18:04:00 +01:00
);
}
2020-05-26 15:34:44 +02:00
return commandBlocParts.join("\n\n");
2021-12-20 18:04:00 +01:00
}
}