diff --git a/README.md b/README.md index 87095f9..e89d61d 100644 --- a/README.md +++ b/README.md @@ -152,6 +152,10 @@ To create a `tagoconfig.json` file, initiate your project using the `tagoio init tagoio init ``` +If you are using the TagoDeploy service, you can configure the URLs for both the API and SSE: + +- To set the URL for the API in the TagoDeploy service, update the `tagoDeployUrl` field in the `tagoconfig.json` file with the URL of your server. +- To set the URL for SSE in the TagoDeploy service, update the `tagoDeploySse` field in the `tagoconfig.json` file with the URL of your server. ### File Contents diff --git a/docs/schema.json b/docs/schema.json index ba58878..86d801e 100644 --- a/docs/schema.json +++ b/docs/schema.json @@ -50,6 +50,16 @@ "type": "string", "format": "uri-reference" }, + "tagoDeployUrl": { + "description": "URL to API for TagoDeploy server", + "type": "string", + "format": "uri-reference" + }, + "tagoDeploySse": { + "description": "URL to SSE for TagoDeploy server", + "type": "string", + "format": "uri-reference" + }, "buildPath": { "description": "Path from the root of the project to the build folder", "type": "string", diff --git a/src/commands/analysis/analysis-console.ts b/src/commands/analysis/analysis-console.ts index 3730581..0026778 100644 --- a/src/commands/analysis/analysis-console.ts +++ b/src/commands/analysis/analysis-console.ts @@ -92,7 +92,7 @@ async function connectAnalysisConsole(scriptName: string | void, options: { envi return; } - const account = new Account({ token: config.profileToken, region: "usa-1" }); + const account = new Account({ token: config.profileToken, region: !process.env.TAGOIO_API ? "usa-1" : "env" }); const analysis_info = await account.analysis.info(scriptObj.id); if (!analysis_info) { errorHandler(`Analysis with ID: ${scriptObj.id} couldn't be found.`); diff --git a/src/commands/analysis/analysis-set-mode.ts b/src/commands/analysis/analysis-set-mode.ts index ca60afa..2334f55 100644 --- a/src/commands/analysis/analysis-set-mode.ts +++ b/src/commands/analysis/analysis-set-mode.ts @@ -67,7 +67,7 @@ async function analysisSetMode(userInputName: string | void, options: { environm return; } - const account = new Account({ token: config.profileToken, region: "usa-1" }); + const account = new Account({ token: config.profileToken, region: !process.env.TAGOIO_API ? "usa-1" : "env" }); const analysisFilterName = userInputName ? `*${userInputName}*` : undefined; // Get analysis list from TagoIO diff --git a/src/commands/analysis/deploy.ts b/src/commands/analysis/deploy.ts index 28e8ba3..713de7e 100644 --- a/src/commands/analysis/deploy.ts +++ b/src/commands/analysis/deploy.ts @@ -125,7 +125,7 @@ async function deployAnalysis(cmdScriptName: string, options: { environment: str return; } - const account = new Account({ token: config.profileToken, region: "usa-1" }); + const account = new Account({ token: config.profileToken, region: !process.env.TAGOIO_API ? "usa-1" : "env" }); for (const { id, fileName } of scriptList) { await buildScript(account, fileName, id, config); } diff --git a/src/commands/analysis/run-analysis.ts b/src/commands/analysis/run-analysis.ts index 30e26e8..e0034c0 100644 --- a/src/commands/analysis/run-analysis.ts +++ b/src/commands/analysis/run-analysis.ts @@ -74,7 +74,7 @@ async function runAnalysis(scriptName: string | undefined, options: { environmen return process.exit(); } - const account = new Account({ token: config.profileToken, region: "usa-1" }); + const account = new Account({ token: config.profileToken, region: !process.env.TAGOIO_API ? "usa-1" : "env" }); let { token: analysisToken, run_on, name } = await account.analysis.info(scriptToRun.id); successMSG(`> Analysis found: ${highlightMSG(scriptToRun.fileName)} (${name}}) [${highlightMSG(analysisToken)}].`); diff --git a/src/commands/analysis/trigger-analysis.ts b/src/commands/analysis/trigger-analysis.ts index 4754456..825300f 100644 --- a/src/commands/analysis/trigger-analysis.ts +++ b/src/commands/analysis/trigger-analysis.ts @@ -24,7 +24,7 @@ async function triggerAnalysis(scriptName: string | void, options: { environment return; } - const account = new Account({ token: config.profileToken, region: "usa-1" }); + const account = new Account({ token: config.profileToken, region: !process.env.TAGOIO_API ? "usa-1" : "env" }); const analysisList = config.analysisList.filter((x) => x.fileName); let script: IEnvironment["analysisList"][0] | undefined; diff --git a/src/commands/dashboard/copy-tab.ts b/src/commands/dashboard/copy-tab.ts index d30b000..3b87d37 100644 --- a/src/commands/dashboard/copy-tab.ts +++ b/src/commands/dashboard/copy-tab.ts @@ -110,7 +110,7 @@ async function copyTabWidgets(dashID: string, options: IOptions) { return; } - const account = new Account({ token: config.profileToken, region: "usa-1" }); + const account = new Account({ token: config.profileToken, region: !process.env.TAGOIO_API ? "usa-1" : "env" }); if (!dashID) { dashID = await pickDashboardIDFromTagoIO(account); } diff --git a/src/commands/devices/change-bucket-type.ts b/src/commands/devices/change-bucket-type.ts index d21585e..6ee5b12 100644 --- a/src/commands/devices/change-bucket-type.ts +++ b/src/commands/devices/change-bucket-type.ts @@ -16,7 +16,7 @@ interface BucketSettings { const coloredBucketType = (type: string) => (type === "mutable" ? kleur.green(type) : type === "legacy" ? kleur.red(type) : kleur.blue(type)); async function convertDevice(deviceID: string, settings: BucketSettings, profileToken: string) { - const account = new Account({ token: profileToken, region: "usa-1" }); + const account = new Account({ token: profileToken, region: !process.env.TAGOIO_API ? "usa-1" : "env" }); const deviceInfo = await account.devices.info(deviceID); const bucketType = deviceInfo.type; @@ -81,7 +81,7 @@ async function changeBucketType(id: string, options: { environment: string }) { errorHandler("Environment not found"); return; } - const account = new Account({ token: config.profileToken, region: "usa-1" }); + const account = new Account({ token: config.profileToken, region: !process.env.TAGOIO_API ? "usa-1" : "env" }); const bucketList = id ? [id] : await chooseBucketsFromList(account); if (id) { const bucketInfo = await account.buckets.info(id); diff --git a/src/commands/devices/copy-data.ts b/src/commands/devices/copy-data.ts index 2a01411..1228922 100644 --- a/src/commands/devices/copy-data.ts +++ b/src/commands/devices/copy-data.ts @@ -38,7 +38,7 @@ async function copyDeviceData(options: IOptions) { } if (!options.from || !options.to) { - const account = new Account({ token: config.profileToken, region: "usa-1" }); + const account = new Account({ token: config.profileToken, region: !process.env.TAGOIO_API ? "usa-1" : "env" }); options.from = await pickDeviceIDFromTagoIO(account, "Choose a device to copy the data from:"); options.to = await pickDeviceIDFromTagoIO(account, "Choose a device to copy the data to: "); } @@ -46,7 +46,7 @@ async function copyDeviceData(options: IOptions) { let deviceFrom: Device | undefined; let deviceTo: Device | undefined; if (options.from?.length === 24 || options.to?.length === 24) { - const account = new Account({ token: config.profileToken, region: "usa-1" }); + const account = new Account({ token: config.profileToken, region: !process.env.TAGOIO_API ? "usa-1" : "env" }); if (options.from.length === 24) { deviceFrom = await Utils.getDevice(account, options.from); diff --git a/src/commands/devices/data-get.ts b/src/commands/devices/data-get.ts index d06e7ac..1f999e0 100644 --- a/src/commands/devices/data-get.ts +++ b/src/commands/devices/data-get.ts @@ -89,7 +89,7 @@ async function getDeviceData(idOrToken: string, options: IOptions) { errorHandler("Environment not found"); return; } - const account = new Account({ token: config.profileToken, region: "usa-1" }); + const account = new Account({ token: config.profileToken, region: !process.env.TAGOIO_API ? "usa-1" : "env" }); if (!idOrToken) { idOrToken = await pickDeviceIDFromTagoIO(account); } diff --git a/src/commands/devices/data-post.ts b/src/commands/devices/data-post.ts index 0b92c6a..4ba6cd8 100644 --- a/src/commands/devices/data-post.ts +++ b/src/commands/devices/data-post.ts @@ -15,14 +15,14 @@ async function postDeviceData(idOrToken: string, options: IOptions) { return; } - const account = new Account({ token: config.profileToken, region: "usa-1" }); + const account = new Account({ token: config.profileToken, region: !process.env.TAGOIO_API ? "usa-1" : "env" }); if (!idOrToken) { idOrToken = await pickDeviceIDFromTagoIO(account); } const deviceInfo = await account.devices .info(idOrToken) .catch(() => { - const device = new Device({ token: idOrToken, region: "usa-1" }); + const device = new Device({ token: idOrToken, region: !process.env.TAGOIO_API ? "usa-1" : "env" }); return device.info(); }) .catch(errorHandler); diff --git a/src/commands/devices/device-bkp.ts b/src/commands/devices/device-bkp.ts index 3ede893..3482bb2 100644 --- a/src/commands/devices/device-bkp.ts +++ b/src/commands/devices/device-bkp.ts @@ -111,7 +111,7 @@ async function bkpDeviceData(idOrToken: string, options: IOptions) { return; } - const account = new Account({ token: config.profileToken, region: "usa-1" }); + const account = new Account({ token: config.profileToken, region: !process.env.TAGOIO_API ? "usa-1" : "env" }); if (!idOrToken) { idOrToken = await pickDeviceIDFromTagoIO(account); } @@ -119,7 +119,7 @@ async function bkpDeviceData(idOrToken: string, options: IOptions) { const deviceInfo = await account.devices .info(idOrToken) .catch(() => { - const device = new Device({ token: idOrToken, region: "usa-1" }); + const device = new Device({ token: idOrToken, region: !process.env.TAGOIO_API ? "usa-1" : "env" }); return device.info(); }) .catch(errorHandler); diff --git a/src/commands/devices/device-info.ts b/src/commands/devices/device-info.ts index de6b48e..f38de0e 100644 --- a/src/commands/devices/device-info.ts +++ b/src/commands/devices/device-info.ts @@ -13,7 +13,7 @@ async function deviceInfo(idOrToken: string, options: { environment: string; raw return; } - const account = new Account({ token: config.profileToken, region: "usa-1" }); + const account = new Account({ token: config.profileToken, region: !process.env.TAGOIO_API ? "usa-1" : "env" }); if (!idOrToken) { idOrToken = await pickDeviceIDFromTagoIO(account); } diff --git a/src/commands/devices/device-live-inspector.ts b/src/commands/devices/device-live-inspector.ts index af2e346..09bb332 100644 --- a/src/commands/devices/device-live-inspector.ts +++ b/src/commands/devices/device-live-inspector.ts @@ -104,14 +104,14 @@ async function inspectorConnection(deviceIdOrToken: string, options: IOptions) { return; } - const account = new Account({ token: config.profileToken, region: "usa-1" }); + const account = new Account({ token: config.profileToken, region: !process.env.TAGOIO_API ? "usa-1" : "env" }); if (!deviceIdOrToken) { deviceIdOrToken = await pickDeviceIDFromTagoIO(account); } let deviceInfo = await account.devices.info(deviceIdOrToken).catch(() => null); if (!deviceInfo) { - const device = new Device({ token: deviceIdOrToken, region: "usa-1" }); + const device = new Device({ token: deviceIdOrToken, region: !process.env.TAGOIO_API ? "usa-1" : "env" }); deviceInfo = await device .info() .then((r) => r as DeviceInfo) diff --git a/src/commands/login.ts b/src/commands/login.ts index 0f41136..6ce02cf 100644 --- a/src/commands/login.ts +++ b/src/commands/login.ts @@ -6,6 +6,31 @@ import { OTPType } from "@tago-io/sdk/lib/types"; import { errorHandler, highlightMSG, successMSG } from "../lib/messages"; import { writeToken } from "../lib/token"; +/** + * @description Set the TagoIO deploy URL. + */ +async function setTagoDeployUrl(): Promise<{ urlAPI: string; urlSSE: string } | undefined> { + const { tagoDeployUrl } = await prompts({ + message: "Do you want to pass on the tagoDeploy URL?", + type: "confirm", + name: "tagoDeployUrl", + }); + if (!tagoDeployUrl) { + return; + } + + const { urlAPI } = await prompts({ type: "text", name: "urlAPI", message: "Set the URL for the API service: " }); + if (!urlAPI) { + return; + } + + const urlSSE = urlAPI.replace("https://api.", "https://sse.").replace(".tago-io.net", ".tago-io.net/events"); + process.env.TAGOIO_API = urlAPI; + process.env.TAGOIO_SSE = urlSSE; + + return { urlAPI, urlSSE }; +} + function writeCustomToken(environment: string, token: string) { writeToken(token, environment); successMSG(`Token successfully written to the environment ${highlightMSG(environment)}.`); @@ -15,6 +40,8 @@ interface LoginOptions { email?: string; password?: string; token?: string; + tagoDeployUrl?: string; + tagoDeploySse?: string; } /** @@ -26,7 +53,7 @@ interface LoginOptions { * @param {string} loginOptions.password - The user's password. * @returns {Promise} - The login result. */ -async function handleOTPLogin({ otp_autosend }: { otp_autosend: OTPType }, { email, password }: Required) { +async function handleOTPLogin({ otp_autosend }: { otp_autosend: OTPType }, { email, password }: Required>) { if (otp_autosend !== "authenticator") { await Account.requestLoginPINCode({ email, password }, otp_autosend).catch(errorHandler); } @@ -56,7 +83,7 @@ async function loginWithEmailPassword(email: string, password: string) { try { const errorJSON = JSON.parse(error); if (errorJSON?.otp_enabled) { - return handleOTPLogin(errorJSON, { email, password, token: "" }); + return handleOTPLogin(errorJSON, { email, password }); } } catch { // Ignore JSON parsing errors @@ -67,6 +94,10 @@ async function loginWithEmailPassword(email: string, password: string) { } async function tagoLogin(environment: string, options: LoginOptions) { + const tagoDeploy = await setTagoDeployUrl(); + options.tagoDeployUrl = tagoDeploy?.urlAPI; + options.tagoDeploySse = tagoDeploy?.urlSSE; + if (options.token) { return writeCustomToken(environment, options.token); } diff --git a/src/commands/profile/export/export-setup.ts b/src/commands/profile/export/export-setup.ts index 88efae7..04a19d4 100644 --- a/src/commands/profile/export/export-setup.ts +++ b/src/commands/profile/export/export-setup.ts @@ -124,7 +124,7 @@ async function setupExport(options: { setup: string }) { return; } - const account = new Account({ token: config.profileToken, region: "usa-1" }); + const account = new Account({ token: config.profileToken, region: !process.env.TAGOIO_API ? "usa-1" : "env" }); const exportTag = await enterExportTag("export_id"); const entities = await chooseEntities([], ["dictionaries", "run"]); diff --git a/src/commands/profile/export/export.ts b/src/commands/profile/export/export.ts index 1b6dcd1..34fe93b 100644 --- a/src/commands/profile/export/export.ts +++ b/src/commands/profile/export/export.ts @@ -90,8 +90,8 @@ async function enterExportTag(defaultTag: string) { } async function confirmEnvironments(userConfig: IExport) { - const exportAcc = new Account({ token: userConfig.export.token, region: "usa-1" }); - const importAcc = new Account({ token: userConfig.import.token, region: "usa-1" }); + const exportAcc = new Account({ token: userConfig.export.token, region: !process.env.TAGOIO_API ? "usa-1" : "env" }); + const importAcc = new Account({ token: userConfig.import.token, region: !process.env.TAGOIO_API ? "usa-1" : "env" }); const errorWhenReading = (error: any, type: string) => { errorHandler(`${type} profile: ${error}`); @@ -163,8 +163,8 @@ async function startExport(options: IExportOptions) { return; } - const account = new Account({ token: userConfig.export.token, region: "usa-1" }); - const import_account = new Account({ token: userConfig.import.token, region: "usa-1" }); + const account = new Account({ token: userConfig.export.token, region: !process.env.TAGOIO_API ? "usa-1" : "env" }); + const import_account = new Account({ token: userConfig.import.token, region: !process.env.TAGOIO_API ? "usa-1" : "env" }); const import_rule = ENTITY_ORDER.filter((entity) => userConfig.entities.includes(entity)); let export_holder: IExportHolder = { diff --git a/src/commands/profile/export/services/analysis-export.ts b/src/commands/profile/export/services/analysis-export.ts index 985b069..b7eafff 100644 --- a/src/commands/profile/export/services/analysis-export.ts +++ b/src/commands/profile/export/services/analysis-export.ts @@ -1,9 +1,9 @@ +import zlib from "zlib"; import axios from "axios"; import prompts from "prompts"; -import zlib from "zlib"; import { Account } from "@tago-io/sdk"; -import { AnalysisInfo } from "@tago-io/sdk/out/modules/Account/analysis.types"; +import { AnalysisInfo } from "@tago-io/sdk/lib/types"; import { infoMSG } from "../../../../lib/messages"; import { replaceObj } from "../../../../lib/replace-obj"; diff --git a/src/commands/profile/export/services/devices-export.ts b/src/commands/profile/export/services/devices-export.ts index 951647e..4fbff37 100644 --- a/src/commands/profile/export/services/devices-export.ts +++ b/src/commands/profile/export/services/devices-export.ts @@ -34,7 +34,7 @@ async function deviceExport(account: Account, import_account: Account, export_ho ({ device_id: target_id, token: new_token } = await import_account.devices.create(new_device)); if (config.data && config.data.length > 0) { - const device = new Device({ token: new_token, region: "usa-1" }); + const device = new Device({ token: new_token, region: !process.env.TAGOIO_API ? "usa-1" : "env" }); const old_device = new Device({ token }); const data = await old_device.getData({ diff --git a/src/commands/start-config.ts b/src/commands/start-config.ts index bda3fc6..4dba55a 100644 --- a/src/commands/start-config.ts +++ b/src/commands/start-config.ts @@ -34,10 +34,10 @@ async function createEnvironmentToken(environment: string) { } infoMSG(`You can create a token by running: ${highlightMSG("tagoio login")}`); - const options = { token: undefined }; + const options = { token: undefined, tagoDeployUrl: undefined, tagoDeploySse: undefined }; await tagoLogin(environment, options); - return options.token; + return { profileToken: options.token, tagoDeployUrl: options?.tagoDeployUrl, tagoDeploySse: options?.tagoDeploySse }; } /** @@ -152,7 +152,10 @@ async function startConfig(environment: string, { token }: ConfigOptions) { if (!token) { token = readToken(environment); if (!token) { - token = await createEnvironmentToken(environment); + const data = await createEnvironmentToken(environment); + token = data?.profileToken; + configFile.tagoDeployUrl = data?.tagoDeployUrl || ""; + configFile.tagoDeploySse = data?.tagoDeploySse || ""; } } else { writeToken(token, environment); diff --git a/src/index.ts b/src/index.ts index 756aaf0..35387ff 100644 --- a/src/index.ts +++ b/src/index.ts @@ -33,6 +33,16 @@ const defaultEnvironment = process.env.TAGOIO_DEFAULT || ""; * @returns A Promise that resolves when all commands have been added. */ async function getAllCommands(program: Command) { + const configFile = getConfigFile(); + + if (configFile?.tagoDeployUrl) { + process.env.TAGOIO_API = configFile.tagoDeployUrl; + } + + if (configFile?.tagoDeploySse) { + process.env.TAGOIO_SSE = configFile.tagoDeploySse; + } + analysisCommands(program); deviceCommands(program); dashboardCommands(program); diff --git a/src/lib/config-file.ts b/src/lib/config-file.ts index ebd8bbc..55481c1 100644 --- a/src/lib/config-file.ts +++ b/src/lib/config-file.ts @@ -19,6 +19,8 @@ interface IConfigFileEnvs { } interface IConfigFile { profileToken?: string; + tagoDeployUrl?: string; + tagoDeploySse?: string; analysisPath: string; buildPath: string; default: string;