55 lines
1.2 KiB
Go
55 lines
1.2 KiB
Go
package update
|
|
|
|
import (
|
|
domain "RenewCMS/internal/domain/update"
|
|
"encoding/json"
|
|
"errors"
|
|
"io/fs"
|
|
"os"
|
|
"sync"
|
|
)
|
|
|
|
type JSONRestartRequestStore struct {
|
|
path string
|
|
mu sync.Mutex
|
|
}
|
|
|
|
var _ domain.RestartRequestStore = &JSONRestartRequestStore{}
|
|
|
|
func NewJSONRestartRequestStore(path string) *JSONRestartRequestStore {
|
|
return &JSONRestartRequestStore{path: path}
|
|
}
|
|
|
|
func (s *JSONRestartRequestStore) Write(request domain.RestartRequest) error {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
return writeJSONAtomically(s.path, request, statusFilePermissions)
|
|
}
|
|
|
|
func (s *JSONRestartRequestStore) Read() (domain.RestartRequest, error) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
info, err := os.Lstat(s.path)
|
|
if errors.Is(err, fs.ErrNotExist) || (err == nil && info.IsDir()) {
|
|
return domain.RestartRequest{}, domain.ErrNoRestartRequest
|
|
}
|
|
if err != nil {
|
|
return domain.RestartRequest{}, err
|
|
}
|
|
content, err := os.ReadFile(s.path)
|
|
if err != nil {
|
|
return domain.RestartRequest{}, err
|
|
}
|
|
var request domain.RestartRequest
|
|
return request, json.Unmarshal(content, &request)
|
|
}
|
|
|
|
func (s *JSONRestartRequestStore) Delete() error {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
return removeIfPresent(s.path)
|
|
}
|