@@ -15,7 +15,14 @@ import (
1515 "github.com/RewstApp/agent-smith-go/internal/utils"
1616)
1717
18- func runSystemCtl (args ... string ) error {
18+ type systemCtl interface {
19+ Run (args ... string ) error
20+ ServiceConfigFilePath (name string ) string
21+ }
22+
23+ type defaultSystemCtl struct {}
24+
25+ func (s * defaultSystemCtl ) Run (args ... string ) error {
1926 cmd := exec .Command ("systemctl" , args ... )
2027 out , err := cmd .CombinedOutput ()
2128 if err != nil {
@@ -25,82 +32,88 @@ func runSystemCtl(args ...string) error {
2532 return nil
2633}
2734
35+ func (s * defaultSystemCtl ) ServiceConfigFilePath (name string ) string {
36+ return filepath .Join ("/etc/systemd/system" , fmt .Sprintf ("%s.service" , name ))
37+ }
38+
2839type linuxService struct {
29- name string
40+ name string
41+ system systemCtl
3042}
3143
3244func (linuxSvc * linuxService ) Close () error {
3345 return nil
3446}
3547
3648func (linuxSvc * linuxService ) Start () error {
37- return runSystemCtl ("start" , linuxSvc .name )
49+ return linuxSvc . system . Run ("start" , linuxSvc .name )
3850}
3951
4052func (linuxSvc * linuxService ) Stop () error {
41- return runSystemCtl ("stop" , linuxSvc .name )
53+ return linuxSvc . system . Run ("stop" , linuxSvc .name )
4254}
4355
4456func (linuxSvc * linuxService ) Delete () error {
45- err := runSystemCtl ("disable" , linuxSvc .name )
57+ err := linuxSvc . system . Run ("disable" , linuxSvc .name )
4658 if err != nil {
4759 return err
4860 }
4961
5062 // Delete the service configuration file
51- serviceConfigFilePath := filepath .Join ("/etc/systemd/system" , fmt .Sprintf ("%s.service" , linuxSvc .name ))
52- return os .Remove (serviceConfigFilePath )
63+ return os .Remove (linuxSvc .system .ServiceConfigFilePath (linuxSvc .name ))
5364}
5465
5566func (linuxSvc * linuxService ) IsActive () bool {
56- return runSystemCtl ("is-active" , linuxSvc .name ) == nil
67+ return linuxSvc . system . Run ("is-active" , linuxSvc .name ) == nil
5768}
5869
5970func Create (params AgentParams ) (Service , error ) {
60- serviceConfig := strings.Builder {}
61-
62- serviceConfig .WriteString ("[Unit]\n " )
63- serviceConfig .WriteString (fmt .Sprintf ("Description=%s\n " , params .Name ))
64- serviceConfig .WriteString ("\n " )
71+ return createWithSystemCtl (params , & defaultSystemCtl {})
72+ }
6573
66- serviceConfig .WriteString ("[Service]\n " )
67- serviceConfig .WriteString (fmt .Sprintf ("ExecStart=%s --org-id %s --config-file %s --log-file %s\n " ,
68- params .AgentExecutablePath , params .OrgId , params .ConfigFilePath , params .LogFilePath ))
69- serviceConfig .WriteString ("Restart=always\n " )
70- serviceConfig .WriteString ("\n " )
74+ func createWithSystemCtl (params AgentParams , system systemCtl ) (Service , error ) {
75+ serviceConfig := strings.Builder {}
7176
72- serviceConfig .WriteString ("[Install]\n " )
73- serviceConfig .WriteString ("WantedBy=multi-user.target\n " )
77+ fmt .Fprintf (& serviceConfig , "[Unit]\n Description=%s\n \n " , params .Name )
78+ fmt .Fprintf (& serviceConfig , "[Service]\n ExecStart=%s --org-id %s --config-file %s --log-file %s\n Restart=always\n \n " ,
79+ params .AgentExecutablePath , params .OrgId , params .ConfigFilePath , params .LogFilePath )
80+ fmt .Fprintf (& serviceConfig , "[Install]\n WantedBy=multi-user.target\n " )
7481
75- serviceConfigFilePath := filepath . Join ( "/etc/systemd/ system" , fmt . Sprintf ( "%s.service" , params .Name ) )
82+ serviceConfigFilePath := system . ServiceConfigFilePath ( params .Name )
7683 err := os .WriteFile (serviceConfigFilePath , []byte (serviceConfig .String ()), utils .DefaultFileMod )
7784 if err != nil {
7885 return nil , err
7986 }
8087
81- err = runSystemCtl ("daemon-reload" )
88+ err = system . Run ("daemon-reload" )
8289 if err != nil {
8390 return nil , err
8491 }
8592
86- err = runSystemCtl ("enable" , params .Name )
93+ err = system . Run ("enable" , params .Name )
8794 if err != nil {
8895 return nil , err
8996 }
9097
9198 return & linuxService {
92- name : params .Name ,
99+ name : params .Name ,
100+ system : system ,
93101 }, nil
94102}
95103
96104func Open (name string ) (Service , error ) {
97- err := runSystemCtl ("status" , name )
105+ return openWithSystemCtl (name , & defaultSystemCtl {})
106+ }
107+
108+ func openWithSystemCtl (name string , system systemCtl ) (Service , error ) {
109+ err := system .Run ("status" , name )
98110 if err != nil {
99111 return nil , err
100112 }
101113
102114 return & linuxService {
103- name : name ,
115+ name : name ,
116+ system : system ,
104117 }, nil
105118}
106119
0 commit comments