Skip to content

Commit

Permalink
feat(net): Expose driver in all subcommands
Browse files Browse the repository at this point in the history
This allows the subcommands to be called programatically
without generating errors.

Signed-off-by: Luca Seritan <[email protected]>
  • Loading branch information
LucaSeri committed Nov 28, 2023
1 parent ed02f18 commit e3a3a78
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 21 deletions.
8 changes: 4 additions & 4 deletions internal/cli/kraft/net/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
)

type CreateOptions struct {
driver string
Driver string `noattribute:"true"`
Network string `long:"network" short:"n" usage:"Set the gateway IP address and the subnet of the network in CIDR format."`
}

Expand Down Expand Up @@ -51,7 +51,7 @@ func NewCmd() *cobra.Command {
}

func (opts *CreateOptions) Pre(cmd *cobra.Command, _ []string) error {
opts.driver = cmd.Flag("driver").Value.String()
opts.Driver = cmd.Flag("driver").Value.String()

// TODO(nderjung): A future implementation can list existing networks and
// generate new subnet and gateway appropriately. Simply calculate a new
Expand All @@ -72,9 +72,9 @@ func (opts *CreateOptions) Pre(cmd *cobra.Command, _ []string) error {
func (opts *CreateOptions) Run(ctx context.Context, args []string) error {
var err error

strategy, ok := network.Strategies()[opts.driver]
strategy, ok := network.Strategies()[opts.Driver]
if !ok {
return fmt.Errorf("unsupported network driver strategy: %v (contributions welcome!)", opts.driver)
return fmt.Errorf("unsupported network driver strategy: %v (contributions welcome!)", opts.Driver)
}

controller, err := strategy.NewNetworkV1alpha1(ctx)
Expand Down
8 changes: 4 additions & 4 deletions internal/cli/kraft/net/down/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

type DownOptions struct {
driver string
Driver string `noattribute:"true"`
}

// Down brings a local machine network offline.
Expand Down Expand Up @@ -48,14 +48,14 @@ func NewCmd() *cobra.Command {
}

func (opts *DownOptions) Pre(cmd *cobra.Command, _ []string) error {
opts.driver = cmd.Flag("driver").Value.String()
opts.Driver = cmd.Flag("driver").Value.String()
return nil
}

func (opts *DownOptions) Run(ctx context.Context, args []string) error {
strategy, ok := network.Strategies()[opts.driver]
strategy, ok := network.Strategies()[opts.Driver]
if !ok {
return fmt.Errorf("unsupported network driver strategy: %v (contributions welcome!)", opts.driver)
return fmt.Errorf("unsupported network driver strategy: %v (contributions welcome!)", opts.Driver)
}

controller, err := strategy.NewNetworkV1alpha1(ctx)
Expand Down
10 changes: 5 additions & 5 deletions internal/cli/kraft/net/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (
)

type ListOptions struct {
Driver string `noattribute:"true"`
Long bool `long:"long" short:"l" usage:"Show more information"`
Output string `long:"output" short:"o" usage:"Set output format" default:"table"`
driver string
}

func NewCmd() *cobra.Command {
Expand All @@ -43,16 +43,16 @@ func NewCmd() *cobra.Command {
}

func (opts *ListOptions) Pre(cmd *cobra.Command, _ []string) error {
opts.driver = cmd.Flag("driver").Value.String()
opts.Driver = cmd.Flag("driver").Value.String()
return nil
}

func (opts *ListOptions) Run(ctx context.Context, _ []string) error {
var err error

strategy, ok := network.Strategies()[opts.driver]
strategy, ok := network.Strategies()[opts.Driver]
if !ok {
return fmt.Errorf("unsupported network driver strategy: %s", opts.driver)
return fmt.Errorf("unsupported network driver strategy: %s", opts.Driver)
}

controller, err := strategy.NewNetworkV1alpha1(ctx)
Expand Down Expand Up @@ -84,7 +84,7 @@ func (opts *ListOptions) Run(ctx context.Context, _ []string) error {
id: string(network.UID),
name: network.Name,
network: addr.String(),
driver: opts.driver,
driver: opts.Driver,
status: network.Status.State,
})

Expand Down
8 changes: 4 additions & 4 deletions internal/cli/kraft/net/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

type RemoveOptions struct {
driver string
Driver string `noattribute:"true"`
}

// Remove a local machine network.
Expand Down Expand Up @@ -48,16 +48,16 @@ func NewCmd() *cobra.Command {
}

func (opts *RemoveOptions) Pre(cmd *cobra.Command, _ []string) error {
opts.driver = cmd.Flag("driver").Value.String()
opts.Driver = cmd.Flag("driver").Value.String()
return nil
}

func (opts *RemoveOptions) Run(ctx context.Context, args []string) error {
var err error

strategy, ok := network.Strategies()[opts.driver]
strategy, ok := network.Strategies()[opts.Driver]
if !ok {
return fmt.Errorf("unsupported network driver strategy: %v (contributions welcome!)", opts.driver)
return fmt.Errorf("unsupported network driver strategy: %v (contributions welcome!)", opts.Driver)
}

controller, err := strategy.NewNetworkV1alpha1(ctx)
Expand Down
8 changes: 4 additions & 4 deletions internal/cli/kraft/net/up/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

type UpOptions struct {
driver string
Driver string `noattribute:"true"`
}

// Up brings a local machine network online.
Expand Down Expand Up @@ -48,14 +48,14 @@ func NewCmd() *cobra.Command {
}

func (opts *UpOptions) Pre(cmd *cobra.Command, _ []string) error {
opts.driver = cmd.Flag("driver").Value.String()
opts.Driver = cmd.Flag("driver").Value.String()
return nil
}

func (opts *UpOptions) Run(ctx context.Context, args []string) error {
strategy, ok := network.Strategies()[opts.driver]
strategy, ok := network.Strategies()[opts.Driver]
if !ok {
return fmt.Errorf("unsupported network driver strategy: %v (contributions welcome!)", opts.driver)
return fmt.Errorf("unsupported network driver strategy: %v (contributions welcome!)", opts.Driver)
}

controller, err := strategy.NewNetworkV1alpha1(ctx)
Expand Down

0 comments on commit e3a3a78

Please sign in to comment.