AB#2094 cloud provider specific configs (#151)

add argument to generate cloud specific configuration file
This commit is contained in:
Fabian Kammel 2022-05-18 11:39:14 +02:00 committed by GitHub
parent 54e2e492df
commit 7c2d1c3490
5 changed files with 113 additions and 12 deletions

View file

@ -1,6 +1,7 @@
package cmd
import (
"github.com/edgelesssys/constellation/cli/cloudprovider"
"github.com/edgelesssys/constellation/internal/config"
"github.com/edgelesssys/constellation/internal/constants"
"github.com/edgelesssys/constellation/internal/file"
@ -11,11 +12,16 @@ import (
func newConfigGenerateCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "generate",
Use: "generate {aws|azure|gcp}",
Short: "Generate a default configuration file",
Long: "Generate a default configuration file for your selected cloud provider.",
Args: cobra.ExactArgs(0),
RunE: runConfigGenerate,
Args: cobra.MatchAll(
cobra.ExactArgs(1),
isCloudProvider(0),
warnAWS(0),
),
ValidArgsFunction: generateCompletion,
RunE: runConfigGenerate,
}
cmd.Flags().StringP("file", "f", constants.ConfigFilename, "output file")
@ -28,17 +34,21 @@ type generateFlags struct {
func runConfigGenerate(cmd *cobra.Command, args []string) error {
fileHandler := file.NewHandler(afero.NewOsFs())
return configGenerate(cmd, fileHandler)
provider := cloudprovider.FromString(args[0])
return configGenerate(cmd, fileHandler, provider)
}
func configGenerate(cmd *cobra.Command, fileHandler file.Handler) error {
func configGenerate(cmd *cobra.Command, fileHandler file.Handler, provider cloudprovider.Provider) error {
flags, err := parseGenerateFlags(cmd)
if err != nil {
return err
}
conf := config.Default()
conf.RemoveProviderExcept(provider)
if flags.file == "-" {
content, err := encoder.NewEncoder(config.Default()).Encode()
content, err := encoder.NewEncoder(conf).Encode()
if err != nil {
return err
}
@ -46,7 +56,7 @@ func configGenerate(cmd *cobra.Command, fileHandler file.Handler) error {
return err
}
return fileHandler.WriteYAML(flags.file, config.Default(), 0o644)
return fileHandler.WriteYAML(flags.file, conf, 0o644)
}
func parseGenerateFlags(cmd *cobra.Command) (generateFlags, error) {
@ -58,3 +68,14 @@ func parseGenerateFlags(cmd *cobra.Command) (generateFlags, error) {
file: file,
}, nil
}
// 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
}
}