@@ -3,47 +3,39 @@ package main
3
3
import (
4
4
"bytes"
5
5
"fmt"
6
- "html/template "
6
+ "io "
7
7
"log"
8
8
"os"
9
9
"os/exec"
10
10
"strings"
11
+
12
+ "github.com/coreos/go-systemd/unit"
11
13
)
12
14
13
15
func installService () {
14
- serviceUnit := `[Unit]
15
- Description=Local Docker Development DNS
16
- BindTo=docker.service
17
- After=docker.service
18
-
19
- [Service]
20
- Type=notify
21
- Environment="DOCKER_API_VERSION={{ .Version }}"
22
- ExecStart={{ .Executable }} service
23
- SuccessExitStatus=15
24
- Restart=on-failure
25
-
26
- [Install]
27
- WantedBy=docker.service
28
- `
29
-
30
16
version := serverVersion ()
31
17
32
18
executable , err := os .Executable ()
33
19
if err != nil {
34
20
panic (fmt .Errorf ("could not find path of executable: %w" , err ))
35
21
}
36
22
37
- data := struct {
38
- Version string
39
- Executable string
40
- }{
41
- Version : version ,
42
- Executable : executable ,
23
+ serviceUnit := unit .Serialize ([]* unit.UnitOption {
24
+ unit .NewUnitOption ("Unit" , "Description" , "Local Docker Development DNS" ),
25
+ unit .NewUnitOption ("Unit" , "BindTo" , "docker.service" ),
26
+ unit .NewUnitOption ("Unit" , "After" , "docker.service" ),
27
+ unit .NewUnitOption ("Service" , "Type" , "notify" ),
28
+ unit .NewUnitOption ("Service" , "Environment" , "DOCKER_API_VERSION=" + version ),
29
+ unit .NewUnitOption ("Service" , "ExecStart" , executable + " service" ),
30
+ unit .NewUnitOption ("Service" , "SuccessExitStatus" , "15" ),
31
+ unit .NewUnitOption ("Service" , "Restart" , "on-failure" ),
32
+ unit .NewUnitOption ("Install" , "WantedBy" , "docker.service" ),
33
+ })
34
+
35
+ _ , err = io .Copy (os .Stdout , serviceUnit )
36
+ if err != nil {
37
+ panic (fmt .Errorf ("could not output service unit: %w" , err ))
43
38
}
44
-
45
- tmpl , _ := template .New ("test" ).Parse (serviceUnit )
46
- _ = tmpl .Execute (os .Stdout , data )
47
39
}
48
40
49
41
func serverVersion () string {
0 commit comments