-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_cli.go
More file actions
301 lines (270 loc) · 11.2 KB
/
Copy pathmain_cli.go
File metadata and controls
301 lines (270 loc) · 11.2 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package main
import (
cronjob "SettingsSentry/cron"
"SettingsSentry/interfaces"
"SettingsSentry/logger"
"SettingsSentry/pkg/backup"
"SettingsSentry/pkg/config"
"SettingsSentry/pkg/printer"
"SettingsSentry/pkg/util"
"embed"
"errors"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
)
// CLI handles command-line interface operations
type CLI struct {
logger *logger.Logger
fs interfaces.FileSystem
cmdExecutor interfaces.CommandExecutor
embeddedConfigs embed.FS
version string
envConfigFolder string
envBackupFolder string
envAppName string
envCommands bool
envDryRun bool
envZip bool
envPassword string
}
// NewCLI creates a new CLI instance
func NewCLI(logger *logger.Logger, fs interfaces.FileSystem, cmdExecutor interfaces.CommandExecutor, embeddedConfigs embed.FS, version string) *CLI {
return &CLI{
logger: logger,
fs: fs,
cmdExecutor: cmdExecutor,
embeddedConfigs: embeddedConfigs,
version: version,
}
}
// ParseFlags parses command-line arguments and environment variables
func (c *CLI) ParseFlags(args []string) (action string, flags map[string]interface{}, err error) {
if len(args) < 1 {
return "", nil, errors.New("no action specified")
}
// Get iCloud path for default backup location
icloudPath, err := config.GetICloudFolderLocation()
if err != nil {
c.logger.Logf("Error: iCloud path not found - %v", err)
icloudPath = ""
} else {
icloudPath = filepath.Join(icloudPath, "settingssentry_backups")
}
if icloudPath == "" {
homeDir, homeErr := config.GetHomeDirectory()
if homeErr == nil {
icloudPath = filepath.Join(homeDir, ".settingssentry_backups")
c.logger.Logf("Warning: iCloud path not found, using default backup path: %s", icloudPath)
} else {
return "", nil, errors.New("cannot determine iCloud or home directory for default backup path")
}
}
// Get environment variable defaults
c.envConfigFolder = getEnvWithDefault("SETTINGSSENTRY_CONFIG", "configs")
c.envBackupFolder = getEnvWithDefault("SETTINGSSENTRY_BACKUP", icloudPath)
c.envAppName = os.Getenv("SETTINGSSENTRY_APP")
c.envCommands = os.Getenv("SETTINGSSENTRY_COMMANDS") == "true"
c.envDryRun = os.Getenv("SETTINGSSENTRY_DRY_RUN") == "true"
c.envZip = os.Getenv("SETTINGSSENTRY_ZIP") == "true"
c.envPassword = os.Getenv("SETTINGSSENTRY_PASSWORD")
action = args[0]
// Validate action
if !isValidAction(action) {
return "", nil, fmt.Errorf("invalid action: %s", action)
}
// Define flags using a specific FlagSet for the action
actionFlags := flag.NewFlagSet(action, flag.ContinueOnError)
configFolder := actionFlags.String("config", c.envConfigFolder, "Path to the configuration folder (env: SETTINGSSENTRY_CONFIG)")
backupFolder := actionFlags.String("backup", c.envBackupFolder, "Path to the backup folder (env: SETTINGSSENTRY_BACKUP)")
appNameFlag := actionFlags.String("app", c.envAppName, "Optional: Comma-separated list of application names to process (env: SETTINGSSENTRY_APP)")
commands := actionFlags.Bool("allow-commands", c.envCommands, "Optional: Allow execution of pre-backup/restore commands from config files. SECURITY WARNING: Only enable for trusted configs! Commands execute with full user privileges. (env: SETTINGSSENTRY_COMMANDS)")
dryRunFlag := actionFlags.Bool("dry-run", c.envDryRun, "Optional: Perform a dry run without making any changes (env: SETTINGSSENTRY_DRY_RUN)")
versionsToKeep := actionFlags.Int("versions", 1, "Number of backup versions to keep")
password := actionFlags.String("password", c.envPassword, "Optional: Password to encrypt/decrypt backups (env: SETTINGSSENTRY_PASSWORD)")
zipFlag := actionFlags.Bool("zip", c.envZip, "Optional: Create backup as a zip archive instead of a directory (env: SETTINGSSENTRY_ZIP)")
logFilePath := actionFlags.String("logfile", "", "Optional: Path to log file.")
// Parse arguments starting from the one after the action
if err := actionFlags.Parse(args[1:]); err != nil {
return "", nil, fmt.Errorf("error parsing flags: %w", err)
}
// Validate versionsToKeep is non-negative
if *versionsToKeep < 0 {
return "", nil, fmt.Errorf("versions must be non-negative, got %d", *versionsToKeep)
}
// Split the appNameFlag string into a slice
var appNames []string
if *appNameFlag != "" {
appNames = strings.Split(*appNameFlag, ",")
// Trim whitespace from each app name and filter out empty strings
var filteredNames []string
for _, name := range appNames {
trimmed := strings.TrimSpace(name)
if trimmed != "" {
filteredNames = append(filteredNames, trimmed)
}
}
appNames = filteredNames
}
flags = map[string]interface{}{
"configFolder": *configFolder,
"backupFolder": *backupFolder,
"appNames": appNames,
"commands": *commands,
"dryRun": *dryRunFlag,
"versionsToKeep": *versionsToKeep,
"password": *password,
"zip": *zipFlag,
"logFilePath": *logFilePath,
"extraArgs": actionFlags.Args(),
}
return action, flags, nil
}
// ExecuteAction executes the specified action with the given flags
func (c *CLI) ExecuteAction(action string, flags map[string]interface{}) error {
switch action {
case "backup", "restore":
return c.executeBackupRestore(action, flags)
case "configsinit":
return c.executeConfigsInit()
case "install":
return c.executeInstall(flags)
case "remove":
return c.executeRemove()
default:
return fmt.Errorf("invalid action: %s", action)
}
}
// executeBackupRestore handles backup and restore actions
func (c *CLI) executeBackupRestore(action string, flags map[string]interface{}) error {
configFolder := flags["configFolder"].(string)
backupFolder := flags["backupFolder"].(string)
appNames := flags["appNames"].([]string)
commands := flags["commands"].(bool)
dryRun := flags["dryRun"].(bool)
versionsToKeep := flags["versionsToKeep"].(int)
zipFlag := flags["zip"].(bool)
password := flags["password"].(string)
util.DryRun = dryRun
backup.DryRun = dryRun
mainPrinter := printer.NewPrinter("", c.logger)
backup.Printer = mainPrinter
isBackup := action == "backup"
backup.ProcessConfiguration(configFolder, backupFolder, appNames, isBackup, commands, versionsToKeep, zipFlag, password)
return nil
}
// executeConfigsInit handles configsinit action
func (c *CLI) executeConfigsInit() error {
err := util.ExtractEmbeddedConfigs(c.embeddedConfigs)
if err != nil {
return fmt.Errorf("error extracting embedded configs: %w", err)
}
return nil
}
// executeInstall handles install action
func (c *CLI) executeInstall(flags map[string]interface{}) error {
cronExpression := ""
extraArgs := flags["extraArgs"].([]string)
if len(extraArgs) > 0 {
cronExpression = extraArgs[0]
}
// Check if --allow-commands flag is set
allowCommands := false
if val, ok := flags["commands"].(bool); ok {
allowCommands = val
}
err := cronjob.InstallCronJob(cronExpression, allowCommands)
if err != nil {
return fmt.Errorf("failed to install cron job: %w", err)
}
if allowCommands {
c.logger.Logf("CRON job installed successfully with --allow-commands enabled")
c.logger.Logf("WARNING: Pre/post backup commands will execute with full user privileges")
} else {
c.logger.Logf("CRON job installed successfully (commands disabled for security)")
}
return nil
}
// executeRemove handles remove action
func (c *CLI) executeRemove() error {
err := cronjob.RemoveCronJob()
if err != nil {
return fmt.Errorf("failed to remove cron job: %w", err)
}
c.logger.Logf("CRON job removed successfully")
return nil
}
// ShowHelp displays help information
func (c *CLI) ShowHelp() {
c.logger.Logf("Usage: settingssentry <action> [options]")
c.logger.Logf("")
c.logger.Logf("Actions:")
c.logger.Logf(" backup - Backup configuration files to the specified backup folder")
c.logger.Logf(" restore - Restore the files to their original locations")
c.logger.Logf(" configsinit - Extract embedded default configs to a 'configs' directory next to the executable")
c.logger.Logf(" install - Install the application as a CRON job that runs at every reboot")
c.logger.Logf(" You can provide a valid cron expression as parameter (e.g., '0 9 * * *')")
c.logger.Logf(" Use --allow-commands to enable command execution in scheduled backups")
c.logger.Logf(" remove - Remove the previously installed CRON job")
c.logger.Logf("")
c.logger.Logf("Options:")
c.logger.Logf(" -config=<path> Path to the configuration folder (default: %s)", c.envConfigFolder)
c.logger.Logf(" -backup=<path> Path to the backup folder (default: %s)", c.envBackupFolder)
c.logger.Logf(" -app=<app1,app2,...> Comma-separated list of application names to process")
c.logger.Logf(" -allow-commands [SECURITY WARNING] Allow execution of pre/post backup/restore commands")
c.logger.Logf(" Commands execute with full user privileges. Only enable for trusted configs!")
c.logger.Logf(" -dry-run Perform a dry run without making any changes")
c.logger.Logf(" -versions=<n> Number of backup versions to keep (default: 1, 0 = keep all)")
c.logger.Logf(" -zip Create backup as a zip archive instead of a directory")
c.logger.Logf(" -password=<pwd> Password to encrypt/decrypt backups (AES-256-GCM)")
c.logger.Logf(" -logfile=<path> Path to log file (logs to console + file if provided)")
c.logger.Logf("")
c.logger.Logf("Environment Variables:")
c.logger.Logf(" SETTINGSSENTRY_CONFIG Path to configuration folder")
c.logger.Logf(" SETTINGSSENTRY_BACKUP Path to backup folder")
c.logger.Logf(" SETTINGSSENTRY_APP Comma-separated app names")
c.logger.Logf(" SETTINGSSENTRY_COMMANDS Set to 'true' to allow command execution")
c.logger.Logf(" SETTINGSSENTRY_DRY_RUN Set to 'true' for dry-run mode")
c.logger.Logf(" SETTINGSSENTRY_ZIP Set to 'true' to create zip archives")
c.logger.Logf(" SETTINGSSENTRY_PASSWORD Password for encryption/decryption")
c.logger.Logf("")
c.logger.Logf("Examples:")
c.logger.Logf(" settingssentry backup")
c.logger.Logf(" settingssentry backup -dry-run")
c.logger.Logf(" settingssentry backup -app=Brew,Git -zip -password=mypass")
c.logger.Logf(" settingssentry restore -app=Brew")
c.logger.Logf(" settingssentry install --allow-commands")
c.logger.Logf(" settingssentry install '0 9 * * *' # Daily at 9 AM")
c.logger.Logf("")
c.logger.Logf("Documentation: https://github.com/sstraus/SettingsSentry")
c.logger.Logf("")
installed, err := cronjob.IsCronJobInstalled()
if err != nil {
c.logger.Logf("Error checking CRON job installation: %v", err)
return
}
if installed {
c.logger.Logf("Status: CRON job is currently installed - backups will run automatically")
}
}
// Helper functions
// isValidAction checks if the action is valid
func isValidAction(action string) bool {
validActions := []string{"backup", "restore", "configsinit", "install", "remove"}
for _, valid := range validActions {
if action == valid {
return true
}
}
return false
}
// getEnvWithDefault gets an environment variable with a default value
func getEnvWithDefault(key, defaultValue string) string {
value := os.Getenv(key)
if value == "" {
return defaultValue
}
return value
}