P1.4: one-time YAML import in storage

When gosentry.json / jobs.json are absent, read a pre-migration
gosentry.yaml / jobs.yaml via private yaml-tagged shadow structs and
return the data unsaved; the existing SaveConfig/SaveJobs in OpenStore
then rewrite it as JSON. Replaces the placeholder that pointed configPath
at the legacy YAML file and tried to json.Unmarshal it.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
mixeme
2026-06-22 21:40:00 +03:00
parent 8df248a1b2
commit d5418efe37
2 changed files with 105 additions and 22 deletions
+1 -1
View File
@@ -83,7 +83,7 @@ These land together because both edit `domain/job.go` and `storage/store.go`.
- [x] P1.1 — JSON struct tags - [x] P1.1 — JSON struct tags
- [x] P1.2 — `writeJSON` + JSON unmarshal - [x] P1.2 — `writeJSON` + JSON unmarshal
- [x] P1.3 — `gosentry.json` / `jobs.json` paths; drop pysentry name - [x] P1.3 — `gosentry.json` / `jobs.json` paths; drop pysentry name
- [ ] P1.4 — One-time YAML import - [x] P1.4 — One-time YAML import
- [ ] P1.5 — Remove `SuccessExitCodes` across code - [ ] P1.5 — Remove `SuccessExitCodes` across code
- [ ] P1.6 — Update storage/runner/format tests + TESTS.md - [ ] P1.6 — Update storage/runner/format tests + TESTS.md
+104 -21
View File
@@ -9,6 +9,7 @@ import (
"strings" "strings"
"gitea.mixdep.ru/mix/gosentry/src/domain" "gitea.mixdep.ru/mix/gosentry/src/domain"
"go.yaml.in/yaml/v4"
) )
type Store struct { type Store struct {
@@ -16,6 +17,38 @@ type Store struct {
Config domain.Config Config domain.Config
} }
// yamlConfig and yamlJob / yamlJobsFile mirror the durable domain types using the
// yaml tags that pre-JSON-migration files carried. They exist only so the
// one-time import can parse a legacy gosentry.yaml / jobs.yaml; the domain types
// themselves stay JSON-only. Field layout must stay identical to the matching
// domain struct so the value conversions in importYAMLConfig / importYAMLJobs
// remain valid.
type yamlConfig struct {
JobsDir string `yaml:"jobs_dir"`
LogsDir string `yaml:"logs_dir"`
MaxLogFiles int `yaml:"max_log_files"`
MaxLogAgeDays int `yaml:"max_log_age_days"`
StartOnLogin bool `yaml:"start_on_login,omitempty"`
KeepRunningInTray bool `yaml:"keep_running_in_tray,omitempty"`
NotifyOnFailure bool `yaml:"notify_on_failure,omitempty"`
}
type yamlJob struct {
ID int `yaml:"id"`
Name string `yaml:"name"`
Folder string `yaml:"folder,omitempty"`
Schedule string `yaml:"schedule"`
Command string `yaml:"command"`
Arguments string `yaml:"arguments,omitempty"`
SuccessExitCodes string `yaml:"success_exit_codes,omitempty"`
StartOnly bool `yaml:"start_only,omitempty"`
Enabled bool `yaml:"enabled"`
}
type yamlJobsFile struct {
Jobs []yamlJob `yaml:"jobs"`
}
func OpenStore() (*Store, []domain.Job, error) { func OpenStore() (*Store, []domain.Job, error) {
paths, err := ResolvePaths() paths, err := ResolvePaths()
if err != nil { if err != nil {
@@ -78,30 +111,29 @@ func loadOrCreateConfig(paths Paths) (domain.Config, error) {
NotifyOnFailure: true, NotifyOnFailure: true,
} }
configPath := paths.ConfigPath if _, err := os.Stat(paths.ConfigPath); errors.Is(err, os.ErrNotExist) {
if _, err := os.Stat(configPath); errors.Is(err, os.ErrNotExist) { // No JSON config yet. Import a pre-migration gosentry.yaml once if it is
// present; otherwise write the defaults so later starts read a normal JSON
// file. The caller's SaveConfig rewrites whatever is loaded as gosentry.json.
legacyPath := filepath.Join(paths.AppDir, legacyYAMLConfigFileName) legacyPath := filepath.Join(paths.AppDir, legacyYAMLConfigFileName)
if _, legacyErr := os.Stat(legacyPath); legacyErr == nil { imported, ok, err := importYAMLConfig(legacyPath, config)
// gosentry.yaml is the pre-JSON-migration config file. Read it once if err != nil {
// when gosentry.json is absent so existing installs migrate without return domain.Config{}, err
// manual intervention. SaveConfig rewrites the result as gosentry.json. }
configPath = legacyPath if !ok {
} else {
return config, writeJSON(paths.ConfigPath, config) return config, writeJSON(paths.ConfigPath, config)
} }
config = imported
} else {
data, err := os.ReadFile(paths.ConfigPath)
if err != nil {
return domain.Config{}, err
}
if err := json.Unmarshal(data, &config); err != nil {
return domain.Config{}, err
}
} }
if _, err := os.Stat(configPath); errors.Is(err, os.ErrNotExist) {
return config, writeJSON(paths.ConfigPath, config)
}
data, err := os.ReadFile(configPath)
if err != nil {
return domain.Config{}, err
}
if err := json.Unmarshal(data, &config); err != nil {
return domain.Config{}, err
}
if strings.TrimSpace(config.JobsDir) == "" { if strings.TrimSpace(config.JobsDir) == "" {
// Empty paths are treated as missing values rather than intentional root // Empty paths are treated as missing values rather than intentional root
// directories. This avoids accidentally writing jobs to unexpected places. // directories. This avoids accidentally writing jobs to unexpected places.
@@ -121,8 +153,19 @@ func loadOrCreateConfig(paths Paths) (domain.Config, error) {
func loadOrCreateJobs(path string) ([]domain.Job, error) { func loadOrCreateJobs(path string) ([]domain.Job, error) {
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) { if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
// The first run creates harmless sample jobs so a new user can immediately // No JSON jobs file yet. Import a pre-migration jobs.yaml once if present;
// see scheduled and manual execution without inventing a command. // otherwise seed harmless sample jobs so a new user can immediately see
// scheduled and manual execution without inventing a command. Imported jobs
// are returned unsaved here — the caller's SaveJobs rewrites them as
// jobs.json after normalization.
legacyPath := filepath.Join(filepath.Dir(path), legacyYAMLJobsFileName)
imported, ok, err := importYAMLJobs(legacyPath)
if err != nil {
return nil, err
}
if ok {
return imported, nil
}
jobs := defaultJobs() jobs := defaultJobs()
normalizeJobs(jobs) normalizeJobs(jobs)
return jobs, writeJSON(path, domain.JobsFile{Jobs: jobs}) return jobs, writeJSON(path, domain.JobsFile{Jobs: jobs})
@@ -139,6 +182,46 @@ func loadOrCreateJobs(path string) ([]domain.Job, error) {
return file.Jobs, nil return file.Jobs, nil
} }
// importYAMLConfig reads a pre-migration gosentry.yaml into the current Config
// shape. It returns ok=false when the file is absent so the caller falls back to
// writing fresh defaults. The supplied base seeds the shadow struct so keys that
// the YAML omits keep their default value instead of becoming zero.
func importYAMLConfig(path string, base domain.Config) (domain.Config, bool, error) {
data, err := os.ReadFile(path)
if errors.Is(err, os.ErrNotExist) {
return domain.Config{}, false, nil
}
if err != nil {
return domain.Config{}, false, err
}
shadow := yamlConfig(base)
if err := yaml.Unmarshal(data, &shadow); err != nil {
return domain.Config{}, false, err
}
return domain.Config(shadow), true, nil
}
// importYAMLJobs reads a pre-migration jobs.yaml into durable domain jobs. It
// returns ok=false when the file is absent so the caller can seed default jobs.
func importYAMLJobs(path string) ([]domain.Job, bool, error) {
data, err := os.ReadFile(path)
if errors.Is(err, os.ErrNotExist) {
return nil, false, nil
}
if err != nil {
return nil, false, err
}
var file yamlJobsFile
if err := yaml.Unmarshal(data, &file); err != nil {
return nil, false, err
}
jobs := make([]domain.Job, len(file.Jobs))
for i := range file.Jobs {
jobs[i] = domain.Job(file.Jobs[i])
}
return jobs, true, nil
}
func normalizeJobs(jobs []domain.Job) { func normalizeJobs(jobs []domain.Job) {
next := 1 next := 1
for index := range jobs { for index := range jobs {