Skip to content

Commit

Permalink
Added homde tdxcli fodler and moved configs
Browse files Browse the repository at this point in the history
  • Loading branch information
mereacre committed Feb 11, 2020
1 parent 03b8706 commit bfaea32
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 64 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ The format is based on
and this project adheres to
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.2.8] - 2020-02-11

### Changed
- The .env and alias config files stored in home dir (```.tdxcli```).

### Fixed


## [0.2.7] - 2020-02-07

### Changed
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</a>
</p>

Nquiringminds TDX API client application
Command-line interface for accessing the TDX API

## Install
The best use of tdxcli tool is to install it with npm globally as follows:
Expand Down
3 changes: 3 additions & 0 deletions config.app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"scraperTimeout": 5000
}
22 changes: 22 additions & 0 deletions config.default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"nqminds": {
"tokenHref": "https://tbx.nqminds.com",
"config": {
"commandServer": "https://cmd.nqminds.com",
"ddpServer": "https://ddp.nqminds.com",
"queryServer": "https://q.nqminds.com",
"tdxServer": "https://tdx.nqminds.com",
"accessTokenTTL": 31622400
}
},
"nq_m": {
"tokenHref": "https://tbx.nq-m.com",
"config": {
"commandServer": "https://cmd.nq-m.com",
"ddpServer": "https://ddp.nq-m.com",
"queryServer": "https://q.nq-m.com",
"tdxServer": "https://tdx.nq-m.com",
"accessTokenTTL": 31622400
}
}
}
35 changes: 0 additions & 35 deletions config.json

This file was deleted.

29 changes: 18 additions & 11 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/* eslint-disable no-console */
"use strict";

const os = require("os");
const path = require("path");
const appConfig = require("./config.json");
const appConfig = require("./config.app.json");
const defaultConfig = require("./config.default.json");
const {TDX_CURRENT_ALIAS, TDX_CREDENTIALS} = require("./src/constants");
const {
checkValidAlias,
Expand All @@ -18,6 +20,7 @@ const {
readJsonFromFile,
numberToString,
createFile,
mkdir,
} = require("./src/utils");
const {
copyAliasConfig,
Expand All @@ -26,8 +29,10 @@ const {
} = require("./src/alias");
const CommandHandler = require("./src");

const envPath = path.join(__dirname, ".env");
const configPath = path.join(__dirname, "config.json");
const homePath = os.homedir();
const tdxcliConfigPath = path.join(homePath, ".tdxcli");
const envPath = path.join(tdxcliConfigPath, ".env");
const configPath = path.join(tdxcliConfigPath, "config.json");

require("dotenv").config({path: envPath});

Expand Down Expand Up @@ -56,12 +61,14 @@ async function run(commandName, commandProps) {
let alias = commandProps.alias;
let credentials = commandProps.credentials;
const {
id, secret, type, command, filepath,
id, secret, type, command, filepath,
aliasName, configJson, apiArgs, apiArgsStringify,
} = commandProps;

try {
await mkdir(tdxcliConfigPath);
await createFile(envPath);
await createFile(configPath, JSON.stringify(defaultConfig, null, 2));

if (alias === "") alias = envToAlias(process.env[TDX_CURRENT_ALIAS] || "");
if (credentials === "") credentials = process.env[TDX_CREDENTIALS] || "";
Expand All @@ -70,9 +77,9 @@ async function run(commandName, commandProps) {
throw Error("No alias or wrong alias name. Only allowed [a-zA-Z0-9_]");
}

const tdxConfigs = await readJsonFromFile(configPath);
const argumentSecret = {id, secret};
const tdxConfig = appConfig.tdxConfigs[alias] || {};
const configArgs = {tdxConfig, timeout: appConfig.scraperTimeout};
const configArgs = {tdxConfig: tdxConfigs[alias] || {}, timeout: appConfig.scraperTimeout};
let commandHandler;

if (commandName !== "signin" && credentials) {
Expand Down Expand Up @@ -109,13 +116,13 @@ async function run(commandName, commandProps) {
output = await commandHandler.getInfo({id, type});
break;
case "config":
output = tdxConfig;
output = tdxConfigs[alias] || {};
break;
case "list":
output = await commandHandler.getList({
type,
alias,
tdxConfigs: appConfig.tdxConfigs,
tdxConfigs,
env: process.env,
});
break;
Expand All @@ -130,17 +137,17 @@ async function run(commandName, commandProps) {
output = await commandHandler.upload(id, filepath);
break;
case "copyalias":
await copyAliasConfig({appConfig, alias, copyAliasName: aliasName, configPath});
await copyAliasConfig({tdxConfigs, alias, copyAliasName: aliasName, configPath});
output = "OK";
break;
case "modifyalias":
const aliasConfig = await readJsonFromFile(configJson);
await modifyAliasConfig({appConfig, aliasName, aliasConfig, configPath});
await modifyAliasConfig({tdxConfigs, aliasName, aliasConfig, configPath});
output = "OK";
break;
case "removealias":
if (alias === aliasName) throw Error("Can't remove the running alias.");
await removeAliasConfig({appConfig, aliasName, configPath});
await removeAliasConfig({tdxConfigs, aliasName, configPath});
output = "OK";
break;
case "databot":
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nqminds/nqm-tdx-terminal-cli",
"version": "0.2.7",
"version": "0.2.8",
"description": "Command-line interface for accessing the TDX API",
"main": "main.js",
"directories": {
Expand Down
22 changes: 11 additions & 11 deletions src/alias.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ function getAliasesArray(configs) {
return Object.keys(configs);
}

async function copyAliasConfig({appConfig, alias, copyAliasName, configPath}) {
const aliases = getAliasesArray(appConfig.tdxConfigs);
async function copyAliasConfig({tdxConfigs, alias, copyAliasName, configPath}) {
const aliases = getAliasesArray(tdxConfigs);

if (!checkValidAlias(copyAliasName)) {
throw Error("Invalid alias name.");
Expand All @@ -18,26 +18,26 @@ async function copyAliasConfig({appConfig, alias, copyAliasName, configPath}) {
throw Error("Alias already exists.");
} else {
return modifyAliasConfig({
appConfig,
tdxConfigs,
aliasName: copyAliasName,
aliasConfig: appConfig.tdxConfigs[alias],
aliasConfig: tdxConfigs[alias],
configPath,
});
}
}

async function modifyAliasConfig({appConfig, aliasName, aliasConfig, configPath}) {
appConfig.tdxConfigs[aliasName] = aliasConfig;
return writeJsonToFile(appConfig, configPath);
async function modifyAliasConfig({tdxConfigs, aliasName, aliasConfig, configPath}) {
tdxConfigs[aliasName] = aliasConfig;
return writeJsonToFile(tdxConfigs, configPath);
}

async function removeAliasConfig({appConfig, aliasName, configPath}) {
if (!(aliasName in appConfig.tdxConfigs)) {
async function removeAliasConfig({tdxConfigs, aliasName, configPath}) {
if (!(aliasName in tdxConfigs)) {
throw Error("Alias configuration doesn't exist.");
}

delete appConfig.tdxConfigs[aliasName];
return writeJsonToFile(appConfig, configPath);
delete tdxConfigs[aliasName];
return writeJsonToFile(tdxConfigs, configPath);
}

module.exports = {
Expand Down
16 changes: 14 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const util = require("util");
const {TDX_TOKEN, TDX_SECRET} = require("./constants");
const writeFile = util.promisify(fs.writeFile);
const readFile = util.promisify(fs.readFile);
const mkdirAsync = util.promisify(fs.mkdir);

function validateEmail(email) {
// eslint-disable-next-line max-len
Expand Down Expand Up @@ -140,11 +141,11 @@ function numberToString(value) {
else return value;
}

function createFile(filepath) {
function createFile(filepath, content = "") {
return new Promise((resolve, reject) => {
fs.open(filepath, "r", (err) => {
if (err) {
fs.writeFile(filepath, "", (err) => {
fs.writeFile(filepath, content, (err) => {
if (err) reject(err);
else resolve();
});
Expand All @@ -153,6 +154,16 @@ function createFile(filepath) {
});
}

async function mkdir(path) {
try {
await mkdirAsync(path);
} catch (error) {
if (error.code !== "EEXIST") {
throw error;
}
}
}

module.exports = {
base64ToJson,
jsonToBase64,
Expand All @@ -174,4 +185,5 @@ module.exports = {
readJsonFromFile,
numberToString,
createFile,
mkdir,
};
6 changes: 3 additions & 3 deletions tests/upload.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ test("pipeStream should return error on resourceStream error", async() => {

const response = upload.pipeStream(mockWriteable, mockReadable);
mockReadable.emit("error", error);
expect(response).rejects.toEqual(error);
await expect(response).rejects.toEqual(error);
});

test("pipeStream should return \"OK\" on end event", async() => {
Expand All @@ -47,7 +47,7 @@ test("pipeStream should return \"OK\" on end event", async() => {

const response = upload.pipeStream(mockWriteable, mockReadable);
mockReadable.emit("end");
expect(response).resolves.toEqual("OK");
await expect(response).resolves;
});

test("pipeStream should return error on erronous data", async() => {
Expand All @@ -61,5 +61,5 @@ test("pipeStream should return error on erronous data", async() => {
const response = upload.pipeStream(mockWriteable, mockReadable);
mockWriteable.emit("data", buf);
mockReadable.emit("end");
expect(response).rejects.toEqual(Error(data.message));
await expect(response).rejects.toEqual(Error(data.message));
});

0 comments on commit bfaea32

Please sign in to comment.