Feat/config/generate (#136)

Implement config command & generate verb to write default configuration to file or stdout.
This commit is contained in:
Fabian Kammel 2022-05-12 15:14:52 +02:00 committed by GitHub
parent 49ee05b680
commit 094a8b7659
5 changed files with 145 additions and 0 deletions

18
cli/cmd/config.go Normal file
View File

@ -0,0 +1,18 @@
package cmd
import (
"github.com/spf13/cobra"
)
func newConfigCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Short: "Work with Constellation configuration file",
Long: "Generate & manipulate configuration file for Constellation.",
Args: cobra.ExactArgs(0),
}
cmd.AddCommand(newConfigGenerateCmd())
return cmd
}

55
cli/cmd/configgenerate.go Normal file
View File

@ -0,0 +1,55 @@
package cmd
import (
"github.com/edgelesssys/constellation/cli/file"
"github.com/edgelesssys/constellation/internal/config"
"github.com/edgelesssys/constellation/internal/constants"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)
func newConfigGenerateCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "generate",
Short: "Generate a default configuration file",
Long: "Generate a default configuration file for your selected cloud provider.",
Args: cobra.ExactArgs(0),
RunE: runConfigGenerate,
}
cmd.Flags().StringP("file", "f", constants.ConfigFilename, "output file")
return cmd
}
type generateFlags struct {
file string
}
func runConfigGenerate(cmd *cobra.Command, args []string) error {
fileHandler := file.NewHandler(afero.NewOsFs())
return configGenerate(cmd, fileHandler)
}
func configGenerate(cmd *cobra.Command, fileHandler file.Handler) error {
flags, err := parseGenerateFlags(cmd)
if err != nil {
return err
}
if flags.file == "-" {
return yaml.NewEncoder(cmd.OutOrStdout()).Encode(config.Default())
}
return fileHandler.WriteYAML(flags.file, config.Default(), 0o644)
}
func parseGenerateFlags(cmd *cobra.Command) (generateFlags, error) {
file, err := cmd.Flags().GetString("file")
if err != nil {
return generateFlags{}, err
}
return generateFlags{
file: file,
}, nil
}

View File

@ -0,0 +1,70 @@
package cmd
import (
"bytes"
"testing"
"github.com/edgelesssys/constellation/cli/file"
"github.com/edgelesssys/constellation/internal/config"
"github.com/edgelesssys/constellation/internal/constants"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)
func defaultConfigAsYAML(t *testing.T) string {
var readBuffer bytes.Buffer
require.NoError(t, yaml.NewEncoder(&readBuffer).Encode(config.Default()))
return readBuffer.String()
}
func TestConfigGenerateDefault(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
fileHandler := file.NewHandler(afero.NewMemMapFs())
cmd := newConfigGenerateCmd()
require.NoError(configGenerate(cmd, fileHandler))
readYAML, err := fileHandler.Read(constants.ConfigFilename)
assert.NoError(err)
assert.Equal(defaultConfigAsYAML(t), string(readYAML))
}
func TestConfigGenerateDefaultExists(t *testing.T) {
require := require.New(t)
fileHandler := file.NewHandler(afero.NewMemMapFs())
require.NoError(fileHandler.Write(constants.ConfigFilename, []byte("foobar"), file.OptNone))
cmd := newConfigGenerateCmd()
require.Error(configGenerate(cmd, fileHandler))
}
func TestConfigGenerateFileFlagRemoved(t *testing.T) {
require := require.New(t)
fileHandler := file.NewHandler(afero.NewMemMapFs())
cmd := newConfigGenerateCmd()
cmd.ResetFlags()
require.Error(configGenerate(cmd, fileHandler))
}
func TestConfigGenerateStdOut(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
fileHandler := file.NewHandler(afero.NewMemMapFs())
var outBuffer bytes.Buffer
cmd := newConfigGenerateCmd()
cmd.SetOut(&outBuffer)
require.NoError(cmd.Flags().Set("file", "-"))
require.NoError(configGenerate(cmd, fileHandler))
assert.Equal(defaultConfigAsYAML(t), outBuffer.String())
}

View File

@ -38,6 +38,7 @@ func NewRootCmd() *cobra.Command {
rootCmd.AddCommand(newRecoverCmd())
rootCmd.AddCommand(newTerminateCmd())
rootCmd.AddCommand(newVersionCmd())
rootCmd.AddCommand(newConfigCmd())
return rootCmd
}

View File

@ -26,6 +26,7 @@ const (
//
StateFilename = "constellation-state.json"
ConfigFilename = "constellation-conf.yaml"
AdminConfFilename = "constellation-admin.conf"
MasterSecretFilename = "constellation-mastersecret.base64"
WGQuickConfigFilename = "wg0.conf"