-
Notifications
You must be signed in to change notification settings - Fork 2
/
example_test.go
72 lines (62 loc) · 1.62 KB
/
example_test.go
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
// To avoid a cyclic dependency with systemdconf <-> go-systemdconf/unit
package systemdconf_test
import (
"fmt"
"github.com/sergeymakinen/go-systemdconf/v2"
"github.com/sergeymakinen/go-systemdconf/v2/unit"
)
func ExampleMarshal() {
service := unit.ServiceFile{
Unit: unit.UnitSection{
Description: systemdconf.Value{"Simple firewall"},
},
Service: unit.ServiceSection{
Type: systemdconf.Value{"oneshot"},
RemainAfterExit: systemdconf.Value{"yes"},
ExecStart: systemdconf.Value{
"/usr/local/sbin/simple-firewall-start1",
"/usr/local/sbin/simple-firewall-start2",
},
ExecStop: systemdconf.Value{"/usr/local/sbin/simple-firewall-stop"},
},
Install: unit.InstallSection{
WantedBy: systemdconf.Value{"multi-user.target"},
},
}
b, _ := systemdconf.Marshal(service)
fmt.Println(string(b))
// Output:
// [Unit]
// Description=Simple firewall
//
// [Service]
// Type=oneshot
// RemainAfterExit=yes
// ExecStart=/usr/local/sbin/simple-firewall-start1
// ExecStart=/usr/local/sbin/simple-firewall-start2
// ExecStop=/usr/local/sbin/simple-firewall-stop
//
// [Install]
// WantedBy=multi-user.target
}
func ExampleUnmarshal() {
file := `[Unit]
Description=Simple firewall
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/sbin/simple-firewall-start
ExecStop=/usr/local/sbin/simple-firewall-stop
[Install]
WantedBy=multi-user.target`
var service unit.ServiceFile
systemdconf.Unmarshal([]byte(file), &service)
fmt.Println(service.Unit.Description)
b, _ := service.Service.RemainAfterExit.Bool()
if b {
fmt.Println(b)
}
// Output:
// Simple firewall
// true
}