Skip to content

Commit

Permalink
Merge pull request #36 from oslabs-beta/dev
Browse files Browse the repository at this point in the history
Verified that passes all current functional testing.
  • Loading branch information
Frynoceros authored Aug 4, 2022
2 parents 1f82cfa + 872441e commit 389cc49
Show file tree
Hide file tree
Showing 37 changed files with 1,907 additions and 792 deletions.
104 changes: 46 additions & 58 deletions README.md

Large diffs are not rendered by default.

Binary file added assets/readmeImages/config_demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/readmeImages/config_demo2.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed assets/readmeImages/gifs/create_db.gif
Binary file not shown.
Binary file added assets/readmeImages/gifs/create_db3.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions backend/BE_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface TableDetails {
columns?: ColumnObj[];
}
export interface DBList {
databaseConnected: boolean[];
databaseList: dbDetails[];
tableList: TableDetails[];
}
Expand All @@ -50,4 +51,13 @@ export enum LogType {
NORMAL = 'NORMAL',
SEND = 'SEND',
RECEIVE = 'RECEIVE'
}

export interface DocConfigFile {
mysql_user: string,
mysql_pass: string,
mysql_port: number | string,
pg_user: string,
pg_pass: string,
pg_port: number | string
}
123 changes: 66 additions & 57 deletions backend/Logging/masterlog.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,72 @@
import { LogType } from "../BE_types";

/* eslint-disable func-names */
/* eslint-disable @typescript-eslint/no-unused-vars */
import { LogType } from '../BE_types';

interface Logger {
log: (message: string, logType?: LogType, opt1?: any, opt2?: any) => void;
log: (message: string, logType?: LogType, opt1?: any, opt2?: any) => void;
}

//Helper functions!
// Helper functions!
const saveLogMessage = (message) => {
const time = new Date().toLocaleDateString('en-US', {timeZone: 'UTC'});
}
const time = new Date().toLocaleDateString('en-US', { timeZone: 'UTC' });
};

//Export
const logger = function(message: string, logType: LogType = LogType.NORMAL, opt1?, opt2?) {
//Code for the log color
let colorCode = 0;

//Prefix for the message
let logString: any = logType;

switch(logType) {
case LogType.ERROR: //RED
colorCode = 31;
break;

case LogType.SUCCESS: //GREEN
colorCode = 32;
break;

case LogType.WARNING: //YELLOW
colorCode = 33;
break;

case LogType.RECEIVE: //BLUE
colorCode = 34;
logString = 'SERVER_IPC_RECEIVE';
break;

case LogType.SEND: //MAGENTA
colorCode = 35;
logString = 'SERVER_IPC_SEND';
break;

case LogType.NORMAL: //WHITE (And change logType string)
colorCode = 0;
logString = 'LOG';
break;

default:
colorCode = 0;
break;
}

let moreText = '';

if(opt1) moreText += JSON.stringify(opt1);
if(opt2) moreText += JSON.stringify(opt2);

console.log( `\u001b[1;${colorCode}m ${`[${logType}] ${message + moreText}`}` + `\u001b[1;0m`);
saveLogMessage(`[${logType}] ${message}`);
}

export default logger;
// Export
const logger = function (
message: string,
logType: LogType = LogType.NORMAL,
opt1?,
opt2?
) {
// Code for the log color
let colorCode = 0;

// Prefix for the message
let logString: any = logType;

switch (logType) {
case LogType.ERROR: // RED
colorCode = 31;
break;

case LogType.SUCCESS: // GREEN
colorCode = 32;
break;

case LogType.WARNING: // YELLOW
colorCode = 33;
break;

case LogType.RECEIVE: // BLUE
colorCode = 34;
logString = 'SERVER_IPC_RECEIVE';
break;

case LogType.SEND: // MAGENTA
colorCode = 35;
logString = 'SERVER_IPC_SEND';
break;

case LogType.NORMAL: // WHITE (And change logType string)
colorCode = 0;
logString = 'LOG';
break;

default:
colorCode = 0;
break;
}

let moreText = '';

if (opt1) moreText += JSON.stringify(opt1);
if (opt2) moreText += JSON.stringify(opt2);

console.log(
`\u001b[1;${colorCode}m ${`[${logType}] ${message + moreText}`}` +
`\u001b[1;0m`
);
saveLogMessage(`[${logType}] ${message}`);
};

export default logger;
112 changes: 112 additions & 0 deletions backend/_documentsConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/* eslint-disable no-throw-literal */
/* eslint-disable no-shadow */
/* eslint-disable object-shorthand */
// import path from 'path';
import fs from 'fs';
import os from 'os';
import { DBType, DocConfigFile, LogType } from './BE_types';
import logger from './Logging/masterlog';

const home = `${os.homedir()}/Documents/SeeQR`;
const configFile = `config.json`;
const configPath = `${home}/${configFile}`;

const writeConfigDefault = function (): DocConfigFile {
logger('Could not find config file. Creating default', LogType.WARNING);

const defaultFile: DocConfigFile = {
mysql_user: 'mysql',
mysql_pass: 'mysql',
mysql_port: 3306,
pg_user: 'postgres',
pg_pass: 'postgres',
pg_port: 5432
};

fs.writeFileSync(configPath, JSON.stringify(defaultFile));

return defaultFile;
};

const readConfigFile = function (): DocConfigFile {
if (fs.existsSync(configPath)) {
try {
const text = fs.readFileSync(configPath, 'utf-8');
return JSON.parse(text) as DocConfigFile;
} catch (err: any) {
throw `Error parsing config file: ${err.message}`;
}
} else {
return writeConfigDefault();
}
};

interface DocConfig {
getConfigFolder: () => string;
getCredentials: (dbType: DBType) => { user: string; pass: string, port: number | string };
getFullConfig: () => Object;
saveConfig: (config: Object) => void;
}

const docConfig: DocConfig = {
getConfigFolder: function () {
if (fs.existsSync(home)) {
logger(`Found documents directory: ${home}`, LogType.SUCCESS);
} else {
logger(
`Could not find documents directory. Creating at: ${home}`,
LogType.WARNING
);
fs.mkdirSync(home);
}
return home;
},

getCredentials: function (dbType: DBType) {
this.getConfigFolder();

let configFile: DocConfigFile;
try {
configFile = readConfigFile();
} catch (err: any) {
logger(err.message, LogType.WARNING);
return { user: 'none', pass: 'none', port: 1 };
}

if (dbType === DBType.Postgres) {
return { user: configFile.pg_user, pass: configFile.pg_pass, port: configFile.pg_port };
}
if (dbType === DBType.MySQL) {
return { user: configFile.mysql_user, pass: configFile.mysql_pass, port: configFile.mysql_port };
}
logger('Could not get credentials of DBType: ', LogType.ERROR, dbType);
return { user: 'none', pass: 'none', port: 1 };
},

getFullConfig: function() {
this.getConfigFolder();
let configFile: DocConfigFile;
try {
configFile = readConfigFile();
return configFile;
} catch (err: any) {
logger(err.message, LogType.WARNING);
return {
mysql_user: 'Failed to retrieve data.', mysql_pass: 'Failed to retrieve data.', mysql_port: 'Failed to retrieve data.',
pg_user: 'Failed to retrieve data.', pg_pass: 'Failed to retrieve data.', pg_port: 'Failed to retrieve data.'
};
}
},

saveConfig: function(config: Object) {
try {
fs.writeFileSync(configPath, JSON.stringify(config));
logger('Saved new config: ', LogType.NORMAL, config);
} catch(err: any) {
logger(err.message, LogType.WARNING);
}

}
};

module.exports = docConfig;
Loading

0 comments on commit 389cc49

Please sign in to comment.