forked from bhurlow/machine-share
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
66 lines (51 loc) · 1.67 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
61
62
63
64
65
66
var fs = require('fs')
var os = require('os');
var path = require('path');
module.exports.copy = function (from, to_folder) {
var name = path.basename(from);
var dest = path.join(to_folder, name);
var file = fs.readFileSync(from);
fs.writeFileSync(dest, file);
console.log(`Copied ${from} to ${dest}`);
};
module.exports.isfile = function (path) {
var stat;
try {
stat = fs.statSync(path)
} catch (e) {
return false;
}
return stat.isFile();
};
module.exports.startsWith = function (s, prefix) {
return s.substring(0, prefix.length) === prefix;
};
// common paths
// var dm = new util.dm(machine_name);
//
module.exports.dm = function(_name) {
var tmpdir = '' + os.tmpdir();
if( tmpdir.length <= 1) {
throw new Error('bad tmp dir: ' + tmpdir);
}
var nname = path.normalize(_name);
if( nname.length < 1
// || nname.match(/\W/) != null
) {
throw new Error( 'bad machine name "' + _name + '"');
}
this._name = nname;
this.temp = '' + os.tmpdir();
this.home = os.homedir();
this.dock = path.join( this.home, '.docker', 'machine');
this.machines = path.join( this.dock, 'machines');
this.certsName = 'certs';
this.certsDir = path.join( this.dock, this.certsName);
this.configDir = path.join( this.machines, this._name);
this.configFile = path.join( this.configDir, 'config.json');
this.tmpDir = path.join( tmpdir, this._name);
this.tmpConfigFile = path.join( this.tmpDir, 'config.json');
this.tmpCerts = path.join( this.tmpDir, this.certsName);
this.extName = 'ext';
this.tmpExt = path.join( this.tmpDir, this.extName);
};