Skip to content

Commit

Permalink
Merge pull request #16 from tago-io/feat/tagoio-deploy-url
Browse files Browse the repository at this point in the history
feat/tagoio-deploy-url
  • Loading branch information
vitorfdl authored Sep 9, 2024
2 parents cee6ad8 + 57cb130 commit 11ec933
Show file tree
Hide file tree
Showing 23 changed files with 91 additions and 31 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions docs/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/commands/analysis/analysis-console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/analysis/analysis-set-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/commands/analysis/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/analysis/run-analysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)}].`);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/analysis/trigger-analysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/commands/dashboard/copy-tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/commands/devices/change-bucket-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/commands/devices/copy-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ 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: ");
}

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);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/devices/data-get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/commands/devices/data-post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/commands/devices/device-bkp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,15 @@ 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);
}

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);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/devices/device-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/commands/devices/device-live-inspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 33 additions & 2 deletions src/commands/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)}.`);
Expand All @@ -15,6 +40,8 @@ interface LoginOptions {
email?: string;
password?: string;
token?: string;
tagoDeployUrl?: string;
tagoDeploySse?: string;
}

/**
Expand All @@ -26,7 +53,7 @@ interface LoginOptions {
* @param {string} loginOptions.password - The user's password.
* @returns {Promise<Object>} - The login result.
*/
async function handleOTPLogin({ otp_autosend }: { otp_autosend: OTPType }, { email, password }: Required<LoginOptions>) {
async function handleOTPLogin({ otp_autosend }: { otp_autosend: OTPType }, { email, password }: Required<Pick<LoginOptions, "email" | "password">>) {
if (otp_autosend !== "authenticator") {
await Account.requestLoginPINCode({ email, password }, otp_autosend).catch(errorHandler);
}
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/profile/export/export-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);
Expand Down
8 changes: 4 additions & 4 deletions src/commands/profile/export/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down Expand Up @@ -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 = {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/profile/export/services/analysis-export.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
2 changes: 1 addition & 1 deletion src/commands/profile/export/services/devices-export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
9 changes: 6 additions & 3 deletions src/commands/start-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}

/**
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/lib/config-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ interface IConfigFileEnvs {
}
interface IConfigFile {
profileToken?: string;
tagoDeployUrl?: string;
tagoDeploySse?: string;
analysisPath: string;
buildPath: string;
default: string;
Expand Down

0 comments on commit 11ec933

Please sign in to comment.