43 lines
1.2 KiB
Go
43 lines
1.2 KiB
Go
package profile
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io/fs"
|
|
)
|
|
|
|
// ReadOnlyLoad reads and validates Profile configuration without creating,
|
|
// chmodding, chowning, locking, or replacing any filesystem object.
|
|
func (s *FileStore) ReadOnlyLoad() (State, error) {
|
|
data, err := s.secureStore().ReadFile(s.path)
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
return State{}, nil
|
|
}
|
|
if err != nil {
|
|
return State{}, fmt.Errorf("open Profile configuration read-only without following links: %w", err)
|
|
}
|
|
var state State
|
|
if err := json.Unmarshal(data, &state); err != nil {
|
|
return State{}, fmt.Errorf("parse Profile configuration: %w", err)
|
|
}
|
|
for _, candidate := range state.Profiles {
|
|
if _, err := ValidateAndNormalize(candidate); err != nil {
|
|
return State{}, fmt.Errorf("Profile %q is invalid: %w", candidate.Name, err)
|
|
}
|
|
}
|
|
if state.ActiveProfile != "" && indexProfile(state.Profiles, state.ActiveProfile) < 0 {
|
|
return State{}, fmt.Errorf("Active Profile %q does not exist", state.ActiveProfile)
|
|
}
|
|
return state, nil
|
|
}
|
|
|
|
func indexProfile(profiles []Profile, name string) int {
|
|
for i := range profiles {
|
|
if profiles[i].Name == name {
|
|
return i
|
|
}
|
|
}
|
|
return -1
|
|
}
|