Skip to content

Commit 0dea966

Browse files
committed
chore: Move all constants to internal/domain and delete redundant settings.go
1 parent 693682d commit 0dea966

File tree

14 files changed

+194
-209
lines changed

14 files changed

+194
-209
lines changed

internal/domain/constants.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package domain
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
const (
9+
OutputSysDirPath = "dist/sys-info"
10+
OutputScriptsDirPath = "dist"
11+
JsonOutputPath = OutputSysDirPath + "/package.json"
12+
ScriptOutputPath = OutputScriptsDirPath + "/setup.sh"
13+
DotfileOutputPath = "dist/dotfile-backup.tar.gz"
14+
OsReleasePath = "/etc/os-release"
15+
RestoreScriptPath = "dist/restored_packages_install.sh"
16+
SystemdDirPath = "/etc/systemd/system"
17+
UserCronPathTemplatePath = "/var/spool/cron/crontabs/%s"
18+
SystemCrontabDefaultPath = "/etc/crontab"
19+
CronDDirPath = "/etc/cron.d"
20+
21+
DebianInstallCmd = "sudo apt-get install -y"
22+
ArchInstallCmd = "sudo pacman -S --noconfirm"
23+
RhelInstallCmd = "sudo dnf install -y"
24+
VoidInstallCmd = "sudo xbps-install -y"
25+
26+
DebianFetchCmd = `dpkg-query -W -f='${Package}\n' | sort > /tmp/all.txt
27+
apt-mark showmanual | sort > /tmp/manual.txt
28+
comm -12 /tmp/all.txt /tmp/manual.txt | xargs -r dpkg-query -W -f='${Package}=${Version}\n'
29+
rm /tmp/all.txt /tmp/manual.txt`
30+
ArchOfficialFetchCmd = `pacman -Qen | cut -d' ' -f1`
31+
ArchYayFetchCmd = `pacman -Qem | cut -d' ' -f1`
32+
RhelFetchCmd = "rpm -qa"
33+
VoidFetchCmd = "xbps-query -l"
34+
35+
FlatpakFetchCmd = "flatpak list --app --columns=origin,application"
36+
SnapFetchCmd = "snap list | awk 'NR>1 {print $1}'"
37+
)
38+
39+
var DotfilePaths = []string{
40+
"~/.bashrc",
41+
"~/.zshrc",
42+
"~/.vimrc",
43+
"~/.config",
44+
"~/.bash_history",
45+
"~/.zsh_history",
46+
"~/.gitconfig",
47+
"~/.profile",
48+
"~/.npmrc",
49+
}
50+
51+
var StandardKeyLocations = []string{
52+
"~/.ssh/",
53+
"~/.gnupg/",
54+
}
55+
56+
var SshPatterns = []string{
57+
"id_rsa", "id_dsa", "id_ecdsa", "id_ed25519",
58+
"authorized_keys", "known_hosts", "config",
59+
}
60+
61+
var GpgPatterns = []string{
62+
"pubring.gpg", "secring.gpg", "trustdb.gpg",
63+
"gpg.conf", "gpg-agent.conf",
64+
}
65+
66+
var PackageManagedDirs = []string{
67+
"/usr/lib/systemd/system/",
68+
"/lib/systemd/system/",
69+
"/usr/share/systemd/",
70+
}
71+
72+
var UnifiedTarballPath = fmt.Sprintf("dist/unified-backup-%s.tar.gz",
73+
time.Now().Format("2006-01-02-15-04-05"))

system/automation/automation.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"fmt"
55
"os"
66
"strings"
7+
8+
"github.com/mdgspace/sysreplicate/internal/domain"
79
)
810

911
type AutomationData struct {
@@ -125,14 +127,8 @@ func (am *AutomationManager) isCustomSystemDUnit(filePath string) bool {
125127
if err != nil {
126128
return false
127129
}
128-
// Package-managed directories that we want to exclude
129-
packageManagedDirs := []string{
130-
"/usr/lib/systemd/system/",
131-
"/lib/systemd/system/",
132-
"/usr/share/systemd/",
133-
}
134130

135-
for _, dir := range packageManagedDirs {
131+
for _, dir := range domain.PackageManagedDirs {
136132
if strings.HasPrefix(target, dir) {
137133
return false
138134
}

system/automation/detect.go

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,39 @@ import (
55
"os"
66
"path/filepath"
77
"strings"
8+
9+
"github.com/mdgspace/sysreplicate/internal/domain"
810
)
11+
912
func (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
6270
func (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

135142
func (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("\nSystemD 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("\nSystemD 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("\nUser 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("\nSystem 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
}

system/backup/dotfile_scanner.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,9 @@ import (
44
"os"
55
"path/filepath"
66
"strings"
7-
)
87

9-
var DotfilePaths = []string{
10-
"~/.bashrc",
11-
"~/.zshrc",
12-
"~/.vimrc",
13-
"~/.config",
14-
"~/.bash_history",
15-
"~/.zsh_history",
16-
"~/.gitconfig",
17-
"~/.profile",
18-
"~/.npmrc",
19-
}
8+
"github.com/mdgspace/sysreplicate/internal/domain"
9+
)
2010

2111
type Dotfile struct {
2212
Path string
@@ -53,7 +43,7 @@ func ScanDotfiles() ([]Dotfile, error) {
5343
var results []Dotfile
5444
home, _ := os.UserHomeDir()
5545

56-
for _, raw := range DotfilePaths {
46+
for _, raw := range domain.DotfilePaths {
5747
full := expandHome(raw)
5848

5949
info, err := os.Stat(full)

system/backup/restore.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"path/filepath"
1414
"strings"
1515

16+
"github.com/mdgspace/sysreplicate/internal/domain"
1617
"github.com/mdgspace/sysreplicate/system/output"
1718
)
1819

@@ -253,7 +254,7 @@ func (rm *RestoreManager) restoreDotfiles(tarballPath string) error {
253254

254255
// generateInstallScript creates a script to reinstall packages
255256
func (rm *RestoreManager) generateInstallScript() error {
256-
scriptPath := "dist/restored_packages_install.sh"
257+
scriptPath := domain.RestoreScriptPath
257258

258259
// dir check
259260
if err := os.MkdirAll(filepath.Dir(scriptPath), 0755); err != nil {

system/backup/search.go

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,9 @@ import (
44
"os"
55
"path/filepath"
66
"strings"
7-
)
87

9-
// common location of keys - more can be added here
10-
var StandardKeyLocations = []string{
11-
"~/.ssh/",
12-
"~/.gnupg/",
13-
}
8+
"github.com/mdgspace/sysreplicate/internal/domain"
9+
)
1410

1511
// any saved keylcoation
1612
type KeyLocation struct {
@@ -31,7 +27,7 @@ func searchStandardLocations() ([]KeyLocation, error) {
3127
return nil, err
3228
}
3329

34-
for _, location := range StandardKeyLocations {
30+
for _, location := range domain.StandardKeyLocations {
3531
// reeplace ~operator with actual home directory
3632
fullPath := strings.Replace(location, "~", homeDir, 1)
3733

@@ -95,27 +91,15 @@ func discoverKeyFiles(dirPath string) ([]string, error) {
9591
func isKeyFile(path string, info os.FileInfo) bool {
9692
name := info.Name()
9793

98-
// SSH key patterns
99-
sshPatterns := []string{
100-
"id_rsa", "id_dsa", "id_ecdsa", "id_ed25519",
101-
"authorized_keys", "known_hosts", "config",
102-
}
103-
104-
// GPG key patterns
105-
gpgPatterns := []string{
106-
"pubring.gpg", "secring.gpg", "trustdb.gpg",
107-
"gpg.conf", "gpg-agent.conf",
108-
}
109-
11094
// check SSH
111-
for _, pattern := range sshPatterns {
95+
for _, pattern := range domain.SshPatterns {
11296
if strings.Contains(name, pattern) {
11397
return true
11498
}
11599
}
116100

117101
// cgeck GPG
118-
for _, pattern := range gpgPatterns {
102+
for _, pattern := range domain.GpgPatterns {
119103
if strings.Contains(name, pattern) {
120104
return true
121105
}

0 commit comments

Comments
 (0)