-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·94 lines (83 loc) · 2.62 KB
/
index.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
#! /usr/bin/env node
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const { Container, ContainerManager } = require('./components/containers');
const manager = new ContainerManager();
async function ps(format) {
var psCmd = 'docker ps';
if ( format ) {
psCmd += ' --format \'' + format + '\'';
}
var { stdout, stderr } = await exec(psCmd);
if (stderr) {
console.log('`' + psCmd + '` returned an error:');
console.log(stderr);
process.exit(1);
}
return stdout;
}
async function main() {
var psResult = await ps('{{.Names}}|{{.ID}}')
psResult.split('\n')
.filter((v) => {return v;})
.map((v) => {
return v.split('|');
})
.map((v) => {
manager.put(v[1], v[0]);
});
// console.log(manager);
const fuzz = require('fuzzball');
const options = { scorer: fuzz.partial_ratio };
var matches = bestMatches = fuzz.extract(target, manager.listNames(), options);
// console.log(matches);
if (matches[0][1] == 100) {
bestMatches = matches.filter((v) => {
return v[1] >= matches[0][1];
});
}
if (bestMatches.length == 1) {
var match = bestMatches[0];
if ( match[1] != 100 ) {
const prompts = require('prompts');
let desc = manager.getByName(match[0]).getDescription();
const response = await prompts({
type: 'confirm',
name: 'connect',
message: 'Connect to: ' + desc,
initial: true,
});
if (!response.connect) return;
manager.getByName(match[0]).bash();
} else {
manager.getByName(match[0]).bash();;
}
} else if (bestMatches.length > 1) {
const prompts = require('prompts');
var choicifiedBestMatches = bestMatches.reduce((acc, item, i) => {
var c = manager.getByName(item[0]);
acc.push({
title: c.getDescription(),
value: c,
})
return acc;
}, []);
var msg = '';
if ( target ) {
msg = 'Multiple matches, choose container';
} else {
msg = 'No filter. Choose container';
}
const response = await prompts({
type: 'select',
name: 'container',
message: msg,
choices: choicifiedBestMatches,
});
if (!response.container) return;
response.container.bash();
}
}
const args = process.argv.slice(2);
const target = args[0];
main();