-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknockerService.js
More file actions
111 lines (101 loc) · 2.99 KB
/
Copy pathknockerService.js
File metadata and controls
111 lines (101 loc) · 2.99 KB
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
/* knockerService.js
*
* Service management for Knocker systemd service and CLI commands
*/
import Gio from 'gi://Gio';
export class KnockerService {
constructor() {
this._cancellable = new Gio.Cancellable();
}
/**
* Check if knocker-cli is installed
* @returns {Promise<boolean>}
*/
async checkKnockerInstalled() {
try {
const [success, stdout] = await this._execCommand(['which', 'knocker']);
return success && stdout.trim().length > 0;
} catch (_err) {
return false;
}
}
/**
* Start the knocker.service (user mode)
* @returns {Promise<boolean>}
*/
async startService() {
try {
const [success] = await this._execCommand(['systemctl', '--user', 'start', 'knocker.service']);
return success;
} catch (e) {
console.error('Failed to start knocker.service:', e);
return false;
}
}
/**
* Stop the knocker.service (user mode)
* @returns {Promise<boolean>}
*/
async stopService() {
try {
const [success] = await this._execCommand(['systemctl', '--user', 'stop', 'knocker.service']);
return success;
} catch (e) {
console.error('Failed to stop knocker.service:', e);
return false;
}
}
/**
* Check if knocker.service is active
* @returns {Promise<boolean>}
*/
async isServiceActive() {
try {
const [success, stdout] = await this._execCommand(['systemctl', '--user', 'is-active', 'knocker.service']);
return success && stdout.trim() === 'active';
} catch (_err) {
return false;
}
}
/**
* Trigger a manual knock
* @returns {Promise<boolean>}
*/
async triggerKnock() {
try {
const [success] = await this._execCommand(['knocker', 'knock']);
return success;
} catch (e) {
console.error('Failed to trigger knock:', e);
return false;
}
}
/**
* Execute a command and return result
* @private
*/
_execCommand(argv) {
return new Promise((resolve, reject) => {
try {
const proc = Gio.Subprocess.new(
argv,
Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE,
);
proc.communicate_utf8_async(null, this._cancellable, (proc, res) => {
try {
const [, stdout, stderr] = proc.communicate_utf8_finish(res);
const success = proc.get_successful();
resolve([success, stdout || '', stderr || '']);
} catch (e) {
reject(e);
}
});
} catch (e) {
reject(e);
}
});
}
destroy() {
this._cancellable.cancel();
}
}