From a2bd88c4f99fbc4a03294053f37d215bd363b42a Mon Sep 17 00:00:00 2001 From: David Dal Busco Date: Thu, 31 Jul 2025 16:03:25 +0200 Subject: [PATCH 1/3] feat: deprecate juno use and multiple profiles in single config file --- src/commands/use.ts | 57 --------- src/commands/whoami.ts | 2 +- src/configs/cli.config.ts | 185 +-------------------------- src/help/use.help.ts | 29 ----- src/index.ts | 8 -- src/services/controllers.services.ts | 38 ++---- src/types/cli.config.ts | 11 +- 7 files changed, 15 insertions(+), 315 deletions(-) delete mode 100644 src/commands/use.ts delete mode 100644 src/help/use.help.ts diff --git a/src/commands/use.ts b/src/commands/use.ts deleted file mode 100644 index f7556bf9..00000000 --- a/src/commands/use.ts +++ /dev/null @@ -1,57 +0,0 @@ -import {hasArgs, nextArg} from '@junobuild/cli-tools'; -import {green, red} from 'kleur'; -import {deleteUse, getProfiles, getUse, saveUse} from '../configs/cli.config'; - -export const use = async (args?: string[]) => { - if (hasArgs({args, options: ['-l', '--list']})) { - await listProfile(); - return; - } - - await switchProfile(args); -}; - -const listProfile = async () => { - const profiles = await getProfiles(); - - if (profiles === undefined) { - console.log('No particular profiles available. Using default.'); - return; - } - - const use = await getUse(); - - console.log('Available profiles:\n'); - console.log( - Object.keys(profiles) - .map((profile) => (profile === use ? `${green(profile)} (currently selected)` : profile)) - .join('\n') - ); -}; - -const switchProfile = async (args?: string[]) => { - const profile = nextArg({args, option: '-p'}) ?? nextArg({args, option: '--profile'}); - - if (profile === undefined) { - console.log(red('No profile provided.')); - return; - } - - if (profile === 'default') { - await deleteUse(); - - console.log(`Now using ${green('default')}.`); - return; - } - - const profiles = await getProfiles(); - - if (profiles?.[profile] === undefined) { - console.log(red('No corresponding profile found.')); - return; - } - - await saveUse(profile); - - console.log(`Now using ${green(profile)}.`); -}; diff --git a/src/commands/whoami.ts b/src/commands/whoami.ts index acf2a988..c69e3dfb 100644 --- a/src/commands/whoami.ts +++ b/src/commands/whoami.ts @@ -18,7 +18,7 @@ const info = async (): Promise<{success: boolean}> => { const profile = await getUse(); if (!isDefaultProfile(profile)) { - console.log(`👤 Profile: ${green(profile!)}`); + console.log(`👤 Profile: ${green(profile)}`); } const token = await getToken(); diff --git a/src/configs/cli.config.ts b/src/configs/cli.config.ts index bd8b0593..0c2b119b 100644 --- a/src/configs/cli.config.ts +++ b/src/configs/cli.config.ts @@ -4,13 +4,7 @@ import type Conf from 'conf'; import {red, yellow} from 'kleur'; import {askForPassword} from '../services/cli.settings.services'; import {getSettingsStore} from '../stores/settings.store'; -import type { - CliConfig, - CliConfigData, - CliOrbiterConfig, - CliProfile, - CliSatelliteConfig -} from '../types/cli.config'; +import type {CliConfig, CliOrbiterConfig, CliSatelliteConfig} from '../types/cli.config'; import {loadConfig} from '../utils/config.utils'; // Save in https://github.com/sindresorhus/env-paths#pathsconfig @@ -43,33 +37,13 @@ export const saveCliConfig = async ({ token, satellites, orbiters, - missionControl, - profile + missionControl }: { token: JsonnableEd25519KeyIdentity; satellites: CliSatelliteConfig[]; orbiters: CliOrbiterConfig[] | null; missionControl: string | null; - profile: CliProfile | null; }) => { - if (!isDefaultProfile(profile)) { - const profiles = await getProfiles(); - - await saveProfiles({ - ...(profiles !== undefined ? profiles : {}), - [profile!]: { - token, - satellites, - ...(orbiters !== null && {orbiters}), - ...(missionControl !== null && {missionControl}) - } - }); - - await saveUse(profile!); - - return; - } - await saveToken(token); await saveCliSatellites(satellites); @@ -80,54 +54,8 @@ export const saveCliConfig = async ({ if (missionControl !== null) { await saveCliMissionControl(missionControl); } - - await deleteUse(); -}; - -// Use / profile - -export const deleteUse = async () => { - await initConfig(); - - // Guard for TypeScript. initConfig ensures config is initialized or exit. - assertNonNullish(config); - - config.delete('use'); -}; -export const saveUse = async (use: CliProfile) => { - await initConfig(); - - // Guard for TypeScript. initConfig ensures config is initialized or exit. - assertNonNullish(config); - - config.set('use', use); -}; -export const getUse = async (): Promise => { - await initConfig(); - - return config?.get('use'); }; -// Profile - -const saveProfiles = async (profiles: Record) => { - await initConfig(); - - // Guard for TypeScript. initConfig ensures config is initialized or exit. - assertNonNullish(config); - - config.set('profiles', profiles); -}; - -export const getProfiles = async (): Promise | undefined> => { - await initConfig(); - - return config?.get('profiles'); -}; - -export const isDefaultProfile = (use: CliProfile | undefined | null): boolean => - use === null || use === undefined || use === 'default'; - // Token const saveToken = async (token: JsonnableEd25519KeyIdentity) => { @@ -142,12 +70,6 @@ const saveToken = async (token: JsonnableEd25519KeyIdentity) => { export const getToken = async (): Promise => { await initConfig(); - const use = await getUse(); - - if (!isDefaultProfile(use)) { - return (await getProfiles())?.[use!]?.token; - } - return config?.get('token'); }; @@ -165,48 +87,13 @@ const saveCliSatellites = async (satellites: CliSatelliteConfig[]) => { export const getCliSatellites = async (): Promise => { await initConfig(); - const use = await getUse(); - - if (!isDefaultProfile(use)) { - return (await getProfiles())?.[use!]?.satellites ?? []; - } - // Guard for TypeScript. initConfig ensures config is initialized or exit. assertNonNullish(config); return config.get('satellites'); }; -export const addCliSatellite = async ({ - satellite, - profile -}: { - satellite: CliSatelliteConfig; - profile: CliProfile | undefined; -}) => { - if (!isDefaultProfile(profile)) { - const profiles = await getProfiles(); - const currentProfile = profiles?.[profile!]; - - if (currentProfile === undefined) { - throw new Error(`The profile must exist.`); - } - - await saveProfiles({ - ...(profiles !== undefined ? profiles : {}), - [profile!]: { - ...currentProfile, - satellites: [ - // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition - ...(currentProfile.satellites ?? []).filter(({p}) => p !== satellite.p), - satellite - ] - } - }); - - return; - } - +export const addCliSatellite = async ({satellite}: {satellite: CliSatelliteConfig}) => { const currentSatellites = await getCliSatellites(); await saveCliSatellites([ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition @@ -229,41 +116,10 @@ const saveCliMissionControl = async (missionControl: string) => { export const getCliMissionControl = async (): Promise => { await initConfig(); - const use = await getUse(); - - if (!isDefaultProfile(use)) { - return (await getProfiles())?.[use!]?.missionControl; - } - return config?.get('missionControl'); }; -export const addCliMissionControl = async ({ - missionControl, - profile -}: { - missionControl: string; - profile: CliProfile | undefined; -}) => { - if (!isDefaultProfile(profile)) { - const profiles = await getProfiles(); - const currentProfile = profiles?.[profile!]; - - if (currentProfile === undefined) { - throw new Error(`The profile must exist.`); - } - - await saveProfiles({ - ...(profiles !== undefined ? profiles : {}), - [profile!]: { - ...currentProfile, - missionControl - } - }); - - return; - } - +export const addCliMissionControl = async ({missionControl}: {missionControl: string}) => { await saveCliMissionControl(missionControl); }; @@ -281,41 +137,10 @@ const saveCliOrbiters = async (orbiters: CliOrbiterConfig[]) => { export const getCliOrbiters = async (): Promise => { await initConfig(); - const use = await getUse(); - - if (!isDefaultProfile(use)) { - return (await getProfiles())?.[use!]?.orbiters; - } - return config?.get('orbiters'); }; -export const addCliOrbiter = async ({ - orbiter, - profile -}: { - orbiter: CliOrbiterConfig; - profile: CliProfile | undefined; -}) => { - if (!isDefaultProfile(profile)) { - const profiles = await getProfiles(); - const currentProfile = profiles?.[profile!]; - - if (currentProfile === undefined) { - throw new Error(`The profile must exist.`); - } - - await saveProfiles({ - ...(profiles !== undefined ? profiles : {}), - [profile!]: { - ...currentProfile, - orbiters: [...(currentProfile.orbiters ?? []).filter(({p}) => p !== orbiter.p), orbiter] - } - }); - - return; - } - +export const addCliOrbiter = async ({orbiter}: {orbiter: CliOrbiterConfig}) => { const currentOrbiters = await getCliOrbiters(); await saveCliOrbiters([...(currentOrbiters ?? []).filter(({p}) => p !== orbiter.p), orbiter]); }; diff --git a/src/help/use.help.ts b/src/help/use.help.ts deleted file mode 100644 index 6bb66a9e..00000000 --- a/src/help/use.help.ts +++ /dev/null @@ -1,29 +0,0 @@ -import {cyan, green, yellow} from 'kleur'; -import {OPTION_HELP, USE_DESCRIPTION} from '../constants/help.constants'; -import {helpOutput} from './common.help'; -import {TITLE} from './help'; - -const usage = `Usage: ${green('juno')} ${cyan('use')} ${yellow('[options]')} - -Options: - ${yellow('-p, --profile')} The profile that should be use. - ${yellow('-l, --list')} What are the available profiles. - ${OPTION_HELP}`; - -const doc = `${USE_DESCRIPTION} - -\`\`\` -${usage} -\`\`\` -`; - -const help = `${TITLE} - -${USE_DESCRIPTION} - -${usage} -`; - -export const logHelpUse = (args?: string[]) => { - console.log(helpOutput(args) === 'doc' ? doc : help); -}; diff --git a/src/index.ts b/src/index.ts index 3ac41ae3..7c9359ea 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,7 +12,6 @@ import {open} from './commands/open'; import {snapshot} from './commands/snapshot'; import {startStop} from './commands/start-stop'; import {upgrade} from './commands/upgrade'; -import {use} from './commands/use'; import {version as versionCommand} from './commands/version'; import {whoami} from './commands/whoami'; import {logHelpClear} from './help/clear.help'; @@ -27,7 +26,6 @@ import {logHelpSnapshot} from './help/snapshot.help'; import {logHelpStart} from './help/start.help'; import {logHelpStop} from './help/stop.help'; import {logHelpUpgrade} from './help/upgrade.help'; -import {logHelpUse} from './help/use.help'; import {logHelpVersion} from './help/version.help'; import {logHelpWhoAmI} from './help/whoami.help'; import {checkNodeVersion} from './utils/env.utils'; @@ -69,9 +67,6 @@ export const run = async () => { case 'open': logHelpOpen(args); break; - case 'use': - logHelpUse(args); - break; case 'clear': logHelpClear(args); break; @@ -150,9 +145,6 @@ export const run = async () => { case 'whoami': await whoami(); break; - case 'use': - await use(args); - break; case 'stop': await startStop({args, action: 'stop'}); break; diff --git a/src/services/controllers.services.ts b/src/services/controllers.services.ts index 3aa06c9f..9a8ef924 100644 --- a/src/services/controllers.services.ts +++ b/src/services/controllers.services.ts @@ -2,7 +2,7 @@ import {type Principal} from '@dfinity/principal'; import {assertAnswerCtrlC} from '@junobuild/cli-tools'; import {bold, green, red} from 'kleur'; import prompts from 'prompts'; -import {addCliMissionControl, addCliOrbiter, addCliSatellite, getUse} from '../configs/cli.config'; +import {addCliMissionControl, addCliOrbiter, addCliSatellite} from '../configs/cli.config'; import {ENV} from '../env'; import {type AssetKey} from '../types/asset-key'; import {displaySegment} from '../utils/display.utils'; @@ -31,17 +31,15 @@ export const reuseController = async (controller: Principal) => { return; } - const profile = await getUse(); - switch (segment) { case 'orbiter': - await saveOrbiter({profile, segmentId}); + await saveOrbiter({segmentId}); break; case 'mission_control': - await saveMissionControl({profile, segmentId}); + await saveMissionControl({segmentId}); break; default: - await saveSatellite({profile, segmentId}); + await saveSatellite({segmentId}); } console.log( @@ -49,13 +47,7 @@ export const reuseController = async (controller: Principal) => { ); }; -const saveSatellite = async ({ - profile, - segmentId -}: { - profile: string | undefined; - segmentId: string; -}) => { +const saveSatellite = async ({segmentId}: {segmentId: string}) => { const {name}: {name: string | undefined} = await prompts({ type: 'text', name: 'name', @@ -68,7 +60,6 @@ const saveSatellite = async ({ } await addCliSatellite({ - profile, satellite: { p: segmentId, n: name @@ -76,25 +67,12 @@ const saveSatellite = async ({ }); }; -const saveMissionControl = async ({ - profile, - segmentId -}: { - profile: string | undefined; - segmentId: string; -}) => { - await addCliMissionControl({profile, missionControl: segmentId}); +const saveMissionControl = async ({segmentId}: {segmentId: string}) => { + await addCliMissionControl({missionControl: segmentId}); }; -const saveOrbiter = async ({ - profile, - segmentId -}: { - profile: string | undefined; - segmentId: string; -}) => { +const saveOrbiter = async ({segmentId}: {segmentId: string}) => { await addCliOrbiter({ - profile, orbiter: { p: segmentId } diff --git a/src/types/cli.config.ts b/src/types/cli.config.ts index fead8000..0e87b5ee 100644 --- a/src/types/cli.config.ts +++ b/src/types/cli.config.ts @@ -1,6 +1,6 @@ import type {JsonnableEd25519KeyIdentity} from '@dfinity/identity/lib/cjs/identity/ed25519'; -export interface CliConfigData { +export interface CliConfig { token: JsonnableEd25519KeyIdentity; satellites: CliSatelliteConfig[]; missionControl?: string; @@ -16,12 +16,3 @@ export interface CliOrbiterConfig { p: string; // principal n?: string; // name } - -// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents -export type CliProfile = 'default' | string; - -// Backwards compatibility. Default is save in root of the object, profile in an optional record. -export interface CliConfig extends CliConfigData { - use?: CliProfile; - profiles?: Record; -} From 3020397bb5aec1b15a69e49d1ad9e4e9e53f1194 Mon Sep 17 00:00:00 2001 From: David Dal Busco Date: Thu, 31 Jul 2025 16:21:06 +0200 Subject: [PATCH 2/3] feat: remove profile from login --- src/services/login.services.ts | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/services/login.services.ts b/src/services/login.services.ts index 8b9bf285..93bcb773 100644 --- a/src/services/login.services.ts +++ b/src/services/login.services.ts @@ -36,7 +36,6 @@ export const login = async (args?: string[]) => { const satellites = url.searchParams.get('satellites'); const orbiters = url.searchParams.get('orbiters'); const missionControl = url.searchParams.get('mission_control'); - const profile = url.searchParams.get('profile'); if (returnedNonce !== `${nonce}`) { await respondWithFile(req, res, 400, '../templates/login/failure.html'); @@ -46,7 +45,7 @@ export const login = async (args?: string[]) => { } try { - await saveConfig({token, satellites, orbiters, missionControl, profile}); + await saveConfig({token, satellites, orbiters, missionControl}); await respondWithFile(req, res, 200, '../templates/login/success.html'); console.log(`${green('Success!')} Logged in. ✅`); resolve(); @@ -98,20 +97,17 @@ const saveConfig = async ({ token, satellites, orbiters, - missionControl, - profile + missionControl }: { token: JsonnableEd25519KeyIdentity; satellites: string | null; orbiters: string | null; missionControl: string | null; - profile: string | null; }) => { await saveCliConfig({ token, satellites: JSON.parse(decodeURIComponent(satellites ?? '[]')), orbiters: orbiters !== null ? JSON.parse(decodeURIComponent(orbiters)) : null, - missionControl, - profile + missionControl }); }; From b9246c2afc7e42d530f8f61b5da5bda593a53bcd Mon Sep 17 00:00:00 2001 From: David Dal Busco Date: Thu, 31 Jul 2025 16:25:00 +0200 Subject: [PATCH 3/3] chore: merge main --- src/services/login.services.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/services/login.services.ts b/src/services/login.services.ts index efe6f12a..93bcb773 100644 --- a/src/services/login.services.ts +++ b/src/services/login.services.ts @@ -111,4 +111,3 @@ const saveConfig = async ({ missionControl }); }; -