-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
94 lines (85 loc) · 2.89 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
const { app, Menu, Tray, BrowserWindow } = require('electron')
const { Docker } = require('node-docker-api');
const path = require('path');
const docker = new Docker({ socketPath: '/var/run/docker.sock' });
let appIcon = null
let wins = {}
function refresh() {
docker.container.list()
.then(containers => {
const menu_items = containers.map(c => ({
label: `${c.data.Names.join(' ')} - ${c.data.Image}`,
submenu: [
{
label: 'Stop',
click: () => {
c.stop().then(() => refresh())
}
},
{
label: 'Logs',
click: () => {
if (wins[c.data.Id]) {
wins[c.data.Id].show()
} else {
wins[c.data.Id] = new BrowserWindow({ width: 800, height: 600, webPreferences: { nodeIntegration: true } })
wins[c.data.Id].on('close', (event) => {
event.preventDefault()
wins[c.data.Id].hide()
event.returnValue = false
return false
})
wins[c.data.Id].setMenuBarVisibility(false)
wins[c.data.Id].loadFile('logs.html')
wins[c.data.Id].webContents.on('did-finish-load', () => {
c.logs({
follow: true,
stdout: true,
stderr: true
})
.then(stream => {
stream.on('data', info => {
if (wins[c.data.Id].webContents) {
wins[c.data.Id].webContents.send('ping', info.toString());
}
})
stream.on('error', err => {
if (wins[c.data.Id].webContents) {
wins[c.data.Id].webContents.send('ping', err.toString());
}
})
})
})
}
}
}
]
}))
const contextMenu = Menu.buildFromTemplate(menu_items)
// Make a change to the context menu
contextMenu.items[1].checked = false
// Call this again for Linux because we modified the context menu
appIcon.setContextMenu(contextMenu)
})
.catch(error => console.log(error));
}
let imgPath
if (!process.env.APP_DEV) {
imgPath = path.join(process.resourcesPath, 'logo.png')
} else {
imgPath = __dirname + '/resources/logo.png'
}
app.on('ready', () => {
appIcon = new Tray(imgPath)
const promisifyStream = stream => new Promise((resolve, reject) => {
stream.on('data', data => refresh())
stream.on('end', resolve)
stream.on('error', reject)
})
docker.events({
since: ((new Date().getTime() / 1000) - 60).toFixed(0)
})
.then(stream => promisifyStream(stream))
.catch(error => console.log(error))
refresh()
})