2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-05-23 09:01:39 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/config"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
2022-05-23 09:01:39 -04:00
|
|
|
)
|
|
|
|
|
2022-09-07 09:38:29 -04:00
|
|
|
func readConfig(out io.Writer, fileHandler file.Handler, name string) (*config.Config, error) {
|
2022-08-08 05:04:17 -04:00
|
|
|
if name == "" {
|
|
|
|
return config.Default(), nil
|
|
|
|
}
|
|
|
|
|
2022-05-23 09:01:39 -04:00
|
|
|
cnf, err := config.FromFile(fileHandler, name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-07 09:38:29 -04:00
|
|
|
if err := validateConfig(out, cnf); err != nil {
|
2022-05-23 09:01:39 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-07 09:38:29 -04:00
|
|
|
|
2022-05-23 09:01:39 -04:00
|
|
|
return cnf, nil
|
|
|
|
}
|
|
|
|
|
2022-09-07 09:38:29 -04:00
|
|
|
func validateConfig(out io.Writer, cnf *config.Config) error {
|
2022-05-23 09:01:39 -04:00
|
|
|
msgs, err := cnf.Validate()
|
|
|
|
if err != nil {
|
2022-06-09 10:10:42 -04:00
|
|
|
return fmt.Errorf("performing config validation: %w", err)
|
2022-05-23 09:01:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(msgs) > 0 {
|
|
|
|
fmt.Fprintln(out, "Invalid fields in config file:")
|
|
|
|
for _, m := range msgs {
|
|
|
|
fmt.Fprintln(out, "\t"+m)
|
|
|
|
}
|
2022-08-08 05:04:17 -04:00
|
|
|
fmt.Fprintln(out, "Fix the invalid entries or generate a new configuration using `constellation config generate`")
|
|
|
|
return errors.New("invalid configuration")
|
2022-05-23 09:01:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|