Skip to content

Commit

Permalink
error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Oct 28, 2024
1 parent a8f850f commit 1c03a27
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
9 changes: 6 additions & 3 deletions cmd/private_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"net"
"fmt"
"strings"

"github.com/spf13/cobra"
Expand All @@ -14,15 +15,15 @@ var privateNetworkCmd = &cobra.Command{
Aliases: []string{"privnet"},
}

func processPrivateNetworkOptions(options []string) *v3.PrivateNetworkOptions {
func processPrivateNetworkOptions(options []string) (*v3.PrivateNetworkOptions, error) {
opts := &v3.PrivateNetworkOptions{}
optionsMap := make(map[string][]string)

// Process each option flag
for _, opt := range options {
keyValue := strings.SplitN(opt, "=", 2)
if len(keyValue) != 2 {
continue
return nil, fmt.Errorf("malformed option %q: must be in format key=\"value1 value2\"", opt)
}
key := keyValue[0]
values := strings.Split(keyValue[1], " ")
Expand Down Expand Up @@ -52,9 +53,11 @@ func processPrivateNetworkOptions(options []string) *v3.PrivateNetworkOptions {
}
case "domain-search":
opts.DomainSearch = values
default:
return nil, fmt.Errorf("unrecognized option key %q: supported keys are: 'dns-servers', 'ntp-servers', 'routers', 'domain-search'", key)
}
}
return opts
return opts, nil
}

func init() {
Expand Down
6 changes: 5 additions & 1 deletion cmd/private_network_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ func (c *privateNetworkCreateCmd) cmdRun(_ *cobra.Command, _ []string) error {
}

if len(c.Option) > 0 {
req.Options = processPrivateNetworkOptions(c.Option)
opts, err := processPrivateNetworkOptions(c.Option)
if err != nil {
return err
}
req.Options = opts
}

op, err := client.CreatePrivateNetwork(ctx, req)
Expand Down
6 changes: 5 additions & 1 deletion cmd/private_network_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ func (c *privateNetworkUpdateCmd) cmdRun(cmd *cobra.Command, _ []string) error {
}

if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.Option)) {
updateReq.Options = processPrivateNetworkOptions(c.Option)
opts, err := processPrivateNetworkOptions(c.Option)
if err != nil {
return err
}
updateReq.Options = opts
updated = true
}

Expand Down

0 comments on commit 1c03a27

Please sign in to comment.