github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/system_info.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/system"
    23  	"github.com/spf13/cobra"
    24  )
    25  
    26  func newInfoCommand() *cobra.Command {
    27  	var infoCommand = &cobra.Command{
    28  		Use:           "info",
    29  		Args:          cobra.NoArgs,
    30  		Short:         "Display system-wide information",
    31  		RunE:          infoAction,
    32  		SilenceUsage:  true,
    33  		SilenceErrors: true,
    34  	}
    35  	infoCommand.Flags().String("mode", "dockercompat", `Information mode, "dockercompat" for Docker-compatible output, "native" for containerd-native output`)
    36  	infoCommand.RegisterFlagCompletionFunc("mode", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    37  		return []string{"dockercompat", "native"}, cobra.ShellCompDirectiveNoFileComp
    38  	})
    39  	infoCommand.Flags().StringP("format", "f", "", "Format the output using the given Go template, e.g, '{{json .}}'")
    40  	infoCommand.RegisterFlagCompletionFunc("format", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    41  		return []string{"json"}, cobra.ShellCompDirectiveNoFileComp
    42  	})
    43  	return infoCommand
    44  }
    45  
    46  func processInfoOptions(cmd *cobra.Command) (types.SystemInfoOptions, error) {
    47  	globalOptions, err := processRootCmdFlags(cmd)
    48  	if err != nil {
    49  		return types.SystemInfoOptions{}, err
    50  	}
    51  
    52  	mode, err := cmd.Flags().GetString("mode")
    53  	if err != nil {
    54  		return types.SystemInfoOptions{}, err
    55  	}
    56  	format, err := cmd.Flags().GetString("format")
    57  	if err != nil {
    58  		return types.SystemInfoOptions{}, err
    59  	}
    60  	return types.SystemInfoOptions{
    61  		GOptions: globalOptions,
    62  		Mode:     mode,
    63  		Format:   format,
    64  		Stdout:   cmd.OutOrStdout(),
    65  		Stderr:   cmd.OutOrStderr(),
    66  	}, nil
    67  }
    68  
    69  func infoAction(cmd *cobra.Command, args []string) error {
    70  	options, err := processInfoOptions(cmd)
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), options.GOptions.Namespace, options.GOptions.Address)
    76  	if err != nil {
    77  		return err
    78  	}
    79  	defer cancel()
    80  
    81  	return system.Info(ctx, client, options)
    82  }