-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_watch.go
More file actions
119 lines (103 loc) · 2.93 KB
/
cmd_watch.go
File metadata and controls
119 lines (103 loc) · 2.93 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
package main
import (
"context"
"errors"
"fmt"
"log"
"time"
"github.com/urfave/cli/v3"
)
const (
minInterval = 1 * time.Hour
maxInterval = 168 * time.Hour // 7 days
defaultInterval = 24 * time.Hour
)
func newWatchCommand() *cli.Command {
watchFlags := append([]cli.Flag{
&cli.DurationFlag{
Name: "interval",
Aliases: []string{"i"},
Usage: "Sync interval (1h-168h, required via --interval or config)",
},
&cli.BoolFlag{
Name: "once",
Usage: "Sync immediately then start watching",
},
}, syncFlags...)
return &cli.Command{
Name: "watch",
Usage: "Run sync on interval (Docker-friendly)",
Flags: watchFlags,
Action: runWatch,
}
}
func runWatch(ctx context.Context, cmd *cli.Command) error {
// Set package-level vars for compatibility with existing code
verboseVal, reverseVal := getSyncFlagsFromCmd(cmd)
// Load config for compatibility with sync
configPath := cmd.String("config")
config, err := loadConfigFromFile(configPath)
if err != nil {
return fmt.Errorf("error loading config: %w", err)
}
applySyncFlagsToConfig(cmd, &config)
// Initialize logger and add to context
logger := NewLogger(verboseVal)
ctx = logger.WithContext(ctx)
// Create app for compatibility
app, err := NewApp(ctx, config, reverseVal)
if err != nil {
return fmt.Errorf("create app: %w", err)
}
interval := cmd.Duration("interval")
// Priority: CLI flag > Config > Error
if interval == 0 {
cfgInterval, err := config.Watch.GetInterval()
if err != nil {
return fmt.Errorf("invalid interval in config: %w", err)
}
if cfgInterval == 0 {
return errors.New("interval required (use --interval or set watch.interval in config)")
}
interval = cfgInterval
}
// Validate interval
if interval < minInterval {
return fmt.Errorf("interval must be at least 1h (got %v)", interval)
}
if interval > maxInterval {
return fmt.Errorf("interval must be at most 168h/7days (got %v)", interval)
}
// Optional immediate sync
if cmd.Bool("once") {
log.Printf("Running initial sync (--once flag set)...")
err := app.Run(ctx)
if err != nil {
return fmt.Errorf("initial sync failed: %w", err)
}
nextTime := time.Now().Add(interval)
log.Printf("Initial sync completed, starting watch mode - next sync in %v at %s", interval, nextTime.Format("2006-01-02 15:04:05"))
} else {
nextTime := time.Now().Add(interval)
log.Printf("Starting watch mode: next sync in %v at %s", interval, nextTime.Format("2006-01-02 15:04:05"))
}
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
log.Printf("Running scheduled sync...")
app.Refresh(ctx)
err := app.Run(ctx)
if err != nil {
log.Printf("Sync error: %v", err)
} else {
nextTime := time.Now().Add(interval)
log.Printf("Sync completed - next sync in %v at %s", interval, nextTime.Format("2006-01-02 15:04:05"))
}
case <-ctx.Done():
log.Printf("Watch mode stopped")
return nil
}
}
}