Skip to content

Commit

Permalink
update use of survey apis to be compatible with v2
Browse files Browse the repository at this point in the history
two primary changes:

- validators are passed differently
- the answer type for `select` questions is now an object instead of a
string, which needs to be unrwrapped
  • Loading branch information
ecordell committed May 10, 2021
1 parent 4d32d2a commit f17dc86
Show file tree
Hide file tree
Showing 27 changed files with 102 additions and 71 deletions.
9 changes: 5 additions & 4 deletions pkg/asset/installconfig/aws/basedomain.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
"sort"
"strings"

survey "github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/core"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
survey "gopkg.in/AlecAivazis/survey.v1"
)

// IsForbidden returns true if and only if the input error is an HTTP
Expand Down Expand Up @@ -61,14 +62,14 @@ func GetBaseDomain() (string, error) {
Message: "Base Domain",
Help: "The base domain of the cluster. All DNS records will be sub-domains of this base and will also include the cluster name.\n\nIf you don't see you intended base-domain listed, create a new public Route53 hosted zone and rerun the installer.",
Options: publicZones,
}, &domain, func(ans interface{}) error {
choice := ans.(string)
}, &domain, survey.WithValidator(func(ans interface{}) error {
choice := ans.(core.OptionAnswer).Value
i := sort.SearchStrings(publicZones, choice)
if i == len(publicZones) || publicZones[i] != choice {
return errors.Errorf("invalid base domain %q", choice)
}
return nil
}); err != nil {
})); err != nil {
return "", errors.Wrap(err, "failed UserInput")
}

Expand Down
18 changes: 13 additions & 5 deletions pkg/asset/installconfig/aws/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"sort"
"strings"

survey "github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/core"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
survey "gopkg.in/AlecAivazis/survey.v1"

"github.com/openshift/installer/pkg/types/aws"
)
Expand All @@ -21,9 +22,16 @@ func Platform() (*aws.Platform, error) {
longRegions = append(longRegions, fmt.Sprintf("%s (%s)", id, location))
shortRegions = append(shortRegions, id)
}
regionTransform := survey.TransformString(func(s string) string {
return strings.SplitN(s, " ", 2)[0]
})

var regionTransform survey.Transformer = func(ans interface{}) interface{} {
switch v := ans.(type) {
case core.OptionAnswer:
return core.OptionAnswer{Value: strings.SplitN(v.Value, " ", 2)[0], Index: v.Index}
case string:
return strings.SplitN(v, " ", 2)[0]
}
return ""
}

defaultRegion := "us-east-1"
if !IsKnownRegion(defaultRegion) {
Expand Down Expand Up @@ -57,7 +65,7 @@ func Platform() (*aws.Platform, error) {
Options: longRegions,
},
Validate: survey.ComposeValidators(survey.Required, func(ans interface{}) error {
choice := regionTransform(ans).(string)
choice := regionTransform(ans).(core.OptionAnswer).Value
i := sort.SearchStrings(shortRegions, choice)
if i == len(shortRegions) || shortRegions[i] != choice {
return errors.Errorf("invalid region %q", choice)
Expand Down
2 changes: 1 addition & 1 deletion pkg/asset/installconfig/aws/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
survey "gopkg.in/AlecAivazis/survey.v1"
survey "github.com/AlecAivazis/survey/v2"
ini "gopkg.in/ini.v1"

typesaws "github.com/openshift/installer/pkg/types/aws"
Expand Down
20 changes: 13 additions & 7 deletions pkg/asset/installconfig/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"sort"
"strings"

survey "github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/core"
"github.com/Azure/go-autorest/autorest/to"
"github.com/pkg/errors"

"github.com/openshift/installer/pkg/types/azure"

"github.com/pkg/errors"
survey "gopkg.in/AlecAivazis/survey.v1"
)

const (
Expand Down Expand Up @@ -51,9 +51,15 @@ func Platform() (*azure.Platform, error) {
}
}

regionTransform := survey.TransformString(func(s string) string {
return strings.SplitN(s, " ", 2)[0]
})
var regionTransform survey.Transformer = func(ans interface{}) interface{} {
switch v := ans.(type) {
case core.OptionAnswer:
return core.OptionAnswer{Value: strings.SplitN(v.Value, " ", 2)[0], Index: v.Index}
case string:
return strings.SplitN(v, " ", 2)[0]
}
return ""
}

_, ok := regions[defaultRegion]
if !ok {
Expand All @@ -73,7 +79,7 @@ func Platform() (*azure.Platform, error) {
Options: longRegions,
},
Validate: survey.ComposeValidators(survey.Required, func(ans interface{}) error {
choice := regionTransform(ans).(string)
choice := regionTransform(ans).(core.OptionAnswer).Value
i := sort.SearchStrings(shortRegions, choice)
if i == len(shortRegions) || shortRegions[i] != choice {
return errors.Errorf("invalid region %q", choice)
Expand Down
2 changes: 1 addition & 1 deletion pkg/asset/installconfig/azure/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
azdns "github.com/Azure/azure-sdk-for-go/profiles/latest/dns/mgmt/dns"
"github.com/Azure/go-autorest/autorest/to"
"github.com/pkg/errors"
survey "gopkg.in/AlecAivazis/survey.v1"
survey "github.com/AlecAivazis/survey/v2"
)

//DNSConfig exposes functions to choose the DNS settings
Expand Down
2 changes: 1 addition & 1 deletion pkg/asset/installconfig/azure/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/Azure/go-autorest/autorest/azure/auth"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"gopkg.in/AlecAivazis/survey.v1"
"github.com/AlecAivazis/survey/v2"

"github.com/openshift/installer/pkg/types/azure"
)
Expand Down
5 changes: 3 additions & 2 deletions pkg/asset/installconfig/baremetal/baremetal.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package baremetal

import (
"fmt"

"github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/terminal"
"github.com/pkg/errors"
"gopkg.in/AlecAivazis/survey.v1"
"gopkg.in/AlecAivazis/survey.v1/terminal"

"github.com/openshift/installer/pkg/ipnet"
"github.com/openshift/installer/pkg/types/baremetal"
Expand Down
2 changes: 1 addition & 1 deletion pkg/asset/installconfig/baremetal/host.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package baremetal

import (
"gopkg.in/AlecAivazis/survey.v1"
"github.com/AlecAivazis/survey/v2"

"github.com/openshift/installer/pkg/types/baremetal"
"github.com/openshift/installer/pkg/validate"
Expand Down
2 changes: 1 addition & 1 deletion pkg/asset/installconfig/basedomain.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package installconfig

import (
survey "github.com/AlecAivazis/survey/v2"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/pkg/errors"
survey "gopkg.in/AlecAivazis/survey.v1"

"github.com/openshift/installer/pkg/asset"
awsconfig "github.com/openshift/installer/pkg/asset/installconfig/aws"
Expand Down
2 changes: 1 addition & 1 deletion pkg/asset/installconfig/clustername.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package installconfig

import (
survey "github.com/AlecAivazis/survey/v2"
"github.com/pkg/errors"
survey "gopkg.in/AlecAivazis/survey.v1"

"github.com/openshift/installer/pkg/asset"
"github.com/openshift/installer/pkg/types"
Expand Down
9 changes: 5 additions & 4 deletions pkg/asset/installconfig/gcp/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"sort"
"time"

survey "github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/core"
"github.com/pkg/errors"
dns "google.golang.org/api/dns/v1"
"google.golang.org/api/googleapi"
survey "gopkg.in/AlecAivazis/survey.v1"
)

// GetPublicZone returns a DNS managed zone from the provided project which matches the baseDomain
Expand Down Expand Up @@ -51,14 +52,14 @@ func GetBaseDomain(project string) (string, error) {
Message: "Base Domain",
Help: "The base domain of the cluster. All DNS records will be sub-domains of this base and will also include the cluster name.\n\nIf you don't see you intended base-domain listed, create a new public hosted zone and rerun the installer.",
Options: publicZones,
}, &domain, func(ans interface{}) error {
choice := ans.(string)
}, &domain, survey.WithValidator(func(ans interface{}) error {
choice := ans.(core.OptionAnswer).Value
i := sort.SearchStrings(publicZones, choice)
if i == len(publicZones) || publicZones[i] != choice {
return errors.Errorf("invalid base domain %q", choice)
}
return nil
}); err != nil {
})); err != nil {
return "", errors.Wrap(err, "failed UserInput")
}

Expand Down
20 changes: 14 additions & 6 deletions pkg/asset/installconfig/gcp/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
"strings"
"time"

"gopkg.in/AlecAivazis/survey.v1"
"github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/core"
"github.com/pkg/errors"

"github.com/openshift/installer/pkg/types/gcp"
"github.com/openshift/installer/pkg/types/gcp/validation"
"github.com/pkg/errors"
)

// Platform collects GCP-specific configuration.
Expand Down Expand Up @@ -88,9 +89,16 @@ func selectRegion(project string) (string, error) {
longRegions = append(longRegions, fmt.Sprintf("%s (%s)", id, location))
shortRegions = append(shortRegions, id)
}
regionTransform := survey.TransformString(func(s string) string {
return strings.SplitN(s, " ", 2)[0]
})

var regionTransform survey.Transformer = func(ans interface{}) interface{} {
switch v := ans.(type) {
case core.OptionAnswer:
return core.OptionAnswer{Value: strings.SplitN(v.Value, " ", 2)[0], Index: v.Index}
case string:
return strings.SplitN(v, " ", 2)[0]
}
return ""
}

sort.Strings(longRegions)
sort.Strings(shortRegions)
Expand All @@ -106,7 +114,7 @@ func selectRegion(project string) (string, error) {
Options: longRegions,
},
Validate: survey.ComposeValidators(survey.Required, func(ans interface{}) error {
choice := regionTransform(ans).(string)
choice := regionTransform(ans).(core.OptionAnswer).Value
i := sort.SearchStrings(shortRegions, choice)
if i == len(shortRegions) || shortRegions[i] != choice {
return errors.Errorf("invalid region %q", choice)
Expand Down
2 changes: 1 addition & 1 deletion pkg/asset/installconfig/gcp/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/sirupsen/logrus"
googleoauth "golang.org/x/oauth2/google"
compute "google.golang.org/api/compute/v1"
"gopkg.in/AlecAivazis/survey.v1"
"github.com/AlecAivazis/survey/v2"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion pkg/asset/installconfig/kubevirt/kubevirt.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package kubevirt

import (
"gopkg.in/AlecAivazis/survey.v1"
"github.com/AlecAivazis/survey/v2"

"github.com/openshift/installer/pkg/types/kubevirt"
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/asset/installconfig/libvirt/libvirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package libvirt

import (
"github.com/pkg/errors"
survey "gopkg.in/AlecAivazis/survey.v1"
survey "github.com/AlecAivazis/survey/v2"

"github.com/openshift/installer/pkg/types/libvirt"
libvirtdefaults "github.com/openshift/installer/pkg/types/libvirt/defaults"
Expand Down
2 changes: 1 addition & 1 deletion pkg/asset/installconfig/networking.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package installconfig

import (
survey "gopkg.in/AlecAivazis/survey.v1"
survey "github.com/AlecAivazis/survey/v2"

"github.com/openshift/installer/pkg/asset"
"github.com/openshift/installer/pkg/ipnet"
Expand Down
11 changes: 6 additions & 5 deletions pkg/asset/installconfig/openstack/openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"sort"
"strings"

survey "github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/core"
"github.com/pkg/errors"
survey "gopkg.in/AlecAivazis/survey.v1"

"github.com/openshift/installer/pkg/types/openstack"
)
Expand All @@ -32,7 +33,7 @@ func Platform() (*openstack.Platform, error) {
Options: cloudNames,
},
Validate: survey.ComposeValidators(survey.Required, func(ans interface{}) error {
value := ans.(string)
value := ans.(core.OptionAnswer).Value
i := sort.SearchStrings(cloudNames, value)
if i == len(cloudNames) || cloudNames[i] != value {
return errors.Errorf("invalid cloud name %q, should be one of %+v", value, strings.Join(cloudNames, ", "))
Expand Down Expand Up @@ -61,7 +62,7 @@ func Platform() (*openstack.Platform, error) {
Default: noExtNet,
},
Validate: survey.ComposeValidators(survey.Required, func(ans interface{}) error {
value := ans.(string)
value := ans.(core.OptionAnswer).Value
i := sort.SearchStrings(networkNames, value)
if i == len(networkNames) || networkNames[i] != value {
return errors.Errorf("invalid network name %q, should be one of %+v", value, strings.Join(networkNames, ", "))
Expand Down Expand Up @@ -92,7 +93,7 @@ func Platform() (*openstack.Platform, error) {
Options: floatingIPNames,
},
Validate: survey.ComposeValidators(survey.Required, func(ans interface{}) error {
value := ans.(string)
value := ans.(core.OptionAnswer).Value
i := sort.SearchStrings(floatingIPNames, value)
if i == len(floatingIPNames) || floatingIPNames[i] != value {
return errors.Errorf("invalid floating IP %q, should be one of %+v", value, strings.Join(floatingIPNames, ", "))
Expand Down Expand Up @@ -120,7 +121,7 @@ func Platform() (*openstack.Platform, error) {
Options: flavorNames,
},
Validate: survey.ComposeValidators(survey.Required, func(ans interface{}) error {
value := ans.(string)
value := ans.(core.OptionAnswer).Value
i := sort.SearchStrings(flavorNames, value)
if i == len(flavorNames) || flavorNames[i] != value {
return errors.Errorf("invalid flavor name %q, should be one of %+v", value, strings.Join(flavorNames, ", "))
Expand Down
9 changes: 5 additions & 4 deletions pkg/asset/installconfig/ovirt/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"fmt"
"sort"

"github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/core"
ovirtsdk4 "github.com/ovirt/go-ovirt"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"gopkg.in/AlecAivazis/survey.v1"

"github.com/openshift/installer/pkg/types/ovirt"
)
Expand Down Expand Up @@ -44,8 +45,8 @@ func askCluster(c *ovirtsdk4.Connection, p *ovirt.Platform) (string, error) {
Options: clusterNames,
},
&clusterName,
func(ans interface{}) error {
choice := ans.(string)
survey.WithValidator(func(ans interface{}) error {
choice := ans.(core.OptionAnswer).Value
sort.Strings(clusterNames)
i := sort.SearchStrings(clusterNames, choice)
if i == len(clusterNames) || clusterNames[i] != choice {
Expand All @@ -57,7 +58,7 @@ func askCluster(c *ovirtsdk4.Connection, p *ovirt.Platform) (string, error) {
}
p.ClusterID = cl.MustId()
return nil
}); err != nil {
})); err != nil {
return clusterName, errors.Wrap(err, "failed UserInput")
}
return clusterName, nil
Expand Down

0 comments on commit f17dc86

Please sign in to comment.