Skip to content

Commit ea6121a

Browse files
committed
chore: normalize line endings
1 parent a5e4898 commit ea6121a

15 files changed

+1018
-1017
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

main.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
package main
2-
3-
import (
4-
"github.com/mdgspace/sysreplicate/system"
5-
)
6-
// main is the entry point for the program.
7-
func main() {
8-
system.Run()
9-
}
1+
package main
2+
3+
import (
4+
"github.com/mdgspace/sysreplicate/system"
5+
)
6+
// main is the entry point for the program.
7+
func main() {
8+
system.Run()
9+
}

system/backup/dotfile_scanner.go

Lines changed: 88 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,88 @@
1-
package backup
2-
3-
import (
4-
"os"
5-
"path/filepath"
6-
"strings"
7-
)
8-
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-
}
20-
21-
type Dotfile struct {
22-
Path string
23-
RelPath string
24-
IsDir bool
25-
IsBinary bool
26-
Mode os.FileMode
27-
Content string // ignore for the binary files
28-
}
29-
30-
// expand ~ to home dir
31-
func expandHome(path string) string {
32-
if strings.HasPrefix(path, "~") {
33-
home, err := os.UserHomeDir()
34-
if err == nil {
35-
return filepath.Join(home, path[2:])
36-
}
37-
}
38-
return path
39-
}
40-
41-
// check for binary files
42-
func containsNullByte(data []byte) bool {
43-
for _, b := range data {
44-
if b == 0 {
45-
return true
46-
}
47-
}
48-
return false
49-
}
50-
51-
// ScanDotfiles scans all dotfiles and returns their metadata + content
52-
func ScanDotfiles() ([]Dotfile, error) {
53-
var results []Dotfile
54-
home, _ := os.UserHomeDir()
55-
56-
for _, raw := range DotfilePaths {
57-
full := expandHome(raw)
58-
59-
info, err := os.Stat(full)
60-
if err != nil {
61-
continue
62-
}
63-
64-
relPath, _ := filepath.Rel(home, full)
65-
entry := Dotfile{
66-
Path: full,
67-
RelPath: relPath,
68-
IsDir: info.IsDir(),
69-
Mode: info.Mode(),
70-
}
71-
72-
if !info.IsDir() {
73-
data, err := os.ReadFile(full)
74-
if err != nil {
75-
continue
76-
}
77-
if containsNullByte(data) {
78-
entry.IsBinary = true
79-
} else {
80-
entry.Content = string(data)
81-
}
82-
}
83-
84-
results = append(results, entry)
85-
}
86-
87-
return results, nil
88-
}
1+
package backup
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"strings"
7+
)
8+
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+
}
20+
21+
type Dotfile struct {
22+
Path string
23+
RelPath string
24+
IsDir bool
25+
IsBinary bool
26+
Mode os.FileMode
27+
Content string // ignore for the binary files
28+
}
29+
30+
// expand ~ to home dir
31+
func expandHome(path string) string {
32+
if strings.HasPrefix(path, "~") {
33+
home, err := os.UserHomeDir()
34+
if err == nil {
35+
return filepath.Join(home, path[2:])
36+
}
37+
}
38+
return path
39+
}
40+
41+
// check for binary files
42+
func containsNullByte(data []byte) bool {
43+
for _, b := range data {
44+
if b == 0 {
45+
return true
46+
}
47+
}
48+
return false
49+
}
50+
51+
// ScanDotfiles scans all dotfiles and returns their metadata + content
52+
func ScanDotfiles() ([]Dotfile, error) {
53+
var results []Dotfile
54+
home, _ := os.UserHomeDir()
55+
56+
for _, raw := range DotfilePaths {
57+
full := expandHome(raw)
58+
59+
info, err := os.Stat(full)
60+
if err != nil {
61+
continue
62+
}
63+
64+
relPath, _ := filepath.Rel(home, full)
65+
entry := Dotfile{
66+
Path: full,
67+
RelPath: relPath,
68+
IsDir: info.IsDir(),
69+
Mode: info.Mode(),
70+
}
71+
72+
if !info.IsDir() {
73+
data, err := os.ReadFile(full)
74+
if err != nil {
75+
continue
76+
}
77+
if containsNullByte(data) {
78+
entry.IsBinary = true
79+
} else {
80+
entry.Content = string(data)
81+
}
82+
}
83+
84+
results = append(results, entry)
85+
}
86+
87+
return results, nil
88+
}

system/backup/dotfiles_backup.go

Lines changed: 79 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,79 @@
1-
package backup
2-
3-
import (
4-
"archive/tar"
5-
"compress/gzip"
6-
"encoding/json"
7-
"fmt"
8-
"io"
9-
"os"
10-
"time"
11-
)
12-
13-
type BackupMetadata struct {
14-
Timestamp time.Time `json:"timestamp"`
15-
Hostname string `json:"hostname"`
16-
Files []Dotfile `json:"files"`
17-
}
18-
19-
type DotfileBackupManager struct{}
20-
21-
func NewDotfileBackupManager() *DotfileBackupManager {
22-
return &DotfileBackupManager{}
23-
}
24-
25-
func (db *DotfileBackupManager) CreateDotfileBackup(outputTar string) error {
26-
files, err := ScanDotfiles()
27-
if err != nil {
28-
return fmt.Errorf("error scanning dotfiles: %w", err)
29-
}
30-
31-
hostname, _ := os.Hostname()
32-
33-
meta := BackupMetadata{
34-
Timestamp: time.Now(),
35-
Hostname: hostname,
36-
Files: files,
37-
}
38-
39-
tarFile, err := os.Create(outputTar)
40-
if err != nil {
41-
return fmt.Errorf("failed to create tar file: %w", err)
42-
}
43-
defer tarFile.Close()
44-
45-
gzipWriter := gzip.NewWriter(tarFile)
46-
defer gzipWriter.Close()
47-
tarWriter := tar.NewWriter(gzipWriter)
48-
defer tarWriter.Close()
49-
50-
// Write metadata JSON
51-
metaBytes, _ := json.MarshalIndent(meta, "", " ")
52-
tarWriter.WriteHeader(&tar.Header{
53-
Name: "backup.json",
54-
Mode: 0644,
55-
Size: int64(len(metaBytes)),
56-
})
57-
tarWriter.Write(metaBytes)
58-
59-
// Add dotfiles
60-
for _, f := range files {
61-
if f.IsDir {
62-
continue
63-
}
64-
file, err := os.Open(f.Path)
65-
if err != nil {
66-
continue
67-
}
68-
defer file.Close()
69-
70-
info, _ := file.Stat()
71-
hdr, _ := tar.FileInfoHeader(info, "")
72-
hdr.Name = f.RelPath
73-
tarWriter.WriteHeader(hdr)
74-
io.Copy(tarWriter, file)
75-
}
76-
77-
fmt.Println("Backup complete:", outputTar)
78-
return nil
79-
}
1+
package backup
2+
3+
import (
4+
"archive/tar"
5+
"compress/gzip"
6+
"encoding/json"
7+
"fmt"
8+
"io"
9+
"os"
10+
"time"
11+
)
12+
13+
type BackupMetadata struct {
14+
Timestamp time.Time `json:"timestamp"`
15+
Hostname string `json:"hostname"`
16+
Files []Dotfile `json:"files"`
17+
}
18+
19+
type DotfileBackupManager struct{}
20+
21+
func NewDotfileBackupManager() *DotfileBackupManager {
22+
return &DotfileBackupManager{}
23+
}
24+
25+
func (db *DotfileBackupManager) CreateDotfileBackup(outputTar string) error {
26+
files, err := ScanDotfiles()
27+
if err != nil {
28+
return fmt.Errorf("error scanning dotfiles: %w", err)
29+
}
30+
31+
hostname, _ := os.Hostname()
32+
33+
meta := BackupMetadata{
34+
Timestamp: time.Now(),
35+
Hostname: hostname,
36+
Files: files,
37+
}
38+
39+
tarFile, err := os.Create(outputTar)
40+
if err != nil {
41+
return fmt.Errorf("failed to create tar file: %w", err)
42+
}
43+
defer tarFile.Close()
44+
45+
gzipWriter := gzip.NewWriter(tarFile)
46+
defer gzipWriter.Close()
47+
tarWriter := tar.NewWriter(gzipWriter)
48+
defer tarWriter.Close()
49+
50+
// Write metadata JSON
51+
metaBytes, _ := json.MarshalIndent(meta, "", " ")
52+
tarWriter.WriteHeader(&tar.Header{
53+
Name: "backup.json",
54+
Mode: 0644,
55+
Size: int64(len(metaBytes)),
56+
})
57+
tarWriter.Write(metaBytes)
58+
59+
// Add dotfiles
60+
for _, f := range files {
61+
if f.IsDir {
62+
continue
63+
}
64+
file, err := os.Open(f.Path)
65+
if err != nil {
66+
continue
67+
}
68+
defer file.Close()
69+
70+
info, _ := file.Stat()
71+
hdr, _ := tar.FileInfoHeader(info, "")
72+
hdr.Name = f.RelPath
73+
tarWriter.WriteHeader(hdr)
74+
io.Copy(tarWriter, file)
75+
}
76+
77+
fmt.Println("Backup complete:", outputTar)
78+
return nil
79+
}

0 commit comments

Comments
 (0)