diff --git a/aix.js b/aix.js index ce5290d..a0c646f 100644 --- a/aix.js +++ b/aix.js @@ -1,19 +1,11 @@ "use strict"; -const getSqlStatement = family => { - let sql = - "select NEXT_HOP, LOCAL_BINDING_INTERFACE from QSYS2.NETSTAT_ROUTE_INFO where ROUTE_TYPE='DFTROUTE' and NEXT_HOP!='*DIRECT'"; - - if (family === "v4") { - sql += " and CONNECTION_TYPE='IPV4'"; - } else { - sql += " and CONNECTION_TYPE='IPV6'"; - } - - return sql; +const queries = { + v4: "select NEXT_HOP, LOCAL_BINDING_INTERFACE from QSYS2.NETSTAT_ROUTE_INFO where ROUTE_TYPE='DFTROUTE' and NEXT_HOP!='*DIRECT' and CONNECTION_TYPE='IPV4'", + v6: "select NEXT_HOP, LOCAL_BINDING_INTERFACE from QSYS2.NETSTAT_ROUTE_INFO where ROUTE_TYPE='DFTROUTE' and NEXT_HOP!='*DIRECT' and CONNECTION_TYPE='IPV6'", }; -const getGatewayInformationAsync = async family => { +const get = family => { return new Promise((resolve, reject) => { try { const idbConnector = require("idb-connector"); @@ -21,72 +13,56 @@ const getGatewayInformationAsync = async family => { const dbconn = new idbConnector.dbconn(); dbconn.conn("*LOCAL"); - const sql = getSqlStatement(family); - const stmt = new idbConnector.dbstmt(dbconn); - - stmt.exec(sql, async results => { + const dbstmt = new idbConnector.dbstmt(dbconn); + dbstmt.exec(queries[family], results => { try { - stmt.close(); + dbstmt.close(); dbconn.disconn(); dbconn.close(); } catch (err) { - reject(new Error("Unable to determine default gateway")); - return; + return reject(err); } if (results && results[0] && results[0].NEXT_HOP) { resolve({ gateway: results[0].NEXT_HOP, - interface: results[0].LOCAL_BINDING_INTERFACE || "" + interface: results[0].LOCAL_BINDING_INTERFACE || null, }); } else { - reject(new Error("Unable to determine default gateway")); + return reject(new Error("Unable to determine default gateway")); } }); } catch (err) { - reject(new Error("Unable to determine default gateway")); - return; + return reject(err); } }); }; -const getGatewayInformationSync = family => { - let results; - try { - const idbConnector = require("idb-connector"); +const getSync = family => { + const idbConnector = require("idb-connector"); - const dbconn = new idbConnector.dbconn(); - dbconn.conn("*LOCAL"); + const dbconn = new idbConnector.dbconn(); + dbconn.conn("*LOCAL"); - const sql = getSqlStatement(family); - const stmt = new idbConnector.dbstmt(dbconn); + const dbstmt = new idbConnector.dbstmt(dbconn); + const results = dbstmt.execSync(queries[family]); - results = stmt.execSync(sql); - - stmt.close(); - dbconn.disconn(); - dbconn.close(); - } catch (err) { - throw new Error("Unable to determine default gateway"); - } + dbstmt.close(); + dbconn.disconn(); + dbconn.close(); if (results && results[0] && results[0].NEXT_HOP) { return { gateway: results[0].NEXT_HOP, - interface: results[0].LOCAL_BINDING_INTERFACE || "" + interface: results[0].LOCAL_BINDING_INTERFACE || null, }; } else { throw new Error("Unable to determine default gateway"); } }; -const promise = family => { - return getGatewayInformationAsync(family); -}; - -const sync = family => { - return getGatewayInformationSync(family); -}; +const promise = family => get(family); +const sync = family => getSync(family); module.exports.v4 = () => promise("v4"); module.exports.v6 = () => promise("v6");