From fb6ba239931f52e3c622c88965f721a045c5fb2c Mon Sep 17 00:00:00 2001 From: Sladuca Date: Wed, 4 Dec 2024 17:26:39 -0800 Subject: [PATCH] sf me --- src/index.ts | 2 ++ src/lib/dev.ts | 36 ------------------------------------ src/lib/me.ts | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 36 deletions(-) create mode 100644 src/lib/me.ts diff --git a/src/index.ts b/src/index.ts index 194714a..a6f426a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,6 +14,7 @@ import { registerDown, registerUp } from "./lib/updown.tsx"; import { registerUpgrade } from "./lib/upgrade.ts"; import { registerClusters } from "./lib/clusters/clusters.tsx"; import { checkVersion } from "./checkVersion.ts"; +import { registerMe } from "./lib/me.ts"; const program = new Command(); @@ -36,6 +37,7 @@ registerUpgrade(program); registerUp(program); registerDown(program); registerClusters(program); +registerMe(program); // (development commands) registerDev(program); diff --git a/src/lib/dev.ts b/src/lib/dev.ts index 724d6f0..7c29dbe 100644 --- a/src/lib/dev.ts +++ b/src/lib/dev.ts @@ -37,14 +37,6 @@ export function registerDev(program: Command) { process.exit(0); }); - // self - program.command("me").action(async () => { - const accountId = await getLoggedInAccountId(); - console.log(accountId); - - process.exit(0); - }); - // connection program.command("ping").action(async () => { const data = await pingServer(); @@ -174,34 +166,6 @@ function colorDiffEpochs(epochStrings: string[]): string[] { // -- -async function getLoggedInAccountId() { - const loggedIn = await isLoggedIn(); - if (!loggedIn) { - logLoginMessageAndQuit(); - } - const config = await loadConfig(); - - const response = await fetch(await getApiUrl("me"), { - method: "GET", - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${config.auth_token}`, - }, - }); - if (!response.ok) { - if (response.status === 401) { - logSessionTokenExpiredAndQuit(); - } - console.log(response); - - logAndQuit("Failed to fetch account info"); - } - - const data = await response.json(); - - return data.id; -} - async function pingServer() { const loggedIn = await isLoggedIn(); if (!loggedIn) { diff --git a/src/lib/me.ts b/src/lib/me.ts new file mode 100644 index 0000000..a0b619a --- /dev/null +++ b/src/lib/me.ts @@ -0,0 +1,46 @@ + + +import type { Command } from "commander"; +import { + logSessionTokenExpiredAndQuit, + logLoginMessageAndQuit, +} from "../helpers/errors.ts"; +import { isLoggedIn, loadConfig } from "../helpers/config.ts"; +import { logAndQuit } from "../helpers/errors.ts"; +import { getApiUrl } from "../helpers/urls.ts"; + +export function registerMe(program: Command) { + program.command("me").action(async () => { + const accountId = await getLoggedInAccountId(); + console.log(accountId); + + process.exit(0); + }); +} + +async function getLoggedInAccountId() { + const loggedIn = await isLoggedIn(); + if (!loggedIn) { + logLoginMessageAndQuit(); + } + const config = await loadConfig(); + + const response = await fetch(await getApiUrl("me"), { + method: "GET", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${config.auth_token}`, + }, + }); + if (!response.ok) { + if (response.status === 401) { + logSessionTokenExpiredAndQuit(); + } + + logAndQuit("Failed to fetch account info"); + } + + const data = await response.json(); + + return data.id; +}