constellation/cli/cmd/validargs.go

68 lines
1.7 KiB
Go
Raw Normal View History

package cmd
import (
2022-04-13 11:01:38 +00:00
"errors"
"fmt"
"net"
"strconv"
"strings"
"github.com/edgelesssys/constellation/cli/azure"
2022-04-13 11:01:38 +00:00
"github.com/edgelesssys/constellation/cli/cloudprovider"
"github.com/edgelesssys/constellation/cli/gcp"
"github.com/spf13/cobra"
)
2022-04-13 11:01:38 +00:00
// warnAWS warns that AWS isn't supported.
func warnAWS(providerPos int) cobra.PositionalArgs {
2022-04-04 13:55:58 +00:00
return func(cmd *cobra.Command, args []string) error {
2022-04-13 11:01:38 +00:00
if cloudprovider.FromString(args[providerPos]) == cloudprovider.AWS {
return errors.New("AWS isn't supported")
2022-04-04 14:44:15 +00:00
}
return nil
2022-04-04 13:55:58 +00:00
}
2022-04-04 14:44:15 +00:00
}
func isCloudProvider(arg int) cobra.PositionalArgs {
return func(cmd *cobra.Command, args []string) error {
if provider := cloudprovider.FromString(args[arg]); provider == cloudprovider.Unknown {
return fmt.Errorf("argument %s isn't a valid cloud provider", args[arg])
}
return nil
}
}
func validInstanceTypeForProvider(insType string, provider cloudprovider.Provider) error {
switch provider {
case cloudprovider.GCP:
for _, instanceType := range gcp.InstanceTypes {
if insType == instanceType {
return nil
}
}
return fmt.Errorf("%s isn't a valid GCP instance type", insType)
case cloudprovider.Azure:
for _, instanceType := range azure.InstanceTypes {
if insType == instanceType {
return nil
}
}
return fmt.Errorf("%s isn't a valid Azure instance type", insType)
default:
return fmt.Errorf("%s isn't a valid cloud platform", provider)
2022-04-04 13:55:58 +00:00
}
}
func validateEndpoint(endpoint string, defaultPort int) (string, error) {
_, _, err := net.SplitHostPort(endpoint)
if err == nil {
return endpoint, nil
}
if strings.Contains(err.Error(), "missing port in address") {
return net.JoinHostPort(endpoint, strconv.Itoa(defaultPort)), nil
}
return "", err
}