Move cli/cmd into cli/internal

This commit is contained in:
katexochen 2022-06-08 08:14:28 +02:00
parent d71e97a940
commit c3ebd3d3cd
34 changed files with 45 additions and 32 deletions

View file

@ -6,6 +6,7 @@ import (
"os" "os"
"os/signal" "os/signal"
"github.com/edgelesssys/constellation/cli/internal/cmd"
"github.com/edgelesssys/constellation/internal/constants" "github.com/edgelesssys/constellation/internal/constants"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -34,13 +35,13 @@ func NewRootCmd() *cobra.Command {
rootCmd.PersistentFlags().String("config", constants.ConfigFilename, "path to the configuration file") rootCmd.PersistentFlags().String("config", constants.ConfigFilename, "path to the configuration file")
must(rootCmd.MarkPersistentFlagFilename("config", "json")) must(rootCmd.MarkPersistentFlagFilename("config", "json"))
rootCmd.AddCommand(newConfigCmd()) rootCmd.AddCommand(cmd.NewConfigCmd())
rootCmd.AddCommand(newCreateCmd()) rootCmd.AddCommand(cmd.NewCreateCmd())
rootCmd.AddCommand(newInitCmd()) rootCmd.AddCommand(cmd.NewInitCmd())
rootCmd.AddCommand(newVerifyCmd()) rootCmd.AddCommand(cmd.NewVerifyCmd())
rootCmd.AddCommand(newRecoverCmd()) rootCmd.AddCommand(cmd.NewRecoverCmd())
rootCmd.AddCommand(newTerminateCmd()) rootCmd.AddCommand(cmd.NewTerminateCmd())
rootCmd.AddCommand(newVersionCmd()) rootCmd.AddCommand(cmd.NewVersionCmd())
return rootCmd return rootCmd
} }

View file

@ -4,7 +4,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
func newConfigCmd() *cobra.Command { func NewConfigCmd() *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "config", Use: "config",
Short: "Work with the Constellation configuration file", Short: "Work with the Constellation configuration file",

View file

@ -15,7 +15,8 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
func newCreateCmd() *cobra.Command { // NewCreateCmd returns a new cobra.Command for the create command.
func NewCreateCmd() *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "create {aws|azure|gcp}", Use: "create {aws|azure|gcp}",
Short: "Create instances on a cloud platform for your Constellation cluster", Short: "Create instances on a cloud platform for your Constellation cluster",
@ -208,6 +209,12 @@ func createCompletion(cmd *cobra.Command, args []string, toComplete string) ([]s
} }
} }
func must(err error) {
if err != nil {
panic(err)
}
}
func instanceTypeCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { func instanceTypeCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 1 { if len(args) != 1 {
return []string{}, cobra.ShellCompDirectiveError return []string{}, cobra.ShellCompDirectiveError

View file

@ -34,7 +34,7 @@ func TestCreateArgumentValidation(t *testing.T) {
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
err := newCreateCmd().ValidateArgs(tc.args) err := NewCreateCmd().ValidateArgs(tc.args)
if tc.wantErr { if tc.wantErr {
assert.Error(err) assert.Error(err)
@ -225,7 +225,7 @@ func TestCreate(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
require := require.New(t) require := require.New(t)
cmd := newCreateCmd() cmd := NewCreateCmd()
cmd.SetOut(&bytes.Buffer{}) cmd.SetOut(&bytes.Buffer{})
cmd.SetErr(&bytes.Buffer{}) cmd.SetErr(&bytes.Buffer{})
cmd.SetIn(bytes.NewBufferString(tc.stdin)) cmd.SetIn(bytes.NewBufferString(tc.stdin))

View file

@ -35,7 +35,8 @@ import (
"golang.zx2c4.com/wireguard/wgctrl/wgtypes" "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
) )
func newInitCmd() *cobra.Command { // NewInitCmd returns a new cobra.Command for the init command.
func NewInitCmd() *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "init", Use: "init",
Short: "Initialize the Constellation cluster", Short: "Initialize the Constellation cluster",

View file

@ -24,7 +24,7 @@ import (
func TestInitArgumentValidation(t *testing.T) { func TestInitArgumentValidation(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
cmd := newInitCmd() cmd := NewInitCmd()
assert.NoError(cmd.ValidateArgs(nil)) assert.NoError(cmd.ValidateArgs(nil))
assert.Error(cmd.ValidateArgs([]string{"something"})) assert.Error(cmd.ValidateArgs([]string{"something"}))
assert.Error(cmd.ValidateArgs([]string{"sth", "sth"})) assert.Error(cmd.ValidateArgs([]string{"sth", "sth"}))
@ -285,7 +285,7 @@ func TestInitialize(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
require := require.New(t) require := require.New(t)
cmd := newInitCmd() cmd := NewInitCmd()
var out bytes.Buffer var out bytes.Buffer
cmd.SetOut(&out) cmd.SetOut(&out)
var errOut bytes.Buffer var errOut bytes.Buffer
@ -592,7 +592,7 @@ func TestAutoscaleFlag(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
require := require.New(t) require := require.New(t)
cmd := newInitCmd() cmd := NewInitCmd()
var out bytes.Buffer var out bytes.Buffer
cmd.SetOut(&out) cmd.SetOut(&out)
var errOut bytes.Buffer var errOut bytes.Buffer

View file

@ -20,7 +20,8 @@ import (
var diskUUIDRegexp = regexp.MustCompile("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$") var diskUUIDRegexp = regexp.MustCompile("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$")
func newRecoverCmd() *cobra.Command { // NewRecoverCmd returns a new cobra.Command for the recover command.
func NewRecoverCmd() *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "recover", Use: "recover",
Short: "Recover a completely stopped Constellation cluster", Short: "Recover a completely stopped Constellation cluster",

View file

@ -27,7 +27,7 @@ func TestRecoverCmdArgumentValidation(t *testing.T) {
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
cmd := newRecoverCmd() cmd := NewRecoverCmd()
err := cmd.ValidateArgs(tc.args) err := cmd.ValidateArgs(tc.args)
if tc.wantErr { if tc.wantErr {
@ -160,7 +160,7 @@ func TestRecover(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
require := require.New(t) require := require.New(t)
cmd := newRecoverCmd() cmd := NewRecoverCmd()
cmd.Flags().String("config", "", "") // register persisten flag manually cmd.Flags().String("config", "", "") // register persisten flag manually
out := &bytes.Buffer{} out := &bytes.Buffer{}
cmd.SetOut(out) cmd.SetOut(out)
@ -255,7 +255,7 @@ func TestParseRecoverFlags(t *testing.T) {
fs := afero.NewMemMapFs() fs := afero.NewMemMapFs()
require.NoError(afero.WriteFile(fs, "constellation-mastersecret.base64", []byte("Y29uc3RlbGxhdGlvbi1tYXN0ZXItc2VjcmV0LWxlbmc="), 0o777)) require.NoError(afero.WriteFile(fs, "constellation-mastersecret.base64", []byte("Y29uc3RlbGxhdGlvbi1tYXN0ZXItc2VjcmV0LWxlbmc="), 0o777))
cmd := newRecoverCmd() cmd := NewRecoverCmd()
cmd.Flags().String("config", "", "") // register persistent flag manually cmd.Flags().String("config", "", "") // register persistent flag manually
require.NoError(cmd.ParseFlags(tc.args)) require.NoError(cmd.ParseFlags(tc.args))
flags, err := parseRecoverFlags(cmd, file.NewHandler(fs)) flags, err := parseRecoverFlags(cmd, file.NewHandler(fs))

View file

@ -15,7 +15,8 @@ import (
"github.com/edgelesssys/constellation/internal/state" "github.com/edgelesssys/constellation/internal/state"
) )
func newTerminateCmd() *cobra.Command { // NewTerminateCmd returns a new cobra.Command for the terminate command.
func NewTerminateCmd() *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "terminate", Use: "terminate",
Short: "Terminate a Constellation cluster", Short: "Terminate a Constellation cluster",

View file

@ -27,7 +27,7 @@ func TestTerminateCmdArgumentValidation(t *testing.T) {
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
cmd := newTerminateCmd() cmd := NewTerminateCmd()
err := cmd.ValidateArgs(tc.args) err := cmd.ValidateArgs(tc.args)
if tc.wantErr { if tc.wantErr {
@ -105,7 +105,7 @@ func TestTerminate(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
require := require.New(t) require := require.New(t)
cmd := newTerminateCmd() cmd := NewTerminateCmd()
cmd.SetOut(&bytes.Buffer{}) cmd.SetOut(&bytes.Buffer{})
cmd.SetErr(&bytes.Buffer{}) cmd.SetErr(&bytes.Buffer{})

View file

@ -15,7 +15,8 @@ import (
rpcStatus "google.golang.org/grpc/status" rpcStatus "google.golang.org/grpc/status"
) )
func newVerifyCmd() *cobra.Command { // NewVerifyCmd returns a new cobra.Command for the verify command.
func NewVerifyCmd() *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "verify {aws|azure|gcp}", Use: "verify {aws|azure|gcp}",
Short: "Verify the confidential properties of a Constellation cluster", Short: "Verify the confidential properties of a Constellation cluster",

View file

@ -32,7 +32,7 @@ func TestVerifyCmdArgumentValidation(t *testing.T) {
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
cmd := newVerifyCmd() cmd := NewVerifyCmd()
err := cmd.ValidateArgs(tc.args) err := cmd.ValidateArgs(tc.args)
if tc.wantErr { if tc.wantErr {
@ -132,7 +132,7 @@ func TestVerify(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
require := require.New(t) require := require.New(t)
cmd := newVerifyCmd() cmd := NewVerifyCmd()
cmd.Flags().String("config", "", "") // register persisten flag manually cmd.Flags().String("config", "", "") // register persisten flag manually
out := &bytes.Buffer{} out := &bytes.Buffer{}
cmd.SetOut(out) cmd.SetOut(out)

View file

@ -5,7 +5,8 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
func newVersionCmd() *cobra.Command { // NewVerifyCmd returns a new cobra.Command for the verify command.
func NewVersionCmd() *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "version", Use: "version",
Short: "Display version of this CLI", Short: "Display version of this CLI",

View file

@ -12,7 +12,7 @@ import (
func TestVersionCmd(t *testing.T) { func TestVersionCmd(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
cmd := newVersionCmd() cmd := NewVersionCmd()
b := &bytes.Buffer{} b := &bytes.Buffer{}
cmd.SetOut(b) cmd.SetOut(b)

View file

@ -7,7 +7,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"github.com/edgelesssys/constellation/cli/qemu" "github.com/edgelesssys/constellation/internal/cloud/cloudtypes"
"github.com/edgelesssys/constellation/internal/state" "github.com/edgelesssys/constellation/internal/state"
) )
@ -41,17 +41,17 @@ func transformState(tfOut terraformOutput) state.ConstellationState {
Name: "qemu", Name: "qemu",
UID: "debug", UID: "debug",
CloudProvider: "qemu", CloudProvider: "qemu",
QEMUNodes: qemu.Instances{}, QEMUNodes: cloudtypes.Instances{},
QEMUCoordinators: qemu.Instances{}, QEMUCoordinators: cloudtypes.Instances{},
} }
for i, ip := range tfOut.ControlPlaneIPs.Value { for i, ip := range tfOut.ControlPlaneIPs.Value {
conState.QEMUCoordinators[fmt.Sprintf("control-plane-%d", i)] = qemu.Instance{ conState.QEMUCoordinators[fmt.Sprintf("control-plane-%d", i)] = cloudtypes.Instance{
PublicIP: ip, PublicIP: ip,
PrivateIP: ip, PrivateIP: ip,
} }
} }
for i, ip := range tfOut.WorkerIPs.Value { for i, ip := range tfOut.WorkerIPs.Value {
conState.QEMUNodes[fmt.Sprintf("worker-%d", i)] = qemu.Instance{ conState.QEMUNodes[fmt.Sprintf("worker-%d", i)] = cloudtypes.Instance{
PublicIP: ip, PublicIP: ip,
PrivateIP: ip, PrivateIP: ip,
} }