2023-05-03 05:11:53 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/config"
|
2023-08-04 09:57:36 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/config/migration"
|
2023-08-04 07:53:51 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
2023-05-03 05:11:53 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newConfigMigrateCmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "migrate",
|
|
|
|
Short: "Migrate a configuration file to a new version",
|
|
|
|
Long: "Migrate a configuration file to a new version.",
|
|
|
|
Args: cobra.NoArgs,
|
|
|
|
RunE: runConfigMigrate,
|
|
|
|
}
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func runConfigMigrate(cmd *cobra.Command, _ []string) error {
|
|
|
|
handler := file.NewHandler(afero.NewOsFs())
|
2023-08-04 07:53:51 -04:00
|
|
|
return configMigrate(cmd, handler)
|
2023-05-03 05:11:53 -04:00
|
|
|
}
|
|
|
|
|
2023-08-04 07:53:51 -04:00
|
|
|
func configMigrate(cmd *cobra.Command, handler file.Handler) error {
|
2023-08-04 09:57:36 -04:00
|
|
|
// Make sure we are reading a v3 config
|
2023-05-03 05:11:53 -04:00
|
|
|
var cfgVersion struct {
|
|
|
|
Version string `yaml:"version"`
|
|
|
|
}
|
2023-08-04 07:53:51 -04:00
|
|
|
if err := handler.ReadYAML(constants.ConfigFilename, &cfgVersion); err != nil {
|
2023-05-03 05:11:53 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch cfgVersion.Version {
|
2023-08-02 08:21:05 -04:00
|
|
|
case config.Version4:
|
|
|
|
cmd.Printf("Config already at version %s, nothing to do\n", config.Version4)
|
2023-05-03 05:11:53 -04:00
|
|
|
return nil
|
2023-08-04 09:57:36 -04:00
|
|
|
case migration.Version3:
|
|
|
|
if err := migration.V3ToV4(constants.ConfigFilename, handler); err != nil {
|
|
|
|
return fmt.Errorf("migrating config: %w", err)
|
|
|
|
}
|
|
|
|
cmd.Printf("Successfully migrated config to %s\n", config.Version4)
|
|
|
|
return nil
|
2023-05-03 05:11:53 -04:00
|
|
|
default:
|
2023-08-02 08:21:05 -04:00
|
|
|
return fmt.Errorf("cannot convert config version %s to %s", cfgVersion.Version, config.Version4)
|
2023-05-03 05:11:53 -04:00
|
|
|
}
|
|
|
|
}
|