-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_trove.go
93 lines (75 loc) · 1.83 KB
/
file_trove.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"context"
"log"
"os"
"os/exec"
"runtime"
"sync"
)
var UseColor = true
type Storer interface {
Backup(context.Context) error
}
type FileWorker struct {
Config Config
}
func NewFileWorker(config Config) Storer {
return &FileWorker{
Config: config,
}
}
func main() {
config, err := ParseConfig()
if err != nil {
log.Fatalf(Colorize(ColorRed, "Error parsing configuration: %v"), err)
}
if config.Detach {
if os.Getenv("IS_CHILD") != "true" {
cmd := exec.Command(os.Args[0], os.Args[1:]...)
cmd.Env = append(os.Environ(), "IS_CHILD=true")
nullDevice := "NUL"
if runtime.GOOS != "windows" {
nullDevice = "/dev/null"
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin, _ = os.Open(nullDevice)
if err := cmd.Start(); err != nil {
log.Fatalf(Colorize(ColorRed, "Failed to start detached process: %v"), err)
}
os.Exit(0)
}
UseColor = false
logFile, err := os.OpenFile(config.LogFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Fatalf(Colorize(ColorRed, "Failed to open log file: %v"), err)
}
defer logFile.Close()
log.SetOutput(logFile)
log.Println("Logging to file:", config.LogFilePath)
} else {
log.Println("Logging to terminal")
}
storer := NewFileWorker(*config)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var wg sync.WaitGroup
switch config.Operation {
case OperationBackup:
wg.Add(1)
go func() {
defer wg.Done()
if err := storer.Backup(ctx); err != nil {
log.Printf(Colorize(ColorRed, "Backup failed: %v"), err)
} else {
log.Println(Colorize(ColorGreen, "Backup completed successfully"))
}
}()
// case OperationRestore:
// if err := storer.Restore(); err != nil {
// log.Fatalf(Colorize(ColorRed, "Restore failed: %v", err))
// }
}
wg.Wait()
}