diff --git a/cli/instance.ts b/cli/instance.ts index fb495c714f..f1a31f9702 100644 --- a/cli/instance.ts +++ b/cli/instance.ts @@ -261,7 +261,7 @@ export async function pickInstance( return instance; } -async function instancePull(opts: GlobalOptions & InstanceSyncOptions) { +async function instancePull(opts: InstanceSyncOptions) { const instance = await pickInstance(opts, true); log.info("Pulling instance-level changes"); log.info(`remote (${instance.name}) -> local`); @@ -393,7 +393,7 @@ async function instancePull(opts: GlobalOptions & InstanceSyncOptions) { } } -async function instancePush(opts: GlobalOptions & InstanceSyncOptions) { +async function instancePush(opts: InstanceSyncOptions) { let instances = await allInstances(); const instance = await pickInstance(opts, true); diff --git a/cli/settings.ts b/cli/settings.ts index d5feeca100..e938cd364e 100644 --- a/cli/settings.ts +++ b/cli/settings.ts @@ -33,6 +33,22 @@ export interface SimplifiedSettings { name: string; } +const INSTANCE_SETTINGS_PATH = "instance_settings.yaml"; +let instanceSettingsPath = INSTANCE_SETTINGS_PATH; +async function checkInstanceSettingsPath(opts: InstanceSyncOptions) { + if (opts.prefix && opts.folderPerInstance && opts.prefixSettings) { + instanceSettingsPath = `${opts.prefix}/${INSTANCE_SETTINGS_PATH}`; + } +} + +const INSTANCE_CONFIGS_PATH = "instance_configs.yaml"; +let instanceConfigsPath = INSTANCE_CONFIGS_PATH; +async function checkInstanceConfigPath(opts: InstanceSyncOptions) { + if (opts.prefix && opts.folderPerInstance && opts.prefixSettings) { + instanceConfigsPath = `${opts.prefix}/${INSTANCE_CONFIGS_PATH}`; + } +} + export async function pushWorkspaceSettings( workspace: string, _path: string, @@ -260,22 +276,15 @@ export async function pushWorkspaceKey( } } -const INSTANCE_SETTINGS_PATH = "instance_settings.yaml"; - -export async function readInstanceSettings( - opts: GlobalOptions & InstanceSyncOptions -) { +export async function readInstanceSettings(opts: InstanceSyncOptions) { let localSettings: GlobalSetting[] = []; - let path = INSTANCE_SETTINGS_PATH; - if (opts.prefix && opts.folderPerInstance && opts.prefixSettings) { - path = `${opts.prefix}/${INSTANCE_SETTINGS_PATH}`; - } + await checkInstanceSettingsPath(opts); try { - localSettings = (await yamlParseFile(path)) as GlobalSetting[]; + localSettings = (await yamlParseFile(instanceSettingsPath)) as GlobalSetting[]; } catch { - log.warn(`No ${path} found`); + log.warn(`No ${instanceSettingsPath} found`); } return localSettings; } @@ -337,11 +346,13 @@ async function processField( } export async function pullInstanceSettings( - opts: GlobalOptions & InstanceSyncOptions, + opts: InstanceSyncOptions, preview = false ) { const remoteSettings = await wmill.listGlobalSettings(); + await checkInstanceSettingsPath(opts); + if (preview) { const localSettings: GlobalSetting[] = await readInstanceSettings(opts); const processedSettings = await processInstanceSettings( @@ -362,16 +373,16 @@ export async function pullInstanceSettings( "encode" ); await Deno.writeTextFile( - INSTANCE_SETTINGS_PATH, + instanceSettingsPath, yamlStringify(processedSettings) ); - log.info(colors.green(`Settings written to ${INSTANCE_SETTINGS_PATH}`)); + log.info(colors.green(`Settings written to ${instanceSettingsPath}`)); } } export async function pushInstanceSettings( - opts: GlobalOptions & InstanceSyncOptions, + opts: InstanceSyncOptions, preview: boolean = false ) { const remoteSettings = await wmill.listGlobalSettings(); @@ -434,28 +445,23 @@ export async function pushInstanceSettings( } } -const INSTANCE_CONFIGS_PATH = "instance_configs.yaml"; - export async function readLocalConfigs( - opts: GlobalOptions & InstanceSyncOptions + opts: InstanceSyncOptions ) { let localConfigs: Config[] = []; - let path = INSTANCE_CONFIGS_PATH; - if (opts.prefix && opts.folderPerInstance && opts.prefixSettings) { - path = `${opts.prefix}/${INSTANCE_CONFIGS_PATH}`; - } + await checkInstanceConfigPath(opts); try { - localConfigs = (await yamlParseFile(path)) as Config[]; + localConfigs = (await yamlParseFile(instanceConfigsPath)) as Config[]; } catch { - log.warn(`No ${path} found`); + log.warn(`No ${instanceConfigsPath} found`); } return localConfigs; } export async function pullInstanceConfigs( - opts: GlobalOptions & InstanceSyncOptions, + opts: InstanceSyncOptions, preview = false ) { const remoteConfigs = (await wmill.listConfigs()).map((x) => { @@ -478,16 +484,16 @@ export async function pullInstanceConfigs( log.info("Pulling configs from instance"); await Deno.writeTextFile( - INSTANCE_CONFIGS_PATH, + instanceConfigsPath, yamlStringify(remoteConfigs as any) ); - log.info(colors.green(`Configs written to ${INSTANCE_CONFIGS_PATH}`)); + log.info(colors.green(`Configs written to ${instanceConfigsPath}`)); } } export async function pushInstanceConfigs( - opts: GlobalOptions & InstanceSyncOptions, + opts: InstanceSyncOptions, preview: boolean = false ) { const remoteConfigs = (await wmill.listConfigs()).map((x) => { diff --git a/cli/user.ts b/cli/user.ts index c955113600..6f37dd76a5 100644 --- a/cli/user.ts +++ b/cli/user.ts @@ -23,6 +23,22 @@ import { InstanceGroup, } from "./gen/types.gen.ts"; +const INSTANCE_USERS_PATH = "instance_users.yaml"; +let instanceUsersPath = INSTANCE_USERS_PATH; +async function checkInstanceUsersPath(opts: InstanceSyncOptions) { + if (opts.prefix && opts.folderPerInstance && opts.prefixSettings) { + instanceUsersPath = `${opts.prefix}/${INSTANCE_USERS_PATH}`; + } +} + +const INSTANCE_GROUPS_PATH = "instance_groups.yaml"; +let instanceGroupsPath = INSTANCE_GROUPS_PATH; +async function checkInstanceGroupsPath(opts: InstanceSyncOptions) { + if (opts.prefix && opts.folderPerInstance && opts.prefixSettings) { + instanceGroupsPath = `${opts.prefix}/${INSTANCE_GROUPS_PATH}`; + } +} + async function list(opts: GlobalOptions) { await requireLogin(opts); @@ -386,67 +402,59 @@ export async function pushGroup( } } -const INSTANCE_USERS_PATH = "instance_users.yaml"; - export async function pullInstanceUsers( - opts: GlobalOptions & InstanceSyncOptions, + opts: InstanceSyncOptions, preview: boolean = false ) { const remoteUsers = await wmill.globalUsersExport(); + await checkInstanceUsersPath(opts); + if (preview) { const localUsers: ExportedUser[] = await readInstanceUsers(opts); return compareInstanceObjects(remoteUsers, localUsers, "email", "user"); } else { log.info("Pulling users from instance..."); await Deno.writeTextFile( - INSTANCE_USERS_PATH, + instanceUsersPath, yamlStringify(remoteUsers as any) ); - log.info(colors.green(`Users written to ${INSTANCE_USERS_PATH}`)); + log.info(colors.green(`Users written to ${instanceUsersPath}`)); } } export async function readInstanceUsers( - opts: GlobalOptions & InstanceSyncOptions + opts: InstanceSyncOptions ) { let localUsers: ExportedUser[] = []; - let path = INSTANCE_USERS_PATH; - if (opts.prefix && opts.folderPerInstance && opts.prefixSettings) { - path = `${opts.prefix}/${INSTANCE_USERS_PATH}`; - } + await checkInstanceUsersPath(opts); try { - localUsers = (await yamlParseFile(path)) as ExportedUser[]; + localUsers = (await yamlParseFile(instanceUsersPath)) as ExportedUser[]; } catch { - log.warn(`No ${path} file found`); + log.warn(`No ${instanceUsersPath} file found`); } return localUsers; } -const INSTANCE_GROUPS_PATH = "instance_groups.yaml"; - export async function readInstanceGroups( - opts: GlobalOptions & InstanceSyncOptions + opts: InstanceSyncOptions ) { let localGroups: InstanceGroup[] = []; - let path = INSTANCE_GROUPS_PATH; - if (opts.prefix && opts.folderPerInstance && opts.prefixSettings) { - path = `${opts.prefix}/${INSTANCE_GROUPS_PATH}`; - } + await checkInstanceGroupsPath(opts); try { - localGroups = (await yamlParseFile(path)) as ExportedInstanceGroup[]; + localGroups = (await yamlParseFile(instanceGroupsPath)) as ExportedInstanceGroup[]; } catch { - log.warn(`No ${path} file found`); + log.warn(`No ${instanceGroupsPath} file found`); } return localGroups; } export async function pushInstanceUsers( - opts: GlobalOptions & InstanceSyncOptions, + opts: InstanceSyncOptions, preview: boolean = false ) { const remoteUsers = await wmill.globalUsersExport(); @@ -465,11 +473,13 @@ export async function pushInstanceUsers( } export async function pullInstanceGroups( - opts: GlobalOptions & InstanceSyncOptions, + opts: InstanceSyncOptions, preview = false ) { const remoteGroups = await wmill.exportInstanceGroups(); + await checkInstanceGroupsPath(opts); + if (preview) { const localGroups = await readInstanceGroups(opts); return compareInstanceObjects(remoteGroups, localGroups, "name", "group"); @@ -477,16 +487,16 @@ export async function pullInstanceGroups( log.info("Pulling groups from instance..."); await Deno.writeTextFile( - INSTANCE_GROUPS_PATH, + instanceGroupsPath, yamlStringify(remoteGroups as any) ); - log.info(colors.green(`Groups written to ${INSTANCE_GROUPS_PATH}`)); + log.info(colors.green(`Groups written to ${instanceGroupsPath}`)); } } export async function pushInstanceGroups( - opts: GlobalOptions & InstanceSyncOptions, + opts: InstanceSyncOptions, preview: boolean = false ) { const remoteGroups = await wmill.exportInstanceGroups(); diff --git a/cli/worker_groups.ts b/cli/worker_groups.ts index bd1eb6f56f..17ff13ec40 100644 --- a/cli/worker_groups.ts +++ b/cli/worker_groups.ts @@ -56,7 +56,7 @@ export async function displayWorkerGroups(opts: void) { async function pullWorkerGroups(opts: InstanceSyncOptions) { await pickInstance(opts, true); - const totalChanges = await pullInstanceConfigs(true) ?? 0; + const totalChanges = await pullInstanceConfigs(opts, true) ?? 0; if (totalChanges === 0) { log.info("No changes to apply"); @@ -72,14 +72,14 @@ async function pullWorkerGroups(opts: InstanceSyncOptions) { } if (confirm) { - await pullInstanceConfigs(false); + await pullInstanceConfigs(opts, false); } } async function pushWorkerGroups(opts: InstanceSyncOptions) { await pickInstance(opts, true); - const totalChanges = await pushInstanceConfigs(true) ?? 0; + const totalChanges = await pushInstanceConfigs(opts, true) ?? 0; if (totalChanges === 0) { log.info("No changes to apply"); @@ -95,7 +95,7 @@ async function pushWorkerGroups(opts: InstanceSyncOptions) { } if (confirm) { - await pushInstanceConfigs(false); + await pushInstanceConfigs(opts, false); } }