-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
71 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'), | ||
); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
|
@@ -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") + "="), | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
|
@@ -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") { | ||
|