Skip to content

Commit

Permalink
refactor to add command
Browse files Browse the repository at this point in the history
  • Loading branch information
uriva committed Dec 24, 2023
1 parent a91acff commit 2543670
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 31 deletions.
30 changes: 30 additions & 0 deletions cli/src/accessToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Path } from "https://deno.land/x/[email protected]/mod.ts";
import homeDir from "https://deno.land/x/[email protected]/home_dir/mod.ts";

const rejectError = (msg: string) => Promise.reject(new Error(msg));

const pathToFileInHomeDir =
(dirName: string, filename: string) => async (): Promise<Path> => {
const home = homeDir();
if (home === null) {
return rejectError("Couldn't find your home directory.");
}
const configPath = new Path(home).push(dirName);
return configPath.exists || (await configPath.mkDir())
? configPath.push(filename)
: rejectError(`Couldn't create the "${dirName}" directory`);
};

const getAccessTokenPath = pathToFileInHomeDir(".rmmbr", "access_token");

export const writeAccessToken = (accessToken: string) =>
getAccessTokenPath()
.then((path) => Deno.writeTextFile(path.toString(), accessToken));

export const getAccessToken = (): Promise<string> =>
getAccessTokenPath().then(
(path) =>
path.exists
? Deno.readTextFile(path.toString())
: rejectError('Not logged-in, run the "login" command first.'),
);
16 changes: 0 additions & 16 deletions cli/src/accessTokenPath.ts

This file was deleted.

14 changes: 2 additions & 12 deletions cli/src/apiToken.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getAccessTokenPath } from "./accessTokenPath.ts";
import { getAccessToken } from "./accessToken.ts";

const serverURL = Deno.env.get("RMMBR_SERVER");

Expand All @@ -14,9 +14,7 @@ export const apiToken = (
const [action, args] =
Object.entries(cmd).find(([action]) => action in commandMapping) ||
Deno.exit();

return getAccessToken()
.then(commandMapping[action](args));
return getAccessToken().then(commandMapping[action](args));
};

const apiTokenRequest = (
Expand All @@ -35,14 +33,6 @@ const apiTokenRequest = (
: Promise.reject(await response.text()),
);

const getAccessToken = (): Promise<string> =>
getAccessTokenPath().then(
(path) =>
path.exists
? Deno.readTextFile(path.toString())
: Promise.reject('Not logged-in, run the "login" command first.'),
);

const createApiToken = apiTokenRequest("POST", { action: "create" });
const listApiTokens = apiTokenRequest("GET", undefined);

Expand Down
14 changes: 14 additions & 0 deletions cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ import yargs from "https://deno.land/x/[email protected]/deno.ts";
const args = yargs(Deno.args)
.scriptName("rmmbr")
.command("login", "Authenticate the CLI")
.command("key", "Manage cache keys", (yargs: any) =>
yargs
.option("get", {
alias: "g",
description: "Get a value for a given key: `<secret>:<input json>``",
string: true,
})
.option("delete", {
alias: "d",
description:
"Deletes a value for a given key: `<secret>:<input json>``",
string: true,
}))
.command(
"token",
"Manage API tokens",
Expand Down Expand Up @@ -50,6 +63,7 @@ const args = yargs(Deno.args)
const command = args._[0];
const commands: Record<string, () => Promise<string>> = {
login,
key: () => keyManipulation(args),
"token": () => apiToken(args),
secret: () => Promise.resolve(randomBytes(32).toString("base64url") + "="),
};
Expand Down
23 changes: 23 additions & 0 deletions cli/src/keyManipulation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { getAccessToken } from "./accessToken.ts";

const commandMapping: Record<
string,
((..._: string[]) => (_: string) => Promise<string>)
> = {
delete: (apiToken: string) => (secretAndKey: string) =>
Promise.resolve("not yet implemented"),
get: (apiToken: string) => (secretAndKey: string) =>
Promise.resolve("not yet implemented"),
};

type APITokenInterface =
| { delete: string }
| { get: true };

export const keyManipulation = (cmd: APITokenInterface) => {
const [action, args] =
Object.entries(cmd).find(([action]) => action in commandMapping) ||
Deno.exit();
return getAccessToken()
.then(commandMapping[action](args));
};
5 changes: 2 additions & 3 deletions cli/src/login.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { delay } from "https://deno.land/[email protected]/async/delay.ts";
import { getAccessTokenPath } from "./accessTokenPath.ts";
import open from "npm:[email protected]";
import { writeAccessToken } from "./accessToken.ts";

const clientId = "ARXipK0k64GivxcX9UVUWMp9g7ywQsqO";
const auth0Tenant = "https://dev-gy4q5ggc5zaobhym.us.auth0.com";
Expand Down Expand Up @@ -47,8 +47,7 @@ and confirm to finish the login.

const { access_token, error } = await response.json();
if (access_token) {
return getAccessTokenPath()
.then((path) => Deno.writeTextFile(path.toString(), access_token))
return writeAccessToken(access_token)
.then(() => "Now logged in.");
}
if (error === "authorization_pending") {
Expand Down

0 comments on commit 2543670

Please sign in to comment.