Skip to content

Commit f7903f5

Browse files
committed
various tweaks
1 parent 0680f5b commit f7903f5

File tree

1 file changed

+23
-47
lines changed

1 file changed

+23
-47
lines changed

aix.js

Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,68 @@
11
"use strict";
22

3-
const getSqlStatement = family => {
4-
let sql =
5-
"select NEXT_HOP, LOCAL_BINDING_INTERFACE from QSYS2.NETSTAT_ROUTE_INFO where ROUTE_TYPE='DFTROUTE' and NEXT_HOP!='*DIRECT'";
6-
7-
if (family === "v4") {
8-
sql += " and CONNECTION_TYPE='IPV4'";
9-
} else {
10-
sql += " and CONNECTION_TYPE='IPV6'";
11-
}
12-
13-
return sql;
3+
const queries = {
4+
v4: "select NEXT_HOP, LOCAL_BINDING_INTERFACE from QSYS2.NETSTAT_ROUTE_INFO where ROUTE_TYPE='DFTROUTE' and NEXT_HOP!='*DIRECT' and CONNECTION_TYPE='IPV4'",
5+
v6: "select NEXT_HOP, LOCAL_BINDING_INTERFACE from QSYS2.NETSTAT_ROUTE_INFO where ROUTE_TYPE='DFTROUTE' and NEXT_HOP!='*DIRECT' and CONNECTION_TYPE='IPV6'",
146
};
157

16-
const getGatewayInformationAsync = async family => {
8+
const get = family => {
179
return new Promise((resolve, reject) => {
1810
try {
1911
const idbConnector = require("idb-connector");
2012

2113
const dbconn = new idbConnector.dbconn();
2214
dbconn.conn("*LOCAL");
2315

24-
const sql = getSqlStatement(family);
25-
const stmt = new idbConnector.dbstmt(dbconn);
26-
27-
stmt.exec(sql, async results => {
16+
const dbstmt = new idbConnector.dbstmt(dbconn);
17+
dbstmt.exec(queries[family], results => {
2818
try {
29-
stmt.close();
19+
dbstmt.close();
3020
dbconn.disconn();
3121
dbconn.close();
3222
} catch (err) {
33-
reject(new Error("Unable to determine default gateway"));
34-
return;
23+
return reject(err);
3524
}
3625

3726
if (results && results[0] && results[0].NEXT_HOP) {
3827
resolve({
3928
gateway: results[0].NEXT_HOP,
40-
interface: results[0].LOCAL_BINDING_INTERFACE || ""
29+
interface: results[0].LOCAL_BINDING_INTERFACE || null,
4130
});
4231
} else {
43-
reject(new Error("Unable to determine default gateway"));
32+
return reject(new Error("Unable to determine default gateway"));
4433
}
4534
});
4635
} catch (err) {
47-
reject(new Error("Unable to determine default gateway"));
48-
return;
36+
return reject(err);
4937
}
5038
});
5139
};
5240

53-
const getGatewayInformationSync = family => {
54-
let results;
55-
try {
56-
const idbConnector = require("idb-connector");
41+
const getSync = family => {
42+
const idbConnector = require("idb-connector");
5743

58-
const dbconn = new idbConnector.dbconn();
59-
dbconn.conn("*LOCAL");
44+
const dbconn = new idbConnector.dbconn();
45+
dbconn.conn("*LOCAL");
6046

61-
const sql = getSqlStatement(family);
62-
const stmt = new idbConnector.dbstmt(dbconn);
47+
const dbstmt = new idbConnector.dbstmt(dbconn);
48+
const results = dbstmt.execSync(queries[family]);
6349

64-
results = stmt.execSync(sql);
65-
66-
stmt.close();
67-
dbconn.disconn();
68-
dbconn.close();
69-
} catch (err) {
70-
throw new Error("Unable to determine default gateway");
71-
}
50+
dbstmt.close();
51+
dbconn.disconn();
52+
dbconn.close();
7253

7354
if (results && results[0] && results[0].NEXT_HOP) {
7455
return {
7556
gateway: results[0].NEXT_HOP,
76-
interface: results[0].LOCAL_BINDING_INTERFACE || ""
57+
interface: results[0].LOCAL_BINDING_INTERFACE || null,
7758
};
7859
} else {
7960
throw new Error("Unable to determine default gateway");
8061
}
8162
};
8263

83-
const promise = family => {
84-
return getGatewayInformationAsync(family);
85-
};
86-
87-
const sync = family => {
88-
return getGatewayInformationSync(family);
89-
};
64+
const promise = family => get(family);
65+
const sync = family => getSync(family);
9066

9167
module.exports.v4 = () => promise("v4");
9268
module.exports.v6 = () => promise("v6");

0 commit comments

Comments
 (0)