Switch storage serialization from YAML to JSON (P1.2)
Replace writeYAML with writeJSON (json.MarshalIndent, 2-space indent, trailing newline) and switch the config and jobs Unmarshal calls to encoding/json. Mark P1.2 done in the pre-release task list. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -81,7 +81,7 @@ These land together because both edit `domain/job.go` and `storage/store.go`.
|
|||||||
|
|
||||||
### Phase 1 — Storage JSON + exit-code removal
|
### Phase 1 — Storage JSON + exit-code removal
|
||||||
- [x] P1.1 — JSON struct tags
|
- [x] P1.1 — JSON struct tags
|
||||||
- [ ] P1.2 — `writeJSON` + JSON unmarshal
|
- [x] P1.2 — `writeJSON` + JSON unmarshal
|
||||||
- [ ] P1.3 — `gosentry.json` / `jobs.json` paths; drop pysentry name
|
- [ ] P1.3 — `gosentry.json` / `jobs.json` paths; drop pysentry name
|
||||||
- [ ] P1.4 — One-time YAML import
|
- [ ] P1.4 — One-time YAML import
|
||||||
- [ ] P1.5 — Remove `SuccessExitCodes` across code
|
- [ ] P1.5 — Remove `SuccessExitCodes` across code
|
||||||
|
|||||||
+14
-11
@@ -1,6 +1,7 @@
|
|||||||
package storage
|
package storage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -8,7 +9,6 @@ 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 {
|
||||||
@@ -55,14 +55,14 @@ func (s *Store) SaveConfig() error {
|
|||||||
if err := os.MkdirAll(s.Paths.AppDir, 0o755); err != nil {
|
if err := os.MkdirAll(s.Paths.AppDir, 0o755); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return writeYAML(s.Paths.ConfigPath, s.Config)
|
return writeJSON(s.Paths.ConfigPath, s.Config)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) SaveJobs(jobs []domain.Job) error {
|
func (s *Store) SaveJobs(jobs []domain.Job) error {
|
||||||
if err := os.MkdirAll(s.Paths.JobsDir, 0o755); err != nil {
|
if err := os.MkdirAll(s.Paths.JobsDir, 0o755); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return writeYAML(s.Paths.JobsPath, domain.JobsFile{Jobs: jobs})
|
return writeJSON(s.Paths.JobsPath, domain.JobsFile{Jobs: jobs})
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadOrCreateConfig(paths Paths) (domain.Config, error) {
|
func loadOrCreateConfig(paths Paths) (domain.Config, error) {
|
||||||
@@ -89,19 +89,19 @@ func loadOrCreateConfig(paths Paths) (domain.Config, error) {
|
|||||||
// naturally rewrites it under gosentry.yaml.
|
// naturally rewrites it under gosentry.yaml.
|
||||||
configPath = legacyPath
|
configPath = legacyPath
|
||||||
} else {
|
} else {
|
||||||
return config, writeYAML(paths.ConfigPath, config)
|
return config, writeJSON(paths.ConfigPath, config)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := os.Stat(configPath); errors.Is(err, os.ErrNotExist) {
|
if _, err := os.Stat(configPath); errors.Is(err, os.ErrNotExist) {
|
||||||
return config, writeYAML(paths.ConfigPath, config)
|
return config, writeJSON(paths.ConfigPath, config)
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := os.ReadFile(configPath)
|
data, err := os.ReadFile(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return domain.Config{}, err
|
return domain.Config{}, err
|
||||||
}
|
}
|
||||||
if err := yaml.Unmarshal(data, &config); err != nil {
|
if err := json.Unmarshal(data, &config); err != nil {
|
||||||
return domain.Config{}, err
|
return domain.Config{}, err
|
||||||
}
|
}
|
||||||
if strings.TrimSpace(config.JobsDir) == "" {
|
if strings.TrimSpace(config.JobsDir) == "" {
|
||||||
@@ -127,7 +127,7 @@ func loadOrCreateJobs(path string) ([]domain.Job, error) {
|
|||||||
// see scheduled and manual execution without inventing a command.
|
// see scheduled and manual execution without inventing a command.
|
||||||
jobs := defaultJobs()
|
jobs := defaultJobs()
|
||||||
normalizeJobs(jobs)
|
normalizeJobs(jobs)
|
||||||
return jobs, writeYAML(path, domain.JobsFile{Jobs: jobs})
|
return jobs, writeJSON(path, domain.JobsFile{Jobs: jobs})
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := os.ReadFile(path)
|
data, err := os.ReadFile(path)
|
||||||
@@ -135,7 +135,7 @@ func loadOrCreateJobs(path string) ([]domain.Job, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var file domain.JobsFile
|
var file domain.JobsFile
|
||||||
if err := yaml.Unmarshal(data, &file); err != nil {
|
if err := json.Unmarshal(data, &file); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return file.Jobs, nil
|
return file.Jobs, nil
|
||||||
@@ -195,16 +195,19 @@ func (s *Store) applyConfigPaths() {
|
|||||||
s.Paths.LogsDir = resolveConfiguredDir(s.Paths.AppDir, s.Config.LogsDir)
|
s.Paths.LogsDir = resolveConfiguredDir(s.Paths.AppDir, s.Config.LogsDir)
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeYAML(path string, value any) error {
|
func writeJSON(path string, value any) error {
|
||||||
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
data, err := yaml.Marshal(value)
|
data, err := json.MarshalIndent(value, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
// A trailing newline keeps the file friendly to editors and diff tools that
|
||||||
|
// expect text files to end with one.
|
||||||
|
data = append(data, '\n')
|
||||||
// WriteFile replaces the full file instead of patching it in place. For small
|
// WriteFile replaces the full file instead of patching it in place. For small
|
||||||
// YAML files this is simpler and prevents stale keys from older versions from
|
// JSON files this is simpler and prevents stale keys from older versions from
|
||||||
// lingering after the schema changes.
|
// lingering after the schema changes.
|
||||||
return os.WriteFile(path, data, 0o644)
|
return os.WriteFile(path, data, 0o644)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user