-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevbackup.go
More file actions
81 lines (67 loc) · 1.68 KB
/
devbackup.go
File metadata and controls
81 lines (67 loc) · 1.68 KB
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
package devflow
import (
"fmt"
"os"
)
const (
backupEnvVar = "DEV_BACKUP"
)
// DevBackup handles backup operations
type DevBackup struct {
bashrc *Bashrc
log func(...any)
}
// NewDevBackup creates a new DevBackup instance
func NewDevBackup() *DevBackup {
return &DevBackup{
bashrc: NewBashrc(),
log: func(...any) {},
}
}
// SetLog sets the logger function
func (d *DevBackup) SetLog(fn func(...any)) {
if fn != nil {
d.log = fn
}
}
// SetCommand sets the backup command in .bashrc and current environment
func (d *DevBackup) SetCommand(command string) error {
// Save to .bashrc for persistence
if err := d.bashrc.Set(backupEnvVar, command); err != nil {
return err
}
// Update current process environment for immediate use
if command == "" {
os.Unsetenv(backupEnvVar)
} else {
os.Setenv(backupEnvVar, command)
}
return nil
}
// GetCommand retrieves the backup command
// First checks environment variable, then falls back to .bashrc
func (d *DevBackup) GetCommand() (string, error) {
// Try environment variable first (current session)
if envCmd := os.Getenv(backupEnvVar); envCmd != "" {
return envCmd, nil
}
// Fallback to .bashrc
return d.bashrc.Get(backupEnvVar)
}
// Run executes the backup command asynchronously
// Returns a message for the summary or empty string if not configured
func (d *DevBackup) Run() (string, error) {
command, err := d.GetCommand()
if err != nil {
// Not configured, silent skip
return "", nil
}
if command == "" {
return "", nil
}
// Execute asynchronously at OS level
if err := RunShellCommandAsync(command); err != nil {
return "", fmt.Errorf("failed to start backup: %w", err)
}
return "✅ Backup started", nil
}