Skip to content

Commit

Permalink
add sf me (#45)
Browse files Browse the repository at this point in the history
* sf me

* fmt
  • Loading branch information
Sladuca authored Dec 5, 2024
1 parent 7a29b6e commit aa499fb
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 38 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@
"typescript": "^5.6.2"
},
"version": "0.1.4"
}
}
2 changes: 1 addition & 1 deletion src/helpers/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const ConfigDefaults = process.env.IS_DEVELOPMENT_CLI_ENV
// --

export async function saveConfig(
config: Partial<Config>
config: Partial<Config>,
): Promise<{ success: boolean }> {
const configPath = getConfigPath();
const configDir = join(homedir(), ".sfcompute");
Expand Down
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
44 changes: 44 additions & 0 deletions src/lib/me.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { Command } from "commander";
import {
logLoginMessageAndQuit,
logSessionTokenExpiredAndQuit,
} 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 aa499fb

Please sign in to comment.