update-config ipc calls now using new cfg.update_config as backend

This commit is contained in:
shockrah 2021-02-11 22:27:15 -08:00
parent 316b5ad1fd
commit ec6701418c

View File

@ -1,11 +1,14 @@
const { ipcRenderer } = require('electron')
const { ArgumentParser } = require('argparse') const { ArgumentParser } = require('argparse')
const fs = require('fs') const fs = require('fs')
/** /**
* Gets the config data off disk
* Because we're calling this from node's process we can actuall us the 'fs' module here
* @param {ArgumentParset} parser * @param {ArgumentParset} parser
* @returns {Config} { path } * @returns {Config} { path }
*/ */
function get_config(parser) { exports.get_config = function(parser) {
const a = parser.parse_args() const a = parser.parse_args()
const defaultcfg = `${process.env.HOME || process.env.USERPROFILE}/.config/freechat/config.json` const defaultcfg = `${process.env.HOME || process.env.USERPROFILE}/.config/freechat/config.json`
@ -32,11 +35,30 @@ function get_config(parser) {
return ret return ret
} }
/**
* Updates the config with the new object data
* Called from node's main process
*
* @param {Config} config new config data to write
* @param {String} path | set to 'default' if we're using $HOME/.config/freechat/config.json
* Set this to anything else to write changes to that specified path
*/
exports.update_config = function(config, path='default') {
if (path == 'default') {
path = `${process.env.HOME || process.env.USERPROFILE}/.config/freechat/config.json`
} else {
path = path
}
const data = JSON.stringify(config)
fs.writeFile(path, data)
}
/** /**
* Loads the parse with the various arguments that the program takes * Loads the parse with the various arguments that the program takes
* @param {ArgumentParser} parser * @param {ArgumentParser} parser
*/ */
function load_parser() { exports.load_parser = function() {
const parser = new ArgumentParser({ const parser = new ArgumentParser({
description: 'Freechat Electron Client' description: 'Freechat Electron Client'
}) })
@ -44,5 +66,39 @@ function load_parser() {
return parser return parser
} }
exports.load_parser = load_parser /**
exports.get_config = get_config * NOTE: can possibly return undefined jwt values
*
* @param {String} domain | server config to search for jwt + id
* @param {String} key | basically are we looking for 'jwt' or 'secret'
*/
function get_creds(domain, key) {
let serv_id, serv_jwt
const config = ipcRenderer.sendSync('config-request')
for(const server in config['servers']) {
if(server['domain'] === domain) {
id = server['id']
jwt = server[key]
}
}
return {id: serv_id, jwt: serv_jwt}
}
/**
* Attempts to grab the id + jwt values from the config
* NOTE: jwt key-value can realistically be undefined, or the JWT can be malformed/expired etc.
* @param {String} domain
*/
exports.jwt_creds = function(domain) {
return get_creds(domain, 'jwt')
}
/**
* Attempts to pull id + secret values from the config
*
* @param {String} domain
*/
exports.basic_creds = function(domain) {
return get_creds(domain , 'secret')
}