sesterce-cli
Version:
A powerful command-line interface tool for managing Sesterce Cloud services. Sesterce CLI provides easy access to GPU cloud instances, AI inference services, container registries, and SSH key management directly from your terminal.
59 lines (45 loc) • 1.59 kB
text/typescript
import fs from "fs";
import os from "os";
import path from "path";
export function getApiKey(profile: string = "default"): string {
const home = os.homedir();
const credentialsPath = path.join(home, ".sesterce", "credentials");
if (!fs.existsSync(credentialsPath)) {
throw new Error(`Credentials file not found at ${credentialsPath}`);
}
const credentials = fs.readFileSync(credentialsPath, "utf8");
const profiles = parseCredentialsFile(credentials);
if (!profiles[profile]) {
throw new Error(`Profile '${profile}' not found in credentials file`);
}
if (!profiles[profile].sesterce_api_key) {
throw new Error(`API key not found for profile '${profile}'`);
}
return profiles[profile].sesterce_api_key;
}
function parseCredentialsFile(
content: string
): Record<string, Record<string, string>> {
const profiles: Record<string, Record<string, string>> = {};
let currentProfile = "";
const lines = content.split("\n");
for (const line of lines) {
const trimmedLine = line.trim();
// Skip empty lines and comments
if (!trimmedLine || trimmedLine.startsWith("#")) {
continue;
}
// Check if it's a profile section
if (trimmedLine.startsWith("[") && trimmedLine.endsWith("]")) {
currentProfile = trimmedLine.slice(1, -1);
profiles[currentProfile] = {};
continue;
}
// Parse key-value pairs
if (currentProfile && trimmedLine.includes("=")) {
const [key, value] = trimmedLine.split("=", 2);
profiles[currentProfile][key.trim()] = value.trim();
}
}
return profiles;
}