Skip to content

Commit 3901a04

Browse files
committed
improve switch command
1 parent 9dda081 commit 3901a04

1 file changed

Lines changed: 35 additions & 9 deletions

File tree

packages/cli-v3/src/commands/switch.ts

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { intro, isCancel, outro, select } from "@clack/prompts";
1+
import { intro, isCancel, log, outro, select } from "@clack/prompts";
22
import { Command } from "commander";
33
import { z } from "zod";
44
import {
@@ -10,8 +10,8 @@ import {
1010
import { chalkGrey } from "../utilities/cliOutput.js";
1111
import { readAuthConfigFile, writeAuthConfigCurrentProfileName } from "../utilities/configFiles.js";
1212
import { printInitialBanner } from "../utilities/initialBanner.js";
13-
import { logger } from "../utilities/logger.js";
1413
import { CLOUD_API_URL } from "../consts.js";
14+
import { hasTTY, isCI } from "std-env";
1515

1616
const SwitchProfilesOptions = CommonCommandOptions.pick({
1717
logLevel: true,
@@ -24,34 +24,35 @@ export function configureSwitchProfilesCommand(program: Command) {
2424
return program
2525
.command("switch")
2626
.description("Set your default CLI profile")
27+
.argument("[profile]", "The profile to switch to. Use interactive mode if not provided.")
2728
.option(
2829
"-l, --log-level <level>",
2930
"The CLI log level to use (debug, info, log, warn, error, none). This does not effect the log level of your trigger.dev tasks.",
3031
"log"
3132
)
3233
.option("--skip-telemetry", "Opt-out of sending telemetry")
33-
.action(async (options) => {
34+
.action(async (profile, options) => {
3435
await handleTelemetry(async () => {
35-
await switchProfilesCommand(options);
36+
await switchProfilesCommand(profile, options);
3637
});
3738
});
3839
}
3940

40-
export async function switchProfilesCommand(options: unknown) {
41+
export async function switchProfilesCommand(profile: string | undefined, options: unknown) {
4142
return await wrapCommandAction("switch", SwitchProfilesOptions, options, async (opts) => {
4243
await printInitialBanner(false);
43-
return await switchProfiles(opts);
44+
return await switchProfiles(profile, opts);
4445
});
4546
}
4647

47-
export async function switchProfiles(options: SwitchProfilesOptions) {
48+
export async function switchProfiles(profile: string | undefined, options: SwitchProfilesOptions) {
4849
intro("Switch profiles");
4950

5051
const authConfig = readAuthConfigFile();
5152

5253
if (!authConfig) {
53-
logger.info("No profiles found");
54-
return;
54+
outro("Failed to switch profiles");
55+
throw new Error("No profiles found. Run `login` to create a profile.");
5556
}
5657

5758
const profileNames = Object.keys(authConfig.profiles).sort((a, b) => {
@@ -62,6 +63,26 @@ export async function switchProfiles(options: SwitchProfilesOptions) {
6263
return a.localeCompare(b);
6364
});
6465

66+
if (profile) {
67+
if (!authConfig.profiles[profile]) {
68+
if (isCI || !hasTTY) {
69+
outro("Failed to switch profiles");
70+
throw new Error(`Profile "${profile}" not found. Run \`login\` to create a profile.`);
71+
} else {
72+
log.message(`Profile "${profile}" not found, showing available profiles`);
73+
}
74+
} else {
75+
if (authConfig.currentProfile === profile) {
76+
outro(`Already using ${profile}`);
77+
return;
78+
}
79+
80+
writeAuthConfigCurrentProfileName(profile);
81+
outro(`Switched to ${profile}`);
82+
return;
83+
}
84+
}
85+
6586
const profileSelection = await select({
6687
message: "Please select a new profile",
6788
initialValue: authConfig.currentProfile,
@@ -79,6 +100,11 @@ export async function switchProfiles(options: SwitchProfilesOptions) {
79100
throw new OutroCommandError();
80101
}
81102

103+
if (authConfig.currentProfile === profileSelection) {
104+
outro(`Already using ${profileSelection}`);
105+
return;
106+
}
107+
82108
writeAuthConfigCurrentProfileName(profileSelection);
83109

84110
if (profileSelection === authConfig.currentProfile) {

0 commit comments

Comments
 (0)