@@ -5,31 +5,39 @@ import (
55 "os"
66 "path/filepath"
77 "strings"
8+
9+ "github.com/mdgspace/sysreplicate/internal/domain"
810)
11+
912func (am * AutomationManager ) detectSystemDUnits () ([]SystemDUnit , []SystemDUnit , error ) {
1013 var services []SystemDUnit
1114 var timers []SystemDUnit
12-
13- systemdDir := "/etc/systemd/system"
14- if _ , err := os .Stat (systemdDir ); os .IsNotExist (err ) {
15- fmt .Printf ("SystemD directory %s does not exist, skipping SystemD detection\n " , systemdDir )
15+
16+ if _ , err := os .Stat (domain .SystemdDirPath ); os .IsNotExist (err ) {
17+ fmt .Printf ("SystemD directory %s does not exist, skipping SystemD detection\n " , domain .SystemdDirPath )
1618 return services , timers , nil
1719 }
18- err := filepath .Walk (systemdDir , func (path string , info os.FileInfo , err error ) error {
19- if err != nil {return err }
20- if info .IsDir () {return nil }
21- if ! am .isCustomSystemDUnit (path ) {return nil }
22-
20+ err := filepath .Walk (domain .SystemdDirPath , func (path string , info os.FileInfo , err error ) error {
21+ if err != nil {
22+ return err
23+ }
24+ if info .IsDir () {
25+ return nil
26+ }
27+ if ! am .isCustomSystemDUnit (path ) {
28+ return nil
29+ }
30+
2331 ext := filepath .Ext (path )
2432 unitName := filepath .Base (path )
25-
33+
2634 content , err := am .readFileContent (path )
2735 if err != nil {
2836 fmt .Printf ("Warning: Could not read SystemD unit %s: %v\n " , path , err )
2937 return nil
3038 }
3139 isEnabled , isActive := am .getSystemDUnitStatus (unitName )
32-
40+
3341 unit := SystemDUnit {
3442 Name : unitName ,
3543 Path : path ,
@@ -38,7 +46,7 @@ func (am *AutomationManager) detectSystemDUnits() ([]SystemDUnit, []SystemDUnit,
3846 IsEnabled : isEnabled ,
3947 IsActive : isActive ,
4048 }
41-
49+
4250 switch ext {
4351 case ".service" :
4452 services = append (services , unit )
@@ -47,23 +55,23 @@ func (am *AutomationManager) detectSystemDUnits() ([]SystemDUnit, []SystemDUnit,
4755 case ".target" :
4856 services = append (services , unit )
4957 }
50-
58+
5159 return nil
5260 })
53-
61+
5462 if err != nil {
5563 return nil , nil , fmt .Errorf ("failed to scan SystemD directory: %w" , err )
5664 }
57-
65+
5866 return services , timers , nil
5967}
6068
61- /////detectCronjobs scans for cron job files
69+ // ///detectCronjobs scans for cron job files
6270func (am * AutomationManager ) detectCronjobs () ([]Cronjob , []Cronjob , error ) {
6371 var userCronjobs []Cronjob
6472 var systemCronjobs []Cronjob
65-
66- userCronPath := fmt .Sprintf ("/var/spool/cron/crontabs/%s" , am .username )
73+
74+ userCronPath := fmt .Sprintf (domain . UserCronPathTemplatePath , am .username )
6775 if content , err := am .readFileContent (userCronPath ); err == nil {
6876 lines := strings .Split (content , "\n " )
6977 var filteredLines []string
@@ -73,7 +81,7 @@ func (am *AutomationManager) detectCronjobs() ([]Cronjob, []Cronjob, error) {
7381 filteredLines = append (filteredLines , line )
7482 }
7583 }
76-
84+
7785 if len (filteredLines ) > 0 {
7886 userCronjobs = append (userCronjobs , Cronjob {
7987 Path : userCronPath ,
@@ -82,13 +90,12 @@ func (am *AutomationManager) detectCronjobs() ([]Cronjob, []Cronjob, error) {
8290 })
8391 }
8492 }
85-
86-
93+
8794 systemCronPaths := []string {
88- "/etc/crontab" ,
95+ domain . SystemCrontabDefaultPath ,
8996 }
90-
91- if cronDDir := "/etc/cron.d" ; am .dirExists (cronDDir ) {
97+
98+ if cronDDir := domain . CronDDirPath ; am .dirExists (cronDDir ) {
9299 if files , err := filepath .Glob (filepath .Join (cronDDir , "*" )); err == nil {
93100 for _ , file := range files {
94101 if info , err := os .Stat (file ); err == nil && ! info .IsDir () {
@@ -97,7 +104,7 @@ func (am *AutomationManager) detectCronjobs() ([]Cronjob, []Cronjob, error) {
97104 }
98105 }
99106 }
100-
107+
101108 for _ , cronPath := range systemCronPaths {
102109 if content , err := am .readFileContent (cronPath ); err == nil {
103110 lines := strings .Split (content , "\n " )
@@ -108,13 +115,13 @@ func (am *AutomationManager) detectCronjobs() ([]Cronjob, []Cronjob, error) {
108115 filteredLines = append (filteredLines , line )
109116 }
110117 }
111-
118+
112119 if len (filteredLines ) > 0 {
113120 cronType := "system"
114121 if strings .Contains (cronPath , "/etc/cron.d/" ) {
115122 cronType = "cron_d"
116123 }
117-
124+
118125 systemCronjobs = append (systemCronjobs , Cronjob {
119126 Path : cronPath ,
120127 Content : content ,
@@ -123,7 +130,7 @@ func (am *AutomationManager) detectCronjobs() ([]Cronjob, []Cronjob, error) {
123130 }
124131 }
125132 }
126-
133+
127134 return userCronjobs , systemCronjobs , nil
128135}
129136
@@ -134,40 +141,40 @@ func (am *AutomationManager) dirExists(path string) bool {
134141
135142func (am * AutomationManager ) GetAutomationSummary (data * AutomationData ) string {
136143 var summary strings.Builder
137-
144+
138145 summary .WriteString ("Automation Detection Summary:\n " )
139146 summary .WriteString (fmt .Sprintf ("- SystemD Services: %d\n " , len (data .SystemDServices )))
140147 summary .WriteString (fmt .Sprintf ("- SystemD Timers: %d\n " , len (data .SystemDTimers )))
141148 summary .WriteString (fmt .Sprintf ("- User Cronjobs: %d\n " , len (data .UserCronjobs )))
142149 summary .WriteString (fmt .Sprintf ("- System Cronjobs: %d\n " , len (data .SystemCronjobs )))
143-
150+
144151 if len (data .SystemDServices ) > 0 {
145152 summary .WriteString ("\n SystemD Services found:\n " )
146153 for _ , service := range data .SystemDServices {
147154 summary .WriteString (fmt .Sprintf (" - %s (%s)\n " , service .Name , service .Path ))
148155 }
149156 }
150-
157+
151158 if len (data .SystemDTimers ) > 0 {
152159 summary .WriteString ("\n SystemD Timers found:\n " )
153160 for _ , timer := range data .SystemDTimers {
154161 summary .WriteString (fmt .Sprintf (" - %s (%s)\n " , timer .Name , timer .Path ))
155162 }
156163 }
157-
164+
158165 if len (data .UserCronjobs ) > 0 {
159166 summary .WriteString ("\n User Cronjobs found:\n " )
160167 for _ , cronjob := range data .UserCronjobs {
161168 summary .WriteString (fmt .Sprintf (" - %s\n " , cronjob .Path ))
162169 }
163170 }
164-
171+
165172 if len (data .SystemCronjobs ) > 0 {
166173 summary .WriteString ("\n System Cronjobs found:\n " )
167174 for _ , cronjob := range data .SystemCronjobs {
168175 summary .WriteString (fmt .Sprintf (" - %s\n " , cronjob .Path ))
169176 }
170177 }
171-
178+
172179 return summary .String ()
173180}
0 commit comments