Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update types 2 #2162

Merged
merged 2 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ant.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const tasksGenerator: Fig.Generator = {
script: "command ant -p | grep -i '^\\s' | tr -d ' '",
script: ["bash", "-c", "command ant -p | grep -i '^\\s' | tr -d ' '"],
postProcess: (out) =>
out.split("\n").map((task) => ({
name: task,
Expand Down
13 changes: 9 additions & 4 deletions src/apt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@ const packages: Fig.Generator = {
if (finalToken.length === 0) {
return [];
}
const { stdout } = await executeShellCommand({
command: "apt",
// eslint-disable-next-line @withfig/fig-linter/no-useless-arrays
args: ["list"],
});

// Only lines matching the first character, delete characters after '/'
const out = await executeShellCommand(
`apt list | grep '^${finalToken[0]}' | sed 's#/.*##g'`
);
return out
return stdout
.trim()
.split("\n")
.filter((name) => name.startsWith(finalToken))
.map((name) => name.replace(/\/.*/, ""))
.map((name) => ({
name,
description: "Package",
Expand Down
88 changes: 52 additions & 36 deletions src/aws/cloudwatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,14 @@ const postPrecessGenerator = (

const listCustomGenerator = async (
tokens: string[],
executeShellCommand: Fig.ExecuteShellCommandFunction,
executeShellCommand: Fig.ExecuteCommandFunction,
command: string,
options: string[],
parentKey: string,
childKey = ""
): Promise<Fig.Suggestion[]> => {
try {
let cmd = `aws cloudwatch ${command}`;
let args = ["cloudwatch", command];

for (let i = 0; i < options.length; i++) {
const option = options[i];
Expand All @@ -193,12 +193,15 @@ const listCustomGenerator = async (
continue;
}
const param = tokens[idx + 1];
cmd += ` ${option} ${param}`;
args = [...args, option, param];
}

const out = await executeShellCommand(cmd);
const { stdout } = await executeShellCommand({
command: "aws",
args,
});

const list = JSON.parse(out)[parentKey];
const list = JSON.parse(stdout)[parentKey];

if (!Array.isArray(list)) {
return [
Expand All @@ -209,15 +212,13 @@ const listCustomGenerator = async (
];
}

return list
.map((elm) => {
const name = (childKey ? elm[childKey] : elm) as string;
return {
name,
icon: "fig://icon?type=aws",
};
})
.filter(uniqueNames);
return list.map((elm) => {
const name = (childKey ? elm[childKey] : elm) as string;
return {
name,
icon: "fig://icon?type=aws",
};
});
} catch (e) {
console.log(e);
}
Expand All @@ -226,7 +227,7 @@ const listCustomGenerator = async (

const listDimensionTypes = async (
tokens: string[],
executeShellCommand: Fig.ExecuteShellCommandFunction,
executeShellCommand: Fig.ExecuteCommandFunction,
command: string,
option: string,
parentKey: string,
Expand All @@ -237,11 +238,13 @@ const listDimensionTypes = async (
if (idx < 0) {
return [];
}
const cmd = `aws cloudwatch ${command} ${option} ${tokens[idx + 1]}`;

const out = await executeShellCommand(cmd);
const { stdout } = await executeShellCommand({
command: "aws",
args: ["cloudwatch", command, option, tokens[idx + 1]],
});

const metrics = JSON.parse(out)[parentKey];
const metrics = JSON.parse(stdout)[parentKey];

// traverse JSON & compose key-value style suggestion
return metrics
Expand All @@ -265,22 +268,29 @@ const listDimensionTypes = async (

const MultiSuggestionsGenerator = async (
tokens: string[],
executeShellCommand: Fig.ExecuteShellCommandFunction,
enabled: Record<string, string>[]
executeShellCommand: Fig.ExecuteCommandFunction,
enabled: {
command: string[];
parentKey: string;
childKey: string;
}[]
) => {
try {
const list: Fig.Suggestion[][] = [];
const promises: Promise<string>[] = [];
for (let i = 0; i < enabled.length; i++) {
promises[i] = executeShellCommand(enabled[i]["command"]);
promises[i] = executeShellCommand({
command: "aws",
args: enabled[i].command,
}).then(({ stdout }) => stdout);
}
const result = await Promise.all(promises);

for (let i = 0; i < enabled.length; i++) {
list[i] = postPrecessGenerator(
result[i],
enabled[i]["parentKey"],
enabled[i]["childKey"]
enabled[i].parentKey,
enabled[i].childKey
);
}

Expand All @@ -293,12 +303,15 @@ const MultiSuggestionsGenerator = async (

const getResultList = async (
tokens: string[],
executeShellCommand: Fig.ExecuteShellCommandFunction,
command: string,
executeShellCommand: Fig.ExecuteCommandFunction,
args: string[],
key: string
): Promise<Fig.Suggestion[]> => {
const out = await executeShellCommand(command);
return JSON.parse(out)[key];
const { stdout } = await executeShellCommand({
command: "aws",
args,
});
return JSON.parse(stdout)[key];
};

const _prefixFile = "file://";
Expand Down Expand Up @@ -535,19 +548,22 @@ const generators: Record<string, Fig.Generator> = {
// individually, to get an ARN
custom: async function (tokens, executeShellCommand) {
// get list of stream names
const result = await Promise.all([
getResultList(
tokens,
executeShellCommand,
"aws firehose list-delivery-streams",
"DeliveryStreamNames"
),
]);
const result = await getResultList(
tokens,
executeShellCommand,
["firehose", "list-delivery-streams"],
"DeliveryStreamNames"
);

// construct "query"
const objects = result.flat().map((stream) => {
return {
command: `aws firehose describe-delivery-stream --delivery-stream-name ${stream}`,
command: [
"firehose",
"describe-delivery-stream",
"--delivery-stream-name",
Array.isArray(stream.name) ? stream.name[0] : stream.name,
],
parentKey: "DeliveryStreamDescription",
childKey: "DeliveryStreamARN",
};
Expand Down
22 changes: 13 additions & 9 deletions src/aws/eks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ const postPrecessGenerator = (

const listCustomGenerator = async (
tokens: string[],
executeShellCommand: Fig.ExecuteShellCommandFunction,
executeShellCommand: Fig.ExecuteCommandFunction,
command: string,
options: string[],
parentKey: string,
childKey = ""
): Promise<Fig.Suggestion[]> => {
try {
let cmd = `aws eks ${command}`;
let args = ["eks", command];

for (let i = 0; i < options.length; i++) {
const option = options[i];
Expand All @@ -48,12 +48,15 @@ const listCustomGenerator = async (
continue;
}
const param = tokens[idx + 1];
cmd += ` ${option} ${param}`;
args = [...args, option, param];
}

const out = await executeShellCommand(cmd);
const { stdout } = await executeShellCommand({
command: "aws",
args,
});

const list = JSON.parse(out)[parentKey];
const list = JSON.parse(stdout)[parentKey];

if (!Array.isArray(list)) {
return [
Expand Down Expand Up @@ -313,10 +316,11 @@ const generators: Record<string, Fig.Generator> = {
}
const param = tokens[idx + 1];

const out = await executeShellCommand(
`aws eks describe-cluster --name ${param}`
);
const cluster = JSON.parse(out)["cluster"];
const { stdout } = await executeShellCommand({
command: "aws",
args: ["eks", "describe-cluster", "--name", param],
});
const cluster = JSON.parse(stdout)["cluster"];
const subnets = cluster["resourcesVpcConfig"]["subnetIds"];
return subnets.map((subnet) => {
return {
Expand Down
42 changes: 26 additions & 16 deletions src/aws/iam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,15 @@ const awsPrincipals = [
];

interface Identity {
command: string;
command: string[];
parentKey: string;
childKey: string;
}

const identityStruct: Identity[] = [
{ command: "aws iam list-users", parentKey: "Users", childKey: "Arn" },
{ command: "aws iam list-groups", parentKey: "Groups", childKey: "Arn" },
{ command: "aws iam list-roles", parentKey: "Roles", childKey: "Arn" },
{ command: ["iam", "list-users"], parentKey: "Users", childKey: "Arn" },
{ command: ["iam", "list-groups"], parentKey: "Groups", childKey: "Arn" },
{ command: ["iam", "list-roles"], parentKey: "Roles", childKey: "Arn" },
];

const _prefixFile = "file://";
Expand Down Expand Up @@ -288,7 +288,7 @@ const filterWithPrefix = (token: string, prefix: string): string => {

const listCustomGenerator = async (
tokens: string[],
executeShellCommand: Fig.ExecuteShellCommandFunction,
executeShellCommand: Fig.ExecuteCommandFunction,
command: string,
option: string,
parentKey: string,
Expand All @@ -300,11 +300,12 @@ const listCustomGenerator = async (
return [];
}
const param = tokens[idx + 1];
const out = await executeShellCommand(
`aws iam ${command} ${option} ${param}`
);
const { stdout } = await executeShellCommand({
command: "aws",
args: ["iam", command, option, param],
});

const policies = JSON.parse(out)[parentKey];
const policies = JSON.parse(stdout)[parentKey];
return policies.map((elm) => (childKey ? elm[childKey] : elm));
} catch (e) {
console.log(e);
Expand All @@ -328,14 +329,17 @@ const postPrecessGenerator = (

const MultiSuggestionsGenerator = async (
tokens: string[],
executeShellCommand: Fig.ExecuteShellCommandFunction,
executeShellCommand: Fig.ExecuteCommandFunction,
enabled: Identity[]
) => {
try {
const list: Fig.Suggestion[][] = [];
const promises: Promise<string>[] = [];
for (let i = 0; i < enabled.length; i++) {
promises[i] = executeShellCommand(enabled[i]["command"]);
promises[i] = executeShellCommand({
command: "aws",
args: enabled[i].command,
}).then(({ stdout }) => stdout);
}

const result = await Promise.all(promises);
Expand Down Expand Up @@ -434,11 +438,17 @@ const generators: Record<string, Fig.Generator> = {
return [];
}
const param = tokens[idx + 1];
const out = await executeShellCommand(
`aws iam get-instance-profile --instance-profile-name ${param}`
);
const { stdout } = await executeShellCommand({
command: "aws",
args: [
"iam",
"get-instance-profile",
"--instance-profile-name",
param,
],
});

const policies = JSON.parse(out as string)["InstanceProfile"];
const policies = JSON.parse(stdout)["InstanceProfile"];
return policies["Roles"].map((elm) => {
return {
name: elm["RoleName"],
Expand Down Expand Up @@ -800,7 +810,7 @@ const generators: Record<string, Fig.Generator> = {
return MultiSuggestionsGenerator(tokens, executeShellCommand, [
...identityStruct,
{
command: "aws iam list-policies --scope Local",
command: ["iam", "list-policies", "--scope", "Local"],
parentKey: "Policies",
childKey: "Arn",
},
Expand Down
Loading