Skip to content

Commit

Permalink
liqoctl install: improve handling of required flags
Browse files Browse the repository at this point in the history
  • Loading branch information
giorio94 authored and adamjensenbot committed Aug 2, 2022
1 parent e802374 commit bc9eb74
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
13 changes: 12 additions & 1 deletion cmd/liqoctl/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ func newInstallCommand(ctx context.Context, f *factory.Factory) *cobra.Command {
sharingPercentage := args.Percentage{Val: 90}
reservedSubnets := args.CIDRList{}

defaultRepoURL := "https://github.com/liqotech/liqo"

var cmd = &cobra.Command{
Use: "install",
Aliases: []string{"upgrade"},
Expand All @@ -110,6 +112,15 @@ func newInstallCommand(ctx context.Context, f *factory.Factory) *cobra.Command {
options.ClusterLabels = clusterLabels.StringMap
options.SharingPercentage = sharingPercentage.Val
options.ReservedSubnets = reservedSubnets.StringList.StringList

switch {
case options.RepoURL != defaultRepoURL && options.ChartPath != "":
options.Printer.ExitWithMessage("Cannot specify both --repo-url and --local-chart-path at the same time")
case options.ChartPath != "" && options.Version == "":
options.Printer.ExitWithMessage("A version must be explicitly specified if the --local-chart-path flag is set")
case options.RepoURL != defaultRepoURL && options.Version == "":
options.Printer.ExitWithMessage("A version must be explicitly specified if the --repo-url flag is set")
}
},

Run: func(cmd *cobra.Command, args []string) {
Expand All @@ -119,7 +130,7 @@ func newInstallCommand(ctx context.Context, f *factory.Factory) *cobra.Command {

cmd.PersistentFlags().StringVar(&options.Version, "version", "",
"The version of Liqo to be installed, among releases and commit SHAs. Defaults to the latest stable release")
cmd.PersistentFlags().StringVar(&options.RepoURL, "repo-url", "https://github.com/liqotech/liqo",
cmd.PersistentFlags().StringVar(&options.RepoURL, "repo-url", defaultRepoURL,
"The URL of the git repository used to retrieve the Helm chart, if a non released version is specified")
cmd.PersistentFlags().StringVar(&options.ChartPath, "local-chart-path", "",
"The local path used to retrieve the Helm chart, instead of the upstream one")
Expand Down
2 changes: 1 addition & 1 deletion cmd/liqoctl/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func WithTemplate(str string) string {
}

// singleClusterPersistentPreRun initializes the local factory.
func singleClusterPersistentPreRun(cmd *cobra.Command, f *factory.Factory, opts ...factory.Options) {
func singleClusterPersistentPreRun(_ *cobra.Command, f *factory.Factory, opts ...factory.Options) {
// Populate the factory fields based on the configured parameters.
f.Printer.CheckErr(f.Initialize(opts...))
}
Expand Down
24 changes: 16 additions & 8 deletions pkg/liqoctl/install/gke/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ func (o *Options) Examples() string {
return `Examples:
$ {{ .Executable }} install gke --credentials-path ~/.liqo/gcp_service_account \
--cluster-id foo --project-id bar --zone europe-west-1b
or (regional cluster)
$ {{ .Executable }} install gke --credentials-path ~/.liqo/gcp_service_account \
--cluster-id foo --project-id bar --region europe-west-1
`
}

Expand All @@ -67,7 +70,6 @@ func (o *Options) RegisterFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&o.zone, "zone", "", "The GCP zone where the cluster is running")
cmd.Flags().StringVar(&o.region, "region", "", "The GCP region where the cluster is running")

cmd.MarkFlagsMutuallyExclusive("zone", "region")
utilruntime.Must(cmd.MarkFlagRequired("credentials-path"))
utilruntime.Must(cmd.MarkFlagRequired("project-id"))
utilruntime.Must(cmd.MarkFlagRequired("cluster-id"))
Expand All @@ -81,9 +83,9 @@ func (o *Options) Initialize(ctx context.Context) error {
o.Printer.Verbosef("GKE Region: %q", o.region)
o.Printer.Verbosef("GKE ClusterID: %q", o.clusterID)

location := o.getLocation()
if location == "" {
return fmt.Errorf("zone or region must be provided")
location, err := o.getLocation()
if err != nil {
return err
}

svc, err := container.NewService(ctx, option.WithCredentialsFile(o.credentialsPath))
Expand Down Expand Up @@ -128,11 +130,17 @@ func (o *Options) checkFeatures(cluster *container.Cluster) error {
return nil
}

func (o *Options) getLocation() string {
if o.zone != "" {
return o.zone
func (o *Options) getLocation() (string, error) {
switch {
case o.zone != "" && o.region != "":
return "", fmt.Errorf("cannot specify both --zone and --region at the same time")
case o.zone != "":
return o.zone, nil
case o.region != "":
return o.region, nil
default:
return "", fmt.Errorf("either --zone or --region must be specified")
}
return o.region
}

func (o *Options) parseClusterOutput(cluster *container.Cluster) {
Expand Down
6 changes: 6 additions & 0 deletions pkg/liqoctl/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ func (p *Printer) CheckErr(err error) {
util.CheckErr(err)
}

// ExitWithMessage prints the error message and exits with a non-zero exit code.
func (p *Printer) ExitWithMessage(errmsg string) {
p.Error.Println(errmsg)
os.Exit(util.DefaultErrorExitCode)
}

// PrettyErr returns a prettified error message, according to standard kubectl style.
func PrettyErr(err error) string {
// Unwrap possible URL errors, to return the prettified message.
Expand Down

0 comments on commit bc9eb74

Please sign in to comment.