inits/pkg/cmd/init/boot/state.go
Mikaël Cluseau 6d5caeb71f fix(boot)
2019-03-13 16:01:34 +11:00

49 lines
765 B
Go

package initboot
import (
"encoding/json"
"io/ioutil"
"log"
"os"
)
var stateFile = "/run/dkl-boot.state"
func readState() (state map[string]bool) {
state = map[string]bool{}
ba, err := ioutil.ReadFile(stateFile)
if err != nil {
if os.IsNotExist(err) {
return
}
log.Fatal("failed to read state: ", err)
}
err = json.Unmarshal(ba, &state)
if err != nil {
log.Fatal("failed to parse state: ", err)
}
return
}
func writeState(state map[string]bool) {
ba, err := json.Marshal(state)
if err != nil {
log.Fatal("failed to serialize state: ", err)
}
ioutil.WriteFile(stateFile, ba, 0600)
}
func step(step string, operation func()) {
state := readState()
if !state[step] {
operation()
state[step] = true
writeState(state)
}
}