github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/network_inspect.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"github.com/containerd/nerdctl/pkg/api/types"
    21  	"github.com/containerd/nerdctl/pkg/cmd/network"
    22  	"github.com/spf13/cobra"
    23  )
    24  
    25  func newNetworkInspectCommand() *cobra.Command {
    26  	networkInspectCommand := &cobra.Command{
    27  		Use:               "inspect [flags] NETWORK [NETWORK, ...]",
    28  		Short:             "Display detailed information on one or more networks",
    29  		Args:              cobra.MinimumNArgs(1),
    30  		RunE:              networkInspectAction,
    31  		ValidArgsFunction: networkInspectShellComplete,
    32  		SilenceUsage:      true,
    33  		SilenceErrors:     true,
    34  	}
    35  	networkInspectCommand.Flags().String("mode", "dockercompat", `Inspect mode, "dockercompat" for Docker-compatible output, "native" for containerd-native output`)
    36  	networkInspectCommand.RegisterFlagCompletionFunc("mode", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    37  		return []string{"dockercompat", "native"}, cobra.ShellCompDirectiveNoFileComp
    38  	})
    39  	networkInspectCommand.Flags().StringP("format", "f", "", "Format the output using the given Go template, e.g, '{{json .}}'")
    40  	networkInspectCommand.RegisterFlagCompletionFunc("format", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    41  		return []string{"json"}, cobra.ShellCompDirectiveNoFileComp
    42  	})
    43  	return networkInspectCommand
    44  }
    45  
    46  func networkInspectAction(cmd *cobra.Command, args []string) error {
    47  	globalOptions, err := processRootCmdFlags(cmd)
    48  	if err != nil {
    49  		return err
    50  	}
    51  	mode, err := cmd.Flags().GetString("mode")
    52  	if err != nil {
    53  		return err
    54  	}
    55  	format, err := cmd.Flags().GetString("format")
    56  	if err != nil {
    57  		return err
    58  	}
    59  	return network.Inspect(cmd.Context(), types.NetworkInspectOptions{
    60  		GOptions: globalOptions,
    61  		Mode:     mode,
    62  		Format:   format,
    63  		Networks: args,
    64  		Stdout:   cmd.OutOrStdout(),
    65  	})
    66  }
    67  
    68  func networkInspectShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    69  	// show network names, including "bridge"
    70  	exclude := []string{"host", "none"}
    71  	return shellCompleteNetworkNames(cmd, exclude)
    72  }