Skip to content

Commit

Permalink
sf me
Browse files Browse the repository at this point in the history
  • Loading branch information
Sladuca committed Dec 5, 2024
1 parent 7a29b6e commit fb6ba23
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 36 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -36,6 +37,7 @@ registerUpgrade(program);
registerUp(program);
registerDown(program);
registerClusters(program);
registerMe(program);

// (development commands)
registerDev(program);
Expand Down
36 changes: 0 additions & 36 deletions src/lib/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down
46 changes: 46 additions & 0 deletions src/lib/me.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit fb6ba23

Please sign in to comment.