2022-05-23 09:01:39 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
2022-06-07 05:08:44 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/cloud/cloudprovider"
|
2022-05-23 09:01:39 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/config"
|
|
|
|
"github.com/edgelesssys/constellation/internal/file"
|
|
|
|
)
|
|
|
|
|
|
|
|
func readConfig(out io.Writer, fileHandler file.Handler, name string, provider cloudprovider.Provider) (*config.Config, error) {
|
|
|
|
cnf, err := config.FromFile(fileHandler, name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := validateConfig(out, cnf, provider); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return cnf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateConfig(out io.Writer, cnf *config.Config, provider cloudprovider.Provider) error {
|
|
|
|
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-06-09 10:10:42 -04:00
|
|
|
return errors.New("invalid configuration. Fix the invalid entries or generate a new configuration using `constellation config generate`")
|
2022-05-23 09:01:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if provider != cloudprovider.Unknown && !cnf.HasProvider(provider) {
|
|
|
|
return fmt.Errorf("configuration doesn't contain provider: %v", provider)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|