commandLineToolSpecGenerator function
Future<FigSubcommand>
commandLineToolSpecGenerator(
- List<
String> tokens, - ExecuteCommandFunction? executeShellCommand, [
- FigGeneratorContext? context
Implementation
Future<FigSubcommand> commandLineToolSpecGenerator(
List<String> tokens, ExecuteCommandFunction? executeShellCommand,
[FigGeneratorContext? context]) async {
if (executeShellCommand == null) return FigSubcommand(name: "cli");
const query = """query CommandLineTool {
currentUser {
namespace {
username
commandlineTools {
...CommandlineToolFields
}
}
teamMemberships {
team {
namespace {
username
commandlineTools {
...CommandlineToolFields
}
}
}
}
}
}
fragment CommandlineToolFields on CommandlineTool {
root {
...CLICommandFields
}
flattenedCommands {
...CLICommandFields
}
}
fragment CLICommandFields on ICLICommand {
uuid
name
description
... on NestedCommand {
subcommands {
uuid
}
}
... on ScriptCommand {
script {
...ScriptFields
}
}
}
$scriptsFieldsFragment""";
final data = await graphql(exec: executeShellCommand, query: query);
List<dynamic> commandlineTools = [];
final currentUserNamespace = data['currentUser']['namespace'];
if (currentUserNamespace != null) {
final username = currentUserNamespace['username'];
commandlineTools
.addAll((currentUserNamespace['commandlineTools'] as List).map((t) => {
...t,
'namespace': username,
}));
}
final teamMemberships = data['currentUser']['teamMemberships'] as List?;
if (teamMemberships != null) {
for (var team in teamMemberships) {
final teamNamespace = team['team']['namespace'];
final username = teamNamespace['username'];
commandlineTools
.addAll((teamNamespace['commandlineTools'] as List).map((t) => {
...t,
'namespace': username,
}));
}
}
FigSubcommand createTree(dynamic root, int depth,
Map<String, dynamic> commands, String namespace) {
if (root['subcommands'] != null) {
final subcommands = <FigSubcommand>[];
for (var cmdRef in root['subcommands']) {
subcommands.add(createTree(
commands[cmdRef['uuid']], depth + 1, commands, namespace));
}
return FigSubcommand(
name: depth == 0 ? "@$namespace/${root['name']}" : root['name'],
description: root['description'],
subcommands: subcommands,
options: [
FigOption(
name: ["-h", "--help"],
description: "Print help information",
)
],
);
} else {
final script = ScriptFields.fromJson(root['script']);
final options = scriptOptions(script);
return FigSubcommand(
icon: script.fields['icon'],
name: root['name'],
description: root['description'],
options: options,
);
}
}
final subcommands = commandlineTools.map((tool) {
final commands = <String, dynamic>{};
for (var cmd in tool['flattenedCommands']) {
commands[cmd['uuid']] = cmd;
}
return createTree(tool['root'], 0, commands, tool['namespace']);
}).toList();
return FigSubcommand(
name: "cli",
subcommands: subcommands,
);
}