forked from hexparrot/mineos-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mineos_console.js
149 lines (129 loc) · 4.16 KB
/
mineos_console.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env node
var getopt = require('node-getopt');
var mineos = require('./mineos');
var opt = getopt.create([
['s' , 'server_name=SERVER_NAME' , 'server name'],
['d' , 'base_dir=BASE_DIR' , 'defaults to /var/games/minecraft'],
['D' , 'debug' , 'show debug output'],
['V' , 'version' , 'show version'],
['h' , 'help' , 'display this help']
]) // create Getopt instance
.bindHelp() // bind option 'help' to default action
.parseSystem(); // parse command line
function return_git_commit_hash(callback) {
var child_process = require('child_process');
var gitproc = child_process.spawn('git', 'log -n 1 --pretty=format:"%H"'.split(' '));
var commit_value = '';
gitproc.stdout.on('data', function(data) {
var buffer = new Buffer(data, 'ascii');
commit_value = buffer.toString('ascii');
});
gitproc.on('error', function(code) {
// branch if path does not exist
if (code != 0)
callback(true, undefined);
});
gitproc.on('exit', function(code) {
if (code == 0) // branch if all is well
callback(code, commit_value);
else
callback(true, undefined);
});
}
function handle_server(args, callback) {
var introspect = require('introspect');
var base_dir = (args.options || {}).d || '/var/games/minecraft';
var command = args.argv.shift();
var fn = instance[command];
var arg_array = [];
var required_args = introspect(fn);
while (required_args.length) {
var ra = required_args.shift();
switch (ra) {
case 'callback':
arg_array.push(function(err, payload) {
var retval = [];
if (!err) {
retval.push('[{0}] Successfully executed "{1}"'.format(args.options.server_name, command));
if (payload)
retval.push(payload)
} else {
retval.push('[{0}] Error executing "{1}" because server condition not met: {2}'.format(
args.options.server_name,
command,
err)
);
}
callback( (err ? 1 : 0), retval );
})
break;
case 'owner':
try {
var owner_pair = opt.argv.shift().split(':');
if (owner_pair.length != 2)
throw 'err';
arg_array.push({
uid: parseInt(owner_pair[0]),
gid: parseInt(owner_pair[1])
})
} catch (e) {
callback(1, ['Provide owner attribute as uid:gid pair, e.g., 1000:1000']);
return;
}
break;
default:
arg_array.push(opt.argv.shift())
break;
} //end switch
} //end while
fn.apply(instance, arg_array); //actually run the function with the args
}
function retrieve_property(args, callback) {
var property = args.argv.shift();
var fn = instance.property;
var arg_array = [property];
var retval = [];
arg_array.push(function(err, payload) {
if (!err && payload !== undefined) {
retval.push('[{0}] Queried property: "{1}"'.format(args.options.server_name, property));
retval.push(payload);
} else {
retval.push('[{0}] Error querying property "{1}"'.format(
args.options.server_name,
property,
err));
}
callback( (err ? 1 : 0), retval);
})
fn.apply(instance, arg_array);
}
var base_dir = (opt.options || {}).base_dir || '/var/games/minecraft';
if ('version' in opt.options) {
return_git_commit_hash(function(code, hash) {
if (!code)
console.log(hash);
process.exit(code);
})
} else {
var instance = new mineos.mc(opt.options.server_name, base_dir);
if (opt.argv[0] in instance) { //first provided param matches a function name) {
handle_server(opt, function(code, retval) {
for (var idx in retval)
console.log(retval[idx])
process.exit(code);
})
} else {
retrieve_property(opt, function(code, retval) {
for (var idx in retval)
console.log(retval[idx])
process.exit(code);
})
}
}
String.prototype.format = function() {
var s = this;
for(var i = 0, iL = arguments.length; i<iL; i++) {
s = s.replace(new RegExp('\\{'+i+'\\}', 'gm'), arguments[i]);
}
return s;
};