-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.js
60 lines (53 loc) · 1.24 KB
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"use strict"
var exec = require('child_process').exec
function clean_fieldname(name) {
if(name[0] == '+') {
name = 'pos_' + name
} else if(name[0] == '-') {
name = 'neg_' + name
} else if(/[^a-zA-Z_]/.test(name[0])) {
name = '_' + name
}
return name.replace(/[^a-zA-Z0-9_]/g, '_')
}
function split_with_escape(string, char) {
var a = []
char = char || ':'
string.split(char).forEach(function(e) {
var prev = a[a.length - 1]
if(prev && prev.substr(-1) == "\\") {
a[a.length - 1] = prev.slice(0, -1) + char + e
} else {
a.push(e)
}
})
return a
}
function parse_zones_list(stdout) {
var zones = {}
stdout.trim().split('\n').forEach(function(line) {
line = split_with_escape(line)
if(line.length != 2) return
zones[line[0]] = line[1]
})
return zones
}
function kstat(stats, cb) {
exec("kstat -p " + stats.join(' '), function(err, stdout, stderr) {
if(err) {
cb(err)
return
}
var list = {}
stdout.split('\n').forEach(function(line) {
line = line.trim().split('\t')
if(line.length != 2) return
list[line[0]] = line[1]
})
cb(null, list)
})
}
exports.clean_fieldname = clean_fieldname
exports.parse_zones_list = parse_zones_list
exports.split_with_escape = split_with_escape
exports.kstat = kstat