|
| 1 | +package session |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "sort" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + "ccgo/internal/contracts" |
| 13 | + "ccgo/internal/platform" |
| 14 | +) |
| 15 | + |
| 16 | +type ScheduleState struct { |
| 17 | + ID string `json:"id"` |
| 18 | + SessionID contracts.ID `json:"sessionId,omitempty"` |
| 19 | + Description string `json:"description,omitempty"` |
| 20 | + Cron string `json:"cron,omitempty"` |
| 21 | + Message string `json:"message,omitempty"` |
| 22 | + TeamID string `json:"teamId,omitempty"` |
| 23 | + Target string `json:"target,omitempty"` |
| 24 | + Enabled bool `json:"enabled"` |
| 25 | + CreatedAt string `json:"createdAt,omitempty"` |
| 26 | + UpdatedAt string `json:"updatedAt,omitempty"` |
| 27 | +} |
| 28 | + |
| 29 | +type ScheduleManifest struct { |
| 30 | + SessionID contracts.ID `json:"sessionId,omitempty"` |
| 31 | + Schedules []ScheduleState `json:"schedules,omitempty"` |
| 32 | +} |
| 33 | + |
| 34 | +type ScheduleOptions struct { |
| 35 | + ID string |
| 36 | + Description string |
| 37 | + Cron string |
| 38 | + Message string |
| 39 | + TeamID string |
| 40 | + Target string |
| 41 | + Enabled bool |
| 42 | + Timestamp time.Time |
| 43 | +} |
| 44 | + |
| 45 | +func ScheduleManifestPath(sessionPath string, sessionID contracts.ID) string { |
| 46 | + if sessionPath == "" || sessionID == "" { |
| 47 | + return "" |
| 48 | + } |
| 49 | + return filepath.Join(filepath.Dir(sessionPath), string(sessionID), "schedules.json") |
| 50 | +} |
| 51 | + |
| 52 | +func LoadScheduleManifest(sessionPath string, sessionID contracts.ID) (ScheduleManifest, error) { |
| 53 | + path := ScheduleManifestPath(sessionPath, sessionID) |
| 54 | + if path == "" { |
| 55 | + return ScheduleManifest{}, os.ErrInvalid |
| 56 | + } |
| 57 | + data, err := os.ReadFile(path) |
| 58 | + if os.IsNotExist(err) { |
| 59 | + return ScheduleManifest{SessionID: sessionID}, nil |
| 60 | + } |
| 61 | + if err != nil { |
| 62 | + return ScheduleManifest{}, err |
| 63 | + } |
| 64 | + var manifest ScheduleManifest |
| 65 | + if err := json.Unmarshal(data, &manifest); err != nil { |
| 66 | + return ScheduleManifest{}, err |
| 67 | + } |
| 68 | + manifest.SessionID = sessionID |
| 69 | + for i := range manifest.Schedules { |
| 70 | + manifest.Schedules[i].SessionID = sessionID |
| 71 | + manifest.Schedules[i].ID = sanitizeSidechainID(manifest.Schedules[i].ID) |
| 72 | + manifest.Schedules[i].Description = strings.TrimSpace(manifest.Schedules[i].Description) |
| 73 | + manifest.Schedules[i].Cron = strings.TrimSpace(manifest.Schedules[i].Cron) |
| 74 | + manifest.Schedules[i].Message = strings.TrimSpace(manifest.Schedules[i].Message) |
| 75 | + manifest.Schedules[i].TeamID = sanitizeSidechainID(manifest.Schedules[i].TeamID) |
| 76 | + manifest.Schedules[i].Target = strings.TrimSpace(manifest.Schedules[i].Target) |
| 77 | + } |
| 78 | + sort.SliceStable(manifest.Schedules, func(i, j int) bool { |
| 79 | + return manifest.Schedules[i].ID < manifest.Schedules[j].ID |
| 80 | + }) |
| 81 | + return manifest, nil |
| 82 | +} |
| 83 | + |
| 84 | +func SaveScheduleManifest(sessionPath string, manifest ScheduleManifest) error { |
| 85 | + path := ScheduleManifestPath(sessionPath, manifest.SessionID) |
| 86 | + if path == "" { |
| 87 | + return os.ErrInvalid |
| 88 | + } |
| 89 | + data, err := json.MarshalIndent(manifest, "", " ") |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + data = append(data, '\n') |
| 94 | + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + return platform.AtomicWriteFile(path, data, 0o644) |
| 98 | +} |
| 99 | + |
| 100 | +func UpsertSchedule(sessionPath string, sessionID contracts.ID, options ScheduleOptions) (ScheduleState, ScheduleManifest, error) { |
| 101 | + manifest, err := LoadScheduleManifest(sessionPath, sessionID) |
| 102 | + if err != nil { |
| 103 | + return ScheduleState{}, ScheduleManifest{}, err |
| 104 | + } |
| 105 | + id := sanitizeSidechainID(options.ID) |
| 106 | + if id == "" { |
| 107 | + id = string(contracts.NewID()) |
| 108 | + } |
| 109 | + now := options.Timestamp |
| 110 | + if now.IsZero() { |
| 111 | + now = time.Now().UTC() |
| 112 | + } |
| 113 | + schedule := ScheduleState{ |
| 114 | + ID: id, |
| 115 | + SessionID: sessionID, |
| 116 | + Description: strings.TrimSpace(options.Description), |
| 117 | + Cron: strings.TrimSpace(options.Cron), |
| 118 | + Message: strings.TrimSpace(options.Message), |
| 119 | + TeamID: sanitizeSidechainID(options.TeamID), |
| 120 | + Target: strings.TrimSpace(options.Target), |
| 121 | + Enabled: options.Enabled, |
| 122 | + CreatedAt: now.UTC().Format(time.RFC3339Nano), |
| 123 | + UpdatedAt: now.UTC().Format(time.RFC3339Nano), |
| 124 | + } |
| 125 | + replaced := false |
| 126 | + for i := range manifest.Schedules { |
| 127 | + if manifest.Schedules[i].ID != id { |
| 128 | + continue |
| 129 | + } |
| 130 | + if manifest.Schedules[i].CreatedAt != "" { |
| 131 | + schedule.CreatedAt = manifest.Schedules[i].CreatedAt |
| 132 | + } |
| 133 | + manifest.Schedules[i] = schedule |
| 134 | + replaced = true |
| 135 | + break |
| 136 | + } |
| 137 | + if !replaced { |
| 138 | + manifest.Schedules = append(manifest.Schedules, schedule) |
| 139 | + } |
| 140 | + sort.SliceStable(manifest.Schedules, func(i, j int) bool { |
| 141 | + return manifest.Schedules[i].ID < manifest.Schedules[j].ID |
| 142 | + }) |
| 143 | + if err := SaveScheduleManifest(sessionPath, manifest); err != nil { |
| 144 | + return ScheduleState{}, ScheduleManifest{}, err |
| 145 | + } |
| 146 | + return schedule, manifest, nil |
| 147 | +} |
| 148 | + |
| 149 | +func DeleteSchedule(sessionPath string, sessionID contracts.ID, scheduleID string) (ScheduleState, ScheduleManifest, error) { |
| 150 | + manifest, err := LoadScheduleManifest(sessionPath, sessionID) |
| 151 | + if err != nil { |
| 152 | + return ScheduleState{}, ScheduleManifest{}, err |
| 153 | + } |
| 154 | + id := sanitizeSidechainID(scheduleID) |
| 155 | + if id == "" { |
| 156 | + return ScheduleState{}, ScheduleManifest{}, fmt.Errorf("schedule_id is required") |
| 157 | + } |
| 158 | + next := manifest.Schedules[:0] |
| 159 | + var deleted ScheduleState |
| 160 | + found := false |
| 161 | + for _, schedule := range manifest.Schedules { |
| 162 | + if schedule.ID == id { |
| 163 | + deleted = schedule |
| 164 | + found = true |
| 165 | + continue |
| 166 | + } |
| 167 | + next = append(next, schedule) |
| 168 | + } |
| 169 | + if !found { |
| 170 | + return ScheduleState{}, ScheduleManifest{}, fmt.Errorf("schedule not found: %s", id) |
| 171 | + } |
| 172 | + manifest.Schedules = next |
| 173 | + if err := SaveScheduleManifest(sessionPath, manifest); err != nil { |
| 174 | + return ScheduleState{}, ScheduleManifest{}, err |
| 175 | + } |
| 176 | + return deleted, manifest, nil |
| 177 | +} |
0 commit comments