github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/apparmor_list_linux.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/apparmor"
    22  	"github.com/spf13/cobra"
    23  )
    24  
    25  func newApparmorLsCommand() *cobra.Command {
    26  	cmd := &cobra.Command{
    27  		Use:           "ls",
    28  		Aliases:       []string{"list"},
    29  		Short:         "List the loaded AppArmor profiles",
    30  		Args:          cobra.NoArgs,
    31  		RunE:          apparmorLsAction,
    32  		SilenceUsage:  true,
    33  		SilenceErrors: true,
    34  	}
    35  	cmd.Flags().BoolP("quiet", "q", false, "Only display profile names")
    36  	// Alias "-f" is reserved for "--filter"
    37  	cmd.Flags().String("format", "", "Format the output using the given go template")
    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 processApparmorListOptions(cmd *cobra.Command) (types.ApparmorListOptions, error) {
    45  	quiet, err := cmd.Flags().GetBool("quiet")
    46  	if err != nil {
    47  		return types.ApparmorListOptions{}, err
    48  	}
    49  	format, err := cmd.Flags().GetString("format")
    50  	if err != nil {
    51  		return types.ApparmorListOptions{}, err
    52  	}
    53  	return types.ApparmorListOptions{
    54  		Quiet:  quiet,
    55  		Format: format,
    56  		Stdout: cmd.OutOrStdout(),
    57  	}, nil
    58  }
    59  
    60  func apparmorLsAction(cmd *cobra.Command, args []string) error {
    61  	options, err := processApparmorListOptions(cmd)
    62  	if err != nil {
    63  		return err
    64  	}
    65  	return apparmor.List(options)
    66  }