mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-05-20 15:10:28 -04:00
cli: create or read state file during constellation create
(#2470)
Signed-off-by: Daniel Weiße <dw@edgeless.systems>
This commit is contained in:
parent
1a141c3972
commit
fe7e16e1cc
4 changed files with 128 additions and 88 deletions
|
@ -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: |
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue