From 35c76d561c423cb68dda5b747878438c66e7f229 Mon Sep 17 00:00:00 2001 From: Edward Harman Date: Fri, 20 Aug 2021 12:57:18 +0000 Subject: [PATCH] make it possible to get config by passing environment variables --- package.json | 9 +++--- src/kumoConfig.ts | 76 +++++++++++++++++++++++++++-------------------- 2 files changed, 49 insertions(+), 36 deletions(-) diff --git a/package.json b/package.json index c7fdabc..a0de31a 100644 --- a/package.json +++ b/package.json @@ -4,12 +4,13 @@ "author": "Sushil Singh", "description": "Api Gateway and Controller for Mitsubishi Airconditioners", "dependencies": { - "express": "^4.15.4", - "source-map-support": "^0.4.16", "base-64": "^0.1", - "request": "", + "dotenv": "^10.0.0", + "express": "^4.15.4", "neodoc": "", - "sjcl": "^1.0" + "request": "", + "sjcl": "^1.0", + "source-map-support": "^0.4.16" }, "devDependencies": { "@types/node": "^8.0.19", diff --git a/src/kumoConfig.ts b/src/kumoConfig.ts index 40ade57..9541744 100644 --- a/src/kumoConfig.ts +++ b/src/kumoConfig.ts @@ -1,7 +1,8 @@ #!/usr/bin/env node +require('dotenv').config() require('source-map-support').install(); const request = require('request'); -const readline = require('readline'); +const readline = require('readline'); const Writable = require('stream').Writable; const fs = require('fs'); import { KumoCfg, KumoCfgElement } from './kumoCfgInterface'; @@ -9,7 +10,7 @@ const S = 0; const W = '44c73283b498d432ff25f5c8e06a016aef931e68f0a00ea710e36e6338fb22db'; var mutableStdout = new Writable({ - write: function(chunk: any, encoding:any, callback:()=>void) { + write: function (chunk: any, encoding: any, callback: () => void) { if (!this.muted) process.stdout.write(chunk, encoding); callback(); @@ -23,14 +24,14 @@ var rl = readline.createInterface({ terminal: true }); -function processcfg(acc:any, j:any): KumoCfg { +function processcfg(acc: any, j: any): KumoCfg { var mycfg: KumoCfg = {} mycfg[acc] = parsechildren(j); return mycfg; } -function parsechildren(j:any) { - let mycl: {[hash:string]:KumoCfgElement} = {} +function parsechildren(j: any) { + let mycl: { [hash: string]: KumoCfgElement } = {} for (let children in j) { let child = j[children] for (let zone in child['zoneTable']) { @@ -42,7 +43,7 @@ function parsechildren(j:any) { return mycl; } -function parsezone(z:any): KumoCfgElement { +function parsezone(z: any): KumoCfgElement { let myc: KumoCfgElement = { serial: z.serial, label: z.label, @@ -58,25 +59,25 @@ function parsezone(z:any): KumoCfgElement { function post(post_data: string) { request.post({ - headers: { - 'Accept': 'application/json, text/plain, */*', - 'Accept-Encoding': 'gzip, deflate, br', - 'accept-Language': 'en-US,en', - 'Content-Length': post_data.length, - 'Content-Type': 'application/json' - }, - url: 'https://geo-c.kumocloud.com/login', - body: post_data + headers: { + 'Accept': 'application/json, text/plain, */*', + 'Accept-Encoding': 'gzip, deflate, br', + 'accept-Language': 'en-US,en', + 'Content-Length': post_data.length, + 'Content-Type': 'application/json' }, + url: 'https://geo-c.kumocloud.com/login', + body: post_data + }, function (error: any, resp: any, body: any) { if (error != null) { console.error("Unable to get config"); console.error(error) } try { + console.log(`Downloaded the config file from cloud...\n${body}`); var dt = JSON.parse(body); - var cfg = processcfg(dt[0]['username'], dt[2]['children']) - console.log("Downloaded the config file from cloud...") + var cfg = processcfg(dt[0]['username'], dt[2]['children']); var out = fs.createWriteStream('kumo.cfg'); out.write('module.exports = \n') out.write(JSON.stringify(cfg)) @@ -91,22 +92,33 @@ function post(post_data: string) { ) } -rl.write('Please enter Kumo cloud credentials\n') -rl.question("Enter username:", function (user: string) { - console.log("Enter password:"); - mutableStdout.muted = true; - rl.question("Enter password:", function (pass: string) { - let msg = { - username: user, - password: pass, - appVersion: "2.2.0" - }; - var post_data = JSON.stringify(msg); - post(post_data) - rl.close() - }) -}) +function submit(user: string, pass: string) { + let msg = { + username: user, + password: pass, + appVersion: "2.2.0" + }; + var post_data = JSON.stringify(msg); + post(post_data); +} + +let userEnv = process.env.KUMO_USER; +let pwEnv = process.env.KUMO_PASS; + +if (userEnv && pwEnv) { + submit(userEnv, pwEnv); +} else { + rl.write('Please enter Kumo cloud credentials\n') + rl.question("Enter username:", function (user: string) { + console.log("Enter password:"); + mutableStdout.muted = true; + rl.question("Enter password:", function (pass: string) { + submit(user, pass); + rl.close() + }) + }) +}