github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/network_list.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 newNetworkLsCommand() *cobra.Command {
    26  	cmd := &cobra.Command{
    27  		Use:           "ls",
    28  		Aliases:       []string{"list"},
    29  		Short:         "List networks",
    30  		Args:          cobra.NoArgs,
    31  		RunE:          networkLsAction,
    32  		SilenceUsage:  true,
    33  		SilenceErrors: true,
    34  	}
    35  	cmd.Flags().BoolP("quiet", "q", false, "Only display network IDs")
    36  	cmd.Flags().StringSliceP("filter", "f", []string{}, "Provide filter values (e.g. \"name=default\")")
    37  	cmd.Flags().String("format", "", "Format the output using the given Go template, e.g, '{{json .}}'")
    38  	cmd.RegisterFlagCompletionFunc("format", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    39  		return []string{"json", "table", "wide"}, cobra.ShellCompDirectiveNoFileComp
    40  	})
    41  	return cmd
    42  }
    43  
    44  func networkLsAction(cmd *cobra.Command, args []string) error {
    45  	globalOptions, err := processRootCmdFlags(cmd)
    46  	if err != nil {
    47  		return err
    48  	}
    49  	quiet, err := cmd.Flags().GetBool("quiet")
    50  	if err != nil {
    51  		return err
    52  	}
    53  	format, err := cmd.Flags().GetString("format")
    54  	if err != nil {
    55  		return err
    56  	}
    57  	filters, err := cmd.Flags().GetStringSlice("filter")
    58  	if err != nil {
    59  		return err
    60  	}
    61  	return network.List(cmd.Context(), types.NetworkListOptions{
    62  		GOptions: globalOptions,
    63  		Quiet:    quiet,
    64  		Format:   format,
    65  		Filters:  filters,
    66  		Stdout:   cmd.OutOrStdout(),
    67  	})
    68  }