@@ -2,11 +2,7 @@ package network
22
33import (
44 "context"
5- "fmt"
6-
75 "errors"
8-
9-
106 "net"
117 "net/netip"
128 "strings"
@@ -25,7 +21,7 @@ type connectOptions struct {
2521 container string
2622 ipaddress net.IP // TODO(thaJeztah): we need a flag-type to handle netip.Addr directly
2723 ipv6address net.IP // TODO(thaJeztah): we need a flag-type to handle netip.Addr directly
28- macAddress net.HardwareAddr
24+ macAddress string // TODO(thaJeztah): we need a flag-type to handle net.HardwareAddr directly
2925 links opts.ListOpts
3026 aliases []string
3127 linklocalips []net.IP // TODO(thaJeztah): we need a flag-type to handle []netip.Addr directly
@@ -37,7 +33,6 @@ func newConnectCommand(dockerCLI command.Cli) *cobra.Command {
3733 options := connectOptions {
3834 links : opts .NewListOpts (opts .ValidateLink ),
3935 }
40- var macStr string
4136
4237 cmd := & cobra.Command {
4338 Use : "connect [OPTIONS] NETWORK CONTAINER" ,
@@ -46,15 +41,6 @@ func newConnectCommand(dockerCLI command.Cli) *cobra.Command {
4641 RunE : func (cmd * cobra.Command , args []string ) error {
4742 options .network = args [0 ]
4843 options .container = args [1 ]
49- // Validate MAC address if provided
50-
51- if macStr != "" {
52- mac , err := net .ParseMAC (macStr )
53- if err != nil {
54- return fmt .Errorf ("invalid MAC address: %q" , macStr )
55- }
56- options .macAddress = mac
57- }
5844 return runConnect (cmd .Context (), dockerCLI .Client (), options )
5945 },
6046 ValidArgsFunction : func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
@@ -75,19 +61,22 @@ func newConnectCommand(dockerCLI command.Cli) *cobra.Command {
7561 flags .IPSliceVar (& options .linklocalips , "link-local-ip" , nil , "Add a link-local address for the container" )
7662 flags .StringSliceVar (& options .driverOpts , "driver-opt" , []string {}, "driver options for the network" )
7763 flags .IntVar (& options .gwPriority , "gw-priority" , 0 , "Highest gw-priority provides the default gateway. Accepts positive and negative values." )
78- flags .StringVar (& macStr , "mac-address" , "" , "MAC address for the container on this network" )
64+ flags .StringVar (& options . macAddress , "mac-address" , "" , "MAC address for the container on this network (e.g., 92:d0:c6:0a:29:33) " )
7965 return cmd
8066}
8167
8268func runConnect (ctx context.Context , apiClient client.NetworkAPIClient , options connectOptions ) error {
8369 driverOpts , err := convertDriverOpt (options .driverOpts )
84-
8570 if err != nil {
8671 return err
8772 }
8873
89-
90-
74+ var macAddr network.HardwareAddr
75+ if options .macAddress != "" {
76+ if err := macAddr .UnmarshalText ([]byte (options .macAddress )); err != nil {
77+ return err
78+ }
79+ }
9180
9281 _ , err = apiClient .NetworkConnect (ctx , options .network , client.NetworkConnectOptions {
9382 Container : options .container ,
@@ -96,13 +85,12 @@ func runConnect(ctx context.Context, apiClient client.NetworkAPIClient, options
9685 IPv4Address : toNetipAddr (options .ipaddress ),
9786 IPv6Address : toNetipAddr (options .ipv6address ),
9887 LinkLocalIPs : toNetipAddrSlice (options .linklocalips ),
99-
10088 },
10189 Links : options .links .GetSlice (),
10290 Aliases : options .aliases ,
10391 DriverOpts : driverOpts ,
10492 GwPriority : options .gwPriority ,
105- MacAddress : network . HardwareAddr ( options . macAddress ) ,
93+ MacAddress : macAddr ,
10694 },
10795 })
10896 return err
0 commit comments