cli: create or read state file during constellation create (#2470)

Signed-off-by: Daniel Weiße <dw@edgeless.systems>
This commit is contained in:
Daniel Weiße 2023-10-17 14:37:09 +02:00 committed by GitHub
parent 1a141c3972
commit fe7e16e1cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 128 additions and 88 deletions

View file

@ -14,7 +14,9 @@ package state
import (
"encoding/hex"
"errors"
"fmt"
"os"
"dario.cat/mergo"
"github.com/edgelesssys/constellation/v2/internal/file"
@ -34,6 +36,20 @@ func ReadFromFile(fileHandler file.Handler, path string) (*State, error) {
return state, nil
}
// CreateOrRead reads the state file at the given path, if it exists, and returns the state.
// If the file does not exist, a new state is created and written to disk.
func CreateOrRead(fileHandler file.Handler, path string) (*State, error) {
state := &State{}
if err := fileHandler.ReadYAML(path, &state); err != nil {
if !errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("reading state file: %w", err)
}
state = New()
return state, state.WriteToFile(fileHandler, path)
}
return state, nil
}
// State describe the entire state to describe a Constellation cluster.
type State struct {
// description: |