Fix CLI logout and add list-profiles command
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"trigger.dev": patch
|
||||
---
|
||||
|
||||
Fix CLI logout and add list-profiles command
|
||||
@@ -7,6 +7,7 @@ import { configureLogoutCommand } from "../commands/logout.js";
|
||||
import { configureWhoamiCommand } from "../commands/whoami.js";
|
||||
import { COMMAND_NAME } from "../consts.js";
|
||||
import { getVersion } from "../utilities/getVersion.js";
|
||||
import { configureListProfilesCommand } from "../commands/list-profiles.js";
|
||||
|
||||
export const program = new Command();
|
||||
|
||||
@@ -21,3 +22,4 @@ configureDevCommand(program);
|
||||
configureDeployCommand(program);
|
||||
configureWhoamiCommand(program);
|
||||
configureLogoutCommand(program);
|
||||
configureListProfilesCommand(program);
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
import { Command } from "commander";
|
||||
import {
|
||||
deleteAuthConfigProfile,
|
||||
readAuthConfigFile,
|
||||
readAuthConfigProfile,
|
||||
writeAuthConfigProfile,
|
||||
} from "../utilities/configFiles.js";
|
||||
import { logger } from "../utilities/logger.js";
|
||||
import {
|
||||
CommonCommandOptions,
|
||||
commonOptions,
|
||||
handleTelemetry,
|
||||
wrapCommandAction,
|
||||
} from "../cli/common.js";
|
||||
import { printInitialBanner } from "../utilities/initialBanner.js";
|
||||
import { z } from "zod";
|
||||
import { chalkGrey } from "../utilities/cliOutput.js";
|
||||
import { log, outro, text } from "@clack/prompts";
|
||||
|
||||
const ListProfilesOptions = CommonCommandOptions;
|
||||
|
||||
type ListProfilesOptions = z.infer<typeof ListProfilesOptions>;
|
||||
|
||||
export function configureListProfilesCommand(program: Command) {
|
||||
return program
|
||||
.command("list-profiles")
|
||||
.description("List all of your CLI profiles")
|
||||
.option(
|
||||
"-l, --log-level <level>",
|
||||
"The CLI log level to use (debug, info, log, warn, error, none). This does not effect the log level of your trigger.dev tasks.",
|
||||
"log"
|
||||
)
|
||||
.option("--skip-telemetry", "Opt-out of sending telemetry")
|
||||
.action(async (options) => {
|
||||
await handleTelemetry(async () => {
|
||||
await printInitialBanner(true);
|
||||
await listProfilesCommand(options);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export async function listProfilesCommand(options: unknown) {
|
||||
return await wrapCommandAction("listProfiles", ListProfilesOptions, options, async (opts) => {
|
||||
return await listProfiles(opts);
|
||||
});
|
||||
}
|
||||
|
||||
export async function listProfiles(options: ListProfilesOptions) {
|
||||
const authConfig = readAuthConfigFile();
|
||||
|
||||
if (!authConfig) {
|
||||
logger.info("No profiles found");
|
||||
return;
|
||||
}
|
||||
|
||||
const profiles = Object.keys(authConfig);
|
||||
|
||||
log.message("Profiles:");
|
||||
|
||||
for (const profile of profiles) {
|
||||
const profileConfig = authConfig[profile];
|
||||
|
||||
log.info(`${profile}${profileConfig?.apiUrl ? ` - ${chalkGrey(profileConfig.apiUrl)}` : ""}`);
|
||||
}
|
||||
|
||||
outro("Retrieve account info by running whoami --profile <profile>");
|
||||
}
|
||||
@@ -1,24 +1,28 @@
|
||||
import { Command } from "commander";
|
||||
import { readAuthConfigProfile, writeAuthConfigProfile } from "../utilities/configFiles.js";
|
||||
import { logger } from "../utilities/logger.js";
|
||||
import { CommonCommandOptions, commonOptions, handleTelemetry, wrapCommandAction } from "../cli/common.js";
|
||||
import { printInitialBanner } from "../utilities/initialBanner.js";
|
||||
import { z } from "zod";
|
||||
import {
|
||||
CommonCommandOptions,
|
||||
commonOptions,
|
||||
handleTelemetry,
|
||||
wrapCommandAction,
|
||||
} from "../cli/common.js";
|
||||
import { deleteAuthConfigProfile, readAuthConfigProfile } from "../utilities/configFiles.js";
|
||||
import { printInitialBanner } from "../utilities/initialBanner.js";
|
||||
import { logger } from "../utilities/logger.js";
|
||||
|
||||
const LogoutCommandOptions = CommonCommandOptions;
|
||||
|
||||
type LogoutCommandOptions = z.infer<typeof LogoutCommandOptions>;
|
||||
|
||||
export function configureLogoutCommand(program: Command) {
|
||||
return commonOptions(program
|
||||
.command("logout")
|
||||
.description("Logout of Trigger.dev"))
|
||||
.action(async (options) => {
|
||||
return commonOptions(program.command("logout").description("Logout of Trigger.dev")).action(
|
||||
async (options) => {
|
||||
await handleTelemetry(async () => {
|
||||
await printInitialBanner(false);
|
||||
await logoutCommand(options);
|
||||
});
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
export async function logoutCommand(options: unknown) {
|
||||
@@ -31,11 +35,11 @@ export async function logout(options: LogoutCommandOptions) {
|
||||
const config = readAuthConfigProfile(options.profile);
|
||||
|
||||
if (!config?.accessToken) {
|
||||
logger.info(`You are already logged out [${options.profile ?? "default"}]`);
|
||||
logger.info(`You are already logged out [${options.profile}]`);
|
||||
return;
|
||||
}
|
||||
|
||||
writeAuthConfigProfile({ ...config, accessToken: undefined, apiUrl: undefined }, options.profile);
|
||||
deleteAuthConfigProfile(options.profile);
|
||||
|
||||
logger.info(`Logged out of Trigger.dev [${options.profile ?? "default"}]`);
|
||||
logger.info(`Logged out of Trigger.dev [${options.profile}]`);
|
||||
}
|
||||
|
||||
@@ -56,7 +56,15 @@ export function readAuthConfigProfile(profile: string = "default"): UserAuthConf
|
||||
}
|
||||
}
|
||||
|
||||
function readAuthConfigFile(): UserAuthConfigFile | undefined {
|
||||
export function deleteAuthConfigProfile(profile: string = "default") {
|
||||
const existingConfig = readAuthConfigFile() || {};
|
||||
|
||||
delete existingConfig[profile];
|
||||
|
||||
writeAuthConfigFile(existingConfig);
|
||||
}
|
||||
|
||||
export function readAuthConfigFile(): UserAuthConfigFile | undefined {
|
||||
try {
|
||||
const authConfigFilePath = getAuthConfigFilePath();
|
||||
|
||||
@@ -71,7 +79,7 @@ function readAuthConfigFile(): UserAuthConfigFile | undefined {
|
||||
}
|
||||
}
|
||||
|
||||
function writeAuthConfigFile(config: UserAuthConfigFile) {
|
||||
export function writeAuthConfigFile(config: UserAuthConfigFile) {
|
||||
const authConfigFilePath = getAuthConfigFilePath();
|
||||
mkdirSync(path.dirname(authConfigFilePath), {
|
||||
recursive: true,
|
||||
|
||||
Reference in New Issue
Block a user