Skip to content

Commit 6ae7455

Browse files
committed
feat(ctl,conf): update docs, chaneg create to write
1 parent 177c045 commit 6ae7455

File tree

6 files changed

+127
-101
lines changed

6 files changed

+127
-101
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ import { Ctl } from "@systemd-js/ctl";
166166

167167
const ctl = new Ctl("test.service");
168168

169+
ctl.isActive();
170+
ctl.isEnabled();
171+
ctl.write();
169172
ctl.disable();
170173
ctl.enable();
171174
ctl.stop();
@@ -196,7 +199,7 @@ service
196199

197200
const ctl = new Ctl("example", service);
198201

199-
ctl.create();
202+
ctl.write();
200203
ctl.enable();
201204
ctl.start();
202205
```
@@ -206,10 +209,14 @@ In addition to `Ctl` class, package expose functions to call systemctl directly.
206209
```ts
207210
import { restart, start, stop } from "@systemd-js/ctl";
208211

212+
write("example.service");
209213
stop("example.service");
210214
start("example.service");
211215
enable("example.service");
212216
disable("example.service");
213217
reload("example.service");
214218
restart("example.service");
219+
isActive("example.service");
220+
isEnabled("example.service");
221+
daemonReload();
215222
```

packages/conf/Readme.md renamed to packages/conf/README.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
## @systemd-js/conf
22

3-
INI parser for systemd config files. Set of fluent builders to create and modify unit files.
4-
Based on systemd v255.4
3+
INI parser for systemd config files. Set of fluent builders to create and modify
4+
unit files. Based on systemd v255.4
55

66
### Installation
77

@@ -13,7 +13,8 @@ yarn add @systemd-js/conf
1313

1414
Parse systemd ini file into object.
1515

16-
Note: Ini parser is not fully implemented, lacks support for escaping and quoting.
16+
Note: Ini parser is not fully implemented, lacks support for escaping and
17+
quoting.
1718

1819
```ts
1920
import {INI} from "@systemd-js/conf";
@@ -58,7 +59,6 @@ const ini = INI.fromString(unit).toObject();
5859
User: "root",
5960
},
6061
};
61-
6262
```
6363

6464
Create service unit for ini string and modify service user definition.
@@ -82,46 +82,46 @@ PrivateTmp=yes
8282
User=root
8383
`;
8484

85-
const ini = INI.fromString(unit)
86-
const service = Service.fromINI(ini)
85+
const ini = INI.fromString(unit);
86+
const service = Service.fromINI(ini);
8787

8888
service
8989
.getServiceSection()
90-
.setUser("test")
90+
.setUser("test");
9191

9292
service.toINIString();
9393
```
9494

9595
Create service unit using fluent builder
9696

9797
```ts
98-
import {Service} from "@systemd-js/config";
98+
import { Service } from "@systemd-js/config";
9999

100100
const service = new Service();
101101

102102
service
103103
.getUnitSection()
104-
.setDescription("This is a example unit")
104+
.setDescription("This is a example unit");
105105

106106
service
107107
.getInstallSection()
108-
.setWantedBy("multi-user.target")
109-
108+
.setWantedBy("multi-user.target");
109+
110110
service
111-
.getServiceSection()
112-
.setType("simple")
113-
.setWorkingDirectory("/tmp")
114-
.setRestart("always")
115-
.setExecStartPre("/usr/bin/echo 'Before'")
116-
.setExecStart("/usr/bin/echo 'Hello World'")
117-
.setExecStartPost("/usr/bin/echo 'After'")
111+
.getServiceSection()
112+
.setType("simple")
113+
.setWorkingDirectory("/tmp")
114+
.setRestart("always")
115+
.setExecStartPre("/usr/bin/echo 'Before'")
116+
.setExecStart("/usr/bin/echo 'Hello World'")
117+
.setExecStartPost("/usr/bin/echo 'After'");
118118
```
119119

120120
Create timer unit using fluent builder
121121

122122
```ts
123-
import {Timer} from "@systemd-js/config";
124-
const timer = new Timer()
123+
import { Timer } from "@systemd-js/config";
124+
const timer = new Timer();
125125

126126
timer
127127
.getUnitSection()
@@ -130,7 +130,7 @@ timer
130130

131131
timer
132132
.getInstallSection()
133-
.setWantedBy("multi-user.target");
133+
.setWantedBy("multi-user.target");
134134

135135
timer
136136
.getTimerSection()

packages/ctl/README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
## @systemd-js/ctl
2+
3+
Control over units. Interface to systemctl. At the moment this lack proper error
4+
handling.
5+
6+
### Installation
7+
8+
```sh
9+
yarn add @systemd-js/ctl
10+
```
11+
12+
### Examples
13+
14+
State manipulation of existing service.
15+
16+
```ts
17+
import { Ctl } from "@systemd-js/ctl";
18+
19+
const ctl = new Ctl("test.service");
20+
21+
ctl.isActive();
22+
ctl.isEnabled();
23+
ctl.write();
24+
ctl.disable();
25+
ctl.enable();
26+
ctl.stop();
27+
ctl.start();
28+
ctl.restart();
29+
```
30+
31+
Creation of new service "example.service"
32+
33+
```ts
34+
import { Service } from "@systemd-js/config";
35+
import { Ctl } from "@systemd-js/ctl";
36+
37+
const service = new Service();
38+
39+
service
40+
.getUnitSection()
41+
.setDescription("This is a example unit");
42+
43+
service
44+
.getInstallSection()
45+
.setWantedBy("multi-user.target");
46+
47+
service
48+
.getServiceSection()
49+
.setType("simple")
50+
.setExecStart("/usr/bin/echo 'Hello World'");
51+
52+
const ctl = new Ctl("example", service);
53+
54+
ctl.write();
55+
ctl.enable();
56+
ctl.start();
57+
```
58+
59+
In addition to `Ctl` class, package expose functions to call systemctl directly.
60+
61+
```ts
62+
import { restart, start, stop } from "@systemd-js/ctl";
63+
64+
write("example.service");
65+
stop("example.service");
66+
start("example.service");
67+
enable("example.service");
68+
disable("example.service");
69+
reload("example.service");
70+
restart("example.service");
71+
isActive("example.service");
72+
isEnabled("example.service");
73+
daemonReload();
74+
```

packages/ctl/Readme.md

Lines changed: 0 additions & 71 deletions
This file was deleted.

packages/ctl/src/ctl.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,25 @@ function getUnit(unitName: string, type: string = getType(unitName)): Unit | und
5757
};
5858

5959
/**
60-
* Create a unit file if it does not exist or if it is different from the current unit.
60+
* Write unit file to filesystem.
61+
* Create the unit file if it does not or update if it is different from the current unit.
62+
* @returns "created" | "updated" | "unchanged"
6163
*/
62-
export function create(unitName: string, unit: Unit) {
64+
export function write(unitName: string, unit: Unit) {
6365
const name = getName(unitName);
6466
const type = getType(unitName, unit);
6567
const path = getPath (name, type);
6668

6769
const current = getUnit(name, type);
6870

6971
if (unit.equals(current)) {
70-
writeFileSync(path, unit.toINIString());
72+
return "unchanged";
7173
}
74+
writeFileSync(path, unit.toINIString());
75+
76+
return current
77+
? "updated"
78+
: "created";
7279
}
7380

7481
export function reload(unitName: string, unit?: Unit) {
@@ -170,15 +177,24 @@ export class Ctl {
170177
}
171178

172179
/**
173-
* Create the unit file if it does not exist or if it is different from the current unit.
180+
* Write unit file to filesystem.
181+
* Create the unit file if it does not or update if it is different from the current unit.
182+
* @returns "created" | "updated" | "unchanged"
174183
*/
175-
public create() {
184+
public write() {
176185
if (!this.unit) {
177186
throw new Error("Unit not found");
178187
}
179-
if (!this.unit.equals(this.current)) {
180-
writeFileSync(this.path, this.unit.toINIString());
188+
189+
if (this.unit.equals(this.current)) {
190+
return "unchanged";
181191
}
192+
193+
writeFileSync(this.path, this.unit.toINIString());
194+
195+
return this.current
196+
? "updated"
197+
: "created";
182198
}
183199

184200
/**

packages/ctl/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export {
22
Ctl,
33

44
reload,
5-
create,
5+
write,
66
enable,
77
disable,
88
start,

0 commit comments

Comments
 (0)