mirror of
https://github.com/podman-container-tools/podman.git
synced 2026-10-13 17:37:42 +00:00
Allow automatic generation for shell completion scripts with the internal cobra functions (requires v1.0.0+). This should replace the handwritten completion scripts and even adds support for fish. With this approach it is less likley that completions and code are out of sync. We can now create the scripts with - podman completion bash - podman completion zsh - podman completion fish To test the completion run: source <(podman completion bash) The same works for podman-remote and podman --remote and it will complete your remote containers/images with the correct endpoints values from --url/--connection. The completion logic is written in go and provided by the cobra library. The completion functions lives in `cmd/podman/completion/completion.go`. The unit test at cmd/podman/shell_completion_test.go checks if each command and flag has an autocompletion function set. This prevents that commands and flags have no shell completion set. This commit does not replace the current autocompletion scripts. Closes #6440 Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
90 lines
3 KiB
Go
90 lines
3 KiB
Go
package network
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
|
|
"github.com/containers/common/pkg/completion"
|
|
"github.com/containers/podman/v2/cmd/podman/common"
|
|
"github.com/containers/podman/v2/cmd/podman/registry"
|
|
"github.com/containers/podman/v2/libpod/define"
|
|
"github.com/containers/podman/v2/pkg/domain/entities"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var (
|
|
networkCreateDescription = `create CNI networks for containers and pods`
|
|
networkCreateCommand = &cobra.Command{
|
|
Use: "create [options] [NETWORK]",
|
|
Short: "network create",
|
|
Long: networkCreateDescription,
|
|
RunE: networkCreate,
|
|
Args: cobra.MaximumNArgs(1),
|
|
ValidArgsFunction: completion.AutocompleteNone,
|
|
Example: `podman network create podman1`,
|
|
}
|
|
)
|
|
|
|
var (
|
|
networkCreateOptions entities.NetworkCreateOptions
|
|
)
|
|
|
|
func networkCreateFlags(cmd *cobra.Command) {
|
|
flags := cmd.Flags()
|
|
|
|
driverFlagName := "driver"
|
|
flags.StringVarP(&networkCreateOptions.Driver, driverFlagName, "d", "bridge", "driver to manage the network")
|
|
_ = cmd.RegisterFlagCompletionFunc(driverFlagName, common.AutocompleteNetworkDriver)
|
|
|
|
gatewayFlagName := "gateway"
|
|
flags.IPVar(&networkCreateOptions.Gateway, gatewayFlagName, nil, "IPv4 or IPv6 gateway for the subnet")
|
|
_ = cmd.RegisterFlagCompletionFunc(gatewayFlagName, completion.AutocompleteNone)
|
|
|
|
flags.BoolVar(&networkCreateOptions.Internal, "internal", false, "restrict external access from this network")
|
|
|
|
ipRangeFlagName := "ip-range"
|
|
flags.IPNetVar(&networkCreateOptions.Range, ipRangeFlagName, net.IPNet{}, "allocate container IP from range")
|
|
_ = cmd.RegisterFlagCompletionFunc(ipRangeFlagName, completion.AutocompleteNone)
|
|
|
|
macvlanFlagName := "macvlan"
|
|
flags.StringVar(&networkCreateOptions.MacVLAN, macvlanFlagName, "", "create a Macvlan connection based on this device")
|
|
_ = cmd.RegisterFlagCompletionFunc(macvlanFlagName, completion.AutocompleteNone)
|
|
|
|
// TODO not supported yet
|
|
// flags.StringVar(&networkCreateOptions.IPamDriver, "ipam-driver", "", "IP Address Management Driver")
|
|
|
|
flags.BoolVar(&networkCreateOptions.IPv6, "ipv6", false, "enable IPv6 networking")
|
|
|
|
subnetFlagName := "subnet"
|
|
flags.IPNetVar(&networkCreateOptions.Subnet, subnetFlagName, net.IPNet{}, "subnet in CIDR format")
|
|
_ = cmd.RegisterFlagCompletionFunc(subnetFlagName, completion.AutocompleteNone)
|
|
|
|
flags.BoolVar(&networkCreateOptions.DisableDNS, "disable-dns", false, "disable dns plugin")
|
|
}
|
|
func init() {
|
|
registry.Commands = append(registry.Commands, registry.CliCommand{
|
|
Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
|
|
Command: networkCreateCommand,
|
|
Parent: networkCmd,
|
|
})
|
|
networkCreateFlags(networkCreateCommand)
|
|
|
|
}
|
|
|
|
func networkCreate(cmd *cobra.Command, args []string) error {
|
|
var (
|
|
name string
|
|
)
|
|
if len(args) > 0 {
|
|
if !define.NameRegex.MatchString(args[0]) {
|
|
return define.RegexError
|
|
}
|
|
name = args[0]
|
|
}
|
|
response, err := registry.ContainerEngine().NetworkCreate(registry.Context(), name, networkCreateOptions)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Println(response.Filename)
|
|
return nil
|
|
}
|