Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
add queries list command
Browse files Browse the repository at this point in the history
  • Loading branch information
boristane committed Mar 20, 2022
1 parent cd9217a commit 522c8b5
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 19 deletions.
52 changes: 34 additions & 18 deletions src/commands/queries.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,43 @@
import { Arguments, CommandBuilder } from "yargs";
import chalk from "chalk";
import { EOL } from "os";
import { authenticate, baseOptions, userConfigNotFound } from "../shared";
import { Options } from "./queries/types";
import handlers from "./queries/handlers";
import spinner from "../services/spinner/index";

interface Options {
name: string;
upper?: boolean;
}

export const command = "greet <name>";
export const desc = "Greet <name> with Hello";
export const command = "queries <subcommand> [parameters]";
export const desc = "Operations on queries";

export const builder: CommandBuilder<Options, Options> = (yargs) => {
return yargs
.options({
upper: { type: "boolean" },
.options(baseOptions)
.positional("subcommand", {
type: "string",
choices: ["list", "create"]
})
.positional("name", { type: "string", demandOption: true });
.example([
["$0 queries <subcommand>"],
["$0 queries <subcommand> --profile prod"]
]);
};

export function handler(argv: Arguments<Options>) {
const { name, upper } = argv;
const greeting = `Hello ${name}`;
const s = upper ? greeting.toUpperCase() : greeting;
process.stdout.write(chalk.green(chalk.bold(s)) + EOL);
process.exit(0);
export async function handler(argv: Arguments<Options>) {
const { subcommand, profile = "default", json, } = argv;
spinner.init(!!argv.quiet)
await authenticate(profile);

switch (subcommand) {
case subCommand.list:
await handlers.list(!!json)
break;
case subCommand.create:
console.log("create");
break;
default:
process.exit(1);
}
}

export enum subCommand {
list = "list",
create = "create",
}
15 changes: 15 additions & 0 deletions src/commands/queries/handlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import spinner from "../../services/spinner/index";
import api from "../../services/api/api";
import outputs from "./outputs";

async function list(json: boolean) {
const s = spinner.get();
s.start("Fetching your queries");
const queries = await api.queriesList();
s.succeed();
outputs.list(queries, json);
}

export default {
list,
}
Empty file removed src/commands/queries/list.ts
Empty file.
15 changes: 15 additions & 0 deletions src/commands/queries/outputs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Query } from "../../services/api/paths/queries";

function list(queries: Query[], json: boolean) {
if (json) {
console.log("json");
console.log(JSON.stringify(queries));
return;
}
console.log("table");
console.log(queries);
}

export default {
list,
}
4 changes: 4 additions & 0 deletions src/commands/queries/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type { BaseOptions } from "../../shared";

export interface Options extends BaseOptions {
}
4 changes: 3 additions & 1 deletion src/services/api/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import auth from "./auth/auth";
import auth from "./paths/auth";
import queries from "./paths/queries";

export default {
...auth,
...queries,
};
45 changes: 45 additions & 0 deletions src/services/api/paths/queries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { client } from "../clients";


export interface QueryParameters {
groupBys?: string[];
namespaces?: Array<{
type: string,
value: string;
}>;
filters?: Array<QueryFilter>;
filterCombination: "AND" | "OR";
calculations: Array<{
key: string;
operator: string;
}>;
}

export interface QueryFilter {
key: string;
operation: string;
value: string | number | boolean | { key: string; };
type: string;
}

export interface Query {
parameters: QueryParameters,
id: string;
name: string;
description: string;
workspaceId: string;
environmentId: string;
userId: string;
created?: string;
updated?: string;
}


async function queriesList(): Promise<Query[]> {
const res = (await client.get("/queries")).data;
return res.queries;
}

export default {
queriesList,
}

0 comments on commit 522c8b5

Please sign in to comment.