| 1 |
package state |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"os" |
| 8 |
"path/filepath" |
| 9 |
"time" |
| 10 |
|
| 11 |
"github.com/blang/semver" |
| 12 |
) |
| 13 |
|
| 14 |
var DefaultState = State{ |
| 15 |
Version: 1, |
| 16 |
} |
| 17 |
|
| 18 |
func GetDefaultStateFilepath() (string, error) { |
| 19 |
home, err := os.UserHomeDir() |
| 20 |
if err != nil { |
| 21 |
return "", fmt.Errorf("$HOME not set, can't determine a state file path") |
| 22 |
} |
| 23 |
stateDirpath := filepath.Join(home, ".state", "humanlog") |
| 24 |
stateFilepath := filepath.Join(stateDirpath, "state.json") |
| 25 |
dfi, err := os.Stat(stateDirpath) |
| 26 |
if err != nil && !errors.Is(err, os.ErrNotExist) { |
| 27 |
return "", fmt.Errorf("state dir %q can't be read: %v", stateDirpath, err) |
| 28 |
} |
| 29 |
if errors.Is(err, os.ErrNotExist) { |
| 30 |
if err := os.MkdirAll(stateDirpath, 0700); err != nil { |
| 31 |
return "", fmt.Errorf("state dir %q can't be created: %v", stateDirpath, err) |
| 32 |
} |
| 33 |
} else if !dfi.IsDir() { |
| 34 |
return "", fmt.Errorf("state dir %q isn't a directory", stateDirpath) |
| 35 |
} |
| 36 |
ffi, err := os.Stat(stateFilepath) |
| 37 |
if err != nil && !errors.Is(err, os.ErrNotExist) { |
| 38 |
return "", fmt.Errorf("can't stat state file: %v", err) |
| 39 |
} |
| 40 |
if errors.Is(err, os.ErrNotExist) { |
| 41 |
|
| 42 |
} else if !ffi.Mode().IsRegular() { |
| 43 |
return "", fmt.Errorf("state file %q isn't a regular file", stateFilepath) |
| 44 |
} |
| 45 |
return stateFilepath, nil |
| 46 |
} |
| 47 |
|
| 48 |
func ReadStateFile(path string, dflt *State) (*State, error) { |
| 49 |
stateFile, err := os.Open(path) |
| 50 |
if err != nil { |
| 51 |
if !errors.Is(err, os.ErrNotExist) { |
| 52 |
return nil, fmt.Errorf("opening state file %q: %v", path, err) |
| 53 |
} |
| 54 |
cfg := (State{path: path}).populateEmpty(dflt) |
| 55 |
return cfg, WriteStateFile(path, cfg) |
| 56 |
} |
| 57 |
defer stateFile.Close() |
| 58 |
var cfg State |
| 59 |
if err := json.NewDecoder(stateFile).Decode(&cfg); err != nil { |
| 60 |
return nil, fmt.Errorf("decoding state file: %v", err) |
| 61 |
} |
| 62 |
cfg.path = path |
| 63 |
return cfg.populateEmpty(dflt), nil |
| 64 |
} |
| 65 |
|
| 66 |
func WriteStateFile(path string, state *State) error { |
| 67 |
content, err := json.MarshalIndent(state, "", "\t") |
| 68 |
if err != nil { |
| 69 |
return fmt.Errorf("marshaling state file: %v", err) |
| 70 |
} |
| 71 |
|
| 72 |
newf, err := os.CreateTemp(filepath.Dir(path), "humanlog_statefile") |
| 73 |
if err != nil { |
| 74 |
return fmt.Errorf("creating temporary file for statefile: %w", err) |
| 75 |
} |
| 76 |
success := false |
| 77 |
defer func() { |
| 78 |
if !success { |
| 79 |
_ = os.Remove(newf.Name()) |
| 80 |
} |
| 81 |
}() |
| 82 |
if _, err := newf.Write(content); err != nil { |
| 83 |
return fmt.Errorf("writing to temporary statefile: %w", err) |
| 84 |
} |
| 85 |
if err := newf.Close(); err != nil { |
| 86 |
return fmt.Errorf("closing temporary statefile: %w", err) |
| 87 |
} |
| 88 |
if err := os.Chmod(newf.Name(), 0600); err != nil { |
| 89 |
return fmt.Errorf("setting permissions on temporary statefile: %w", err) |
| 90 |
} |
| 91 |
if err := os.Rename(newf.Name(), path); err != nil { |
| 92 |
return fmt.Errorf("replacing statefile at %q with %q: %w", path, newf.Name(), err) |
| 93 |
} |
| 94 |
success = true |
| 95 |
return nil |
| 96 |
} |
| 97 |
|
| 98 |
type State struct { |
| 99 |
Version int `json:"version"` |
| 100 |
AccountID *int64 `json:"account_id"` |
| 101 |
MachineID *int64 `json:"machine_id"` |
| 102 |
LatestKnownVersion *semver.Version `json:"latest_known_version,omitempty"` |
| 103 |
LastestKnownVersionUpdatedAt *time.Time `json:"latest_known_version_updated_at"` |
| 104 |
|
| 105 |
|
| 106 |
path string |
| 107 |
} |
| 108 |
|
| 109 |
func (cfg *State) WriteBack() error { |
| 110 |
return WriteStateFile(cfg.path, cfg) |
| 111 |
} |
| 112 |
|
| 113 |
func (cfg State) populateEmpty(other *State) *State { |
| 114 |
ptr := &cfg |
| 115 |
out := *ptr |
| 116 |
if out.AccountID == nil && other.AccountID != nil { |
| 117 |
out.AccountID = other.AccountID |
| 118 |
} |
| 119 |
if out.MachineID == nil && other.MachineID != nil { |
| 120 |
out.MachineID = other.MachineID |
| 121 |
} |
| 122 |
if out.LatestKnownVersion == nil && other.LatestKnownVersion != nil { |
| 123 |
out.LatestKnownVersion = other.LatestKnownVersion |
| 124 |
} |
| 125 |
if out.LastestKnownVersionUpdatedAt == nil && other.LastestKnownVersionUpdatedAt != nil { |
| 126 |
out.LastestKnownVersionUpdatedAt = other.LastestKnownVersionUpdatedAt |
| 127 |
} |
| 128 |
return &out |
| 129 |
} |