-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime_state.go
More file actions
38 lines (34 loc) · 916 Bytes
/
Copy pathruntime_state.go
File metadata and controls
38 lines (34 loc) · 916 Bytes
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
package contexting
import (
"encoding/json"
"fmt"
"os"
"time"
)
type RuntimeState struct {
RootPath string `json:"root_path"`
Address string `json:"address"`
PID int `json:"pid"`
StartedAt time.Time `json:"started_at"`
}
func SaveRuntimeState(path string, state RuntimeState) error {
bytes, err := json.MarshalIndent(state, "", " ")
if err != nil {
return fmt.Errorf("marshal runtime state: %w", err)
}
if err := writeFileAtomic(path, append(bytes, '\n'), 0o644); err != nil {
return fmt.Errorf("write runtime state: %w", err)
}
return nil
}
func LoadRuntimeState(path string) (*RuntimeState, error) {
bytes, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read runtime state: %w", err)
}
var state RuntimeState
if err := json.Unmarshal(bytes, &state); err != nil {
return nil, fmt.Errorf("parse runtime state: %w", err)
}
return &state, nil
}