github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/namespace_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/clientutil"
    22  	"github.com/containerd/nerdctl/pkg/cmd/namespace"
    23  	"github.com/spf13/cobra"
    24  )
    25  
    26  func newNamespaceInspectCommand() *cobra.Command {
    27  	namespaceInspectCommand := &cobra.Command{
    28  		Use:           "inspect NAMESPACE",
    29  		Short:         "Display detailed information on one or more namespaces.",
    30  		RunE:          labelInspectAction,
    31  		Args:          cobra.MinimumNArgs(1),
    32  		SilenceUsage:  true,
    33  		SilenceErrors: true,
    34  	}
    35  	namespaceInspectCommand.Flags().StringP("format", "f", "", "Format the output using the given Go template, e.g, '{{json .}}'")
    36  	namespaceInspectCommand.RegisterFlagCompletionFunc("format", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    37  		return []string{"json"}, cobra.ShellCompDirectiveNoFileComp
    38  	})
    39  	return namespaceInspectCommand
    40  }
    41  
    42  func processNamespaceInspectOptions(cmd *cobra.Command) (types.NamespaceInspectOptions, error) {
    43  	globalOptions, err := processRootCmdFlags(cmd)
    44  	if err != nil {
    45  		return types.NamespaceInspectOptions{}, err
    46  	}
    47  	format, err := cmd.Flags().GetString("format")
    48  	if err != nil {
    49  		return types.NamespaceInspectOptions{}, err
    50  	}
    51  	return types.NamespaceInspectOptions{
    52  		GOptions: globalOptions,
    53  		Format:   format,
    54  		Stdout:   cmd.OutOrStdout(),
    55  	}, nil
    56  }
    57  
    58  func labelInspectAction(cmd *cobra.Command, args []string) error {
    59  	options, err := processNamespaceInspectOptions(cmd)
    60  	if err != nil {
    61  		return err
    62  	}
    63  
    64  	client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), options.GOptions.Namespace, options.GOptions.Address)
    65  	if err != nil {
    66  		return err
    67  	}
    68  	defer cancel()
    69  
    70  	return namespace.Inspect(ctx, client, args, options)
    71  }