2022-05-12 09:14:52 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-06-09 10:10:42 -04:00
|
|
|
"fmt"
|
|
|
|
|
2022-06-07 05:08:44 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/cloud/cloudprovider"
|
2022-05-12 09:14:52 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/config"
|
|
|
|
"github.com/edgelesssys/constellation/internal/constants"
|
2022-05-16 11:32:00 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/file"
|
2022-05-12 09:14:52 -04:00
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/spf13/cobra"
|
2022-05-16 12:54:25 -04:00
|
|
|
"github.com/talos-systems/talos/pkg/machinery/config/encoder"
|
2022-05-12 09:14:52 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func newConfigGenerateCmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
2022-05-18 05:39:14 -04:00
|
|
|
Use: "generate {aws|azure|gcp}",
|
2022-05-12 09:14:52 -04:00
|
|
|
Short: "Generate a default configuration file",
|
|
|
|
Long: "Generate a default configuration file for your selected cloud provider.",
|
2022-05-18 05:39:14 -04:00
|
|
|
Args: cobra.MatchAll(
|
|
|
|
cobra.ExactArgs(1),
|
|
|
|
isCloudProvider(0),
|
|
|
|
warnAWS(0),
|
|
|
|
),
|
|
|
|
ValidArgsFunction: generateCompletion,
|
|
|
|
RunE: runConfigGenerate,
|
2022-05-12 09:14:52 -04:00
|
|
|
}
|
2022-05-24 05:56:40 -04:00
|
|
|
cmd.Flags().StringP("file", "f", constants.ConfigFilename, "path to output file, or '-' for stdout")
|
2022-05-12 09:14:52 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
type generateFlags struct {
|
|
|
|
file string
|
|
|
|
}
|
|
|
|
|
|
|
|
func runConfigGenerate(cmd *cobra.Command, args []string) error {
|
|
|
|
fileHandler := file.NewHandler(afero.NewOsFs())
|
2022-05-18 05:39:14 -04:00
|
|
|
provider := cloudprovider.FromString(args[0])
|
|
|
|
return configGenerate(cmd, fileHandler, provider)
|
2022-05-12 09:14:52 -04:00
|
|
|
}
|
|
|
|
|
2022-05-18 05:39:14 -04:00
|
|
|
func configGenerate(cmd *cobra.Command, fileHandler file.Handler, provider cloudprovider.Provider) error {
|
2022-05-12 09:14:52 -04:00
|
|
|
flags, err := parseGenerateFlags(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-18 05:39:14 -04:00
|
|
|
conf := config.Default()
|
|
|
|
conf.RemoveProviderExcept(provider)
|
|
|
|
|
2022-05-12 09:14:52 -04:00
|
|
|
if flags.file == "-" {
|
2022-05-18 05:39:14 -04:00
|
|
|
content, err := encoder.NewEncoder(conf).Encode()
|
2022-05-16 12:54:25 -04:00
|
|
|
if err != nil {
|
2022-06-09 10:10:42 -04:00
|
|
|
return fmt.Errorf("encoding config content: %w", err)
|
2022-05-16 12:54:25 -04:00
|
|
|
}
|
|
|
|
_, err = cmd.OutOrStdout().Write(content)
|
|
|
|
return err
|
2022-05-12 09:14:52 -04:00
|
|
|
}
|
|
|
|
|
2022-08-08 05:04:17 -04:00
|
|
|
if err := fileHandler.WriteYAML(flags.file, conf, file.OptMkdirAll); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmd.Println("Config file written to", flags.file)
|
|
|
|
cmd.Println("Please fill in your CSP specific configuration before proceeding.")
|
2022-09-01 10:46:24 -04:00
|
|
|
cmd.Println("You can find the list of supported virtual machine types by executing:")
|
|
|
|
cmd.Println("\tconstellation config instance-types")
|
2022-08-08 05:04:17 -04:00
|
|
|
return nil
|
2022-05-12 09:14:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseGenerateFlags(cmd *cobra.Command) (generateFlags, error) {
|
|
|
|
file, err := cmd.Flags().GetString("file")
|
|
|
|
if err != nil {
|
2022-06-09 10:10:42 -04:00
|
|
|
return generateFlags{}, fmt.Errorf("parsing config generate flags: %w", err)
|
2022-05-12 09:14:52 -04:00
|
|
|
}
|
|
|
|
return generateFlags{
|
|
|
|
file: file,
|
|
|
|
}, nil
|
|
|
|
}
|
2022-05-18 05:39:14 -04:00
|
|
|
|
|
|
|
// createCompletion handles the completion of the create command. It is frequently called
|
|
|
|
// while the user types arguments of the command to suggest completion.
|
|
|
|
func generateCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
|
|
switch len(args) {
|
|
|
|
case 0:
|
|
|
|
return []string{"aws", "gcp", "azure"}, cobra.ShellCompDirectiveNoFileComp
|
|
|
|
default:
|
|
|
|
return []string{}, cobra.ShellCompDirectiveError
|
|
|
|
}
|
|
|
|
}
|