Skip to content

Commit

Permalink
Merge pull request #9 from fluentci-io/feat/flu-11-cli-login
Browse files Browse the repository at this point in the history
feat: implement command line `login`
  • Loading branch information
tsirysndr authored Dec 22, 2023
2 parents 51a897e + 4c8af20 commit a41d70f
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
47 changes: 47 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ export { z } from "https://deno.land/x/[email protected]/mod.ts";
export { decompress } from "https://deno.land/x/[email protected]/mod.ts";
export { existsSync } from "https://deno.land/[email protected]/fs/exists.ts";
export { load } from "https://deno.land/[email protected]/dotenv/mod.ts";
export { Secret } from "https://deno.land/x/[email protected]/prompt/secret.ts";
import dir from "https://deno.land/x/[email protected]/mod.ts";
export { dir };
5 changes: 5 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import docs from "./src/cmd/docs.ts";
import cache from "./src/cmd/cache.ts";
import doctor from "./src/cmd/doctor.ts";
import showEnvs from "./src/cmd/env.ts";
import login from "./src/cmd/login.ts";
import { brightGreen } from "./deps.ts";

export async function main() {
Expand Down Expand Up @@ -188,6 +189,10 @@ export async function main() {
.action(async function () {
await showEnvs();
})
.command("login", "Login to FluentCI")
.action(async function () {
await login();
})
.parse(Deno.args);
}

Expand Down
24 changes: 24 additions & 0 deletions src/cmd/login.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Secret, brightGreen, dir, red } from "../../deps.ts";

async function login() {
console.log(
`You can generate an access token from ${brightGreen(
"https://app.fluentci.io/settings/tokens"
)}`
);
const accessToken: string = await Secret.prompt("Enter your access token: ");
const status = await fetch(
`https://api.fluentci.io/validate?token=${accessToken}`
).then((res) => res.status);

if (status !== 200) {
console.log(`${red("[✗]")} Invalid access token`);
Deno.exit(1);
}

Deno.mkdirSync(`${dir("home")}/.fluentci`, { recursive: true });
Deno.writeTextFileSync(`${dir("home")}/.fluentci/access-token`, accessToken);
console.log(`${brightGreen("[✓]")} Logged in successfully`);
}

export default login;

0 comments on commit a41d70f

Please sign in to comment.