|
| 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 | + "bytes" |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + "text/tabwriter" |
| 24 | + "text/template" |
| 25 | + |
| 26 | + "github.com/containerd/nerdctl/pkg/apparmorutil" |
| 27 | + "github.com/spf13/cobra" |
| 28 | +) |
| 29 | + |
| 30 | +func newApparmorLsCommand() *cobra.Command { |
| 31 | + cmd := &cobra.Command{ |
| 32 | + Use: "ls", |
| 33 | + Aliases: []string{"list"}, |
| 34 | + Short: "List the loaded AppArmor profiles", |
| 35 | + Args: cobra.NoArgs, |
| 36 | + RunE: apparmorLsAction, |
| 37 | + SilenceUsage: true, |
| 38 | + SilenceErrors: true, |
| 39 | + } |
| 40 | + cmd.Flags().BoolP("quiet", "q", false, "Only display profile names") |
| 41 | + // Alias "-f" is reserved for "--filter" |
| 42 | + cmd.Flags().String("format", "", "Format the output using the given go template") |
| 43 | + cmd.RegisterFlagCompletionFunc("format", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 44 | + return []string{"json"}, cobra.ShellCompDirectiveNoFileComp |
| 45 | + }) |
| 46 | + return cmd |
| 47 | +} |
| 48 | + |
| 49 | +func apparmorLsAction(cmd *cobra.Command, args []string) error { |
| 50 | + quiet, err := cmd.Flags().GetBool("quiet") |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + w := cmd.OutOrStdout() |
| 55 | + var tmpl *template.Template |
| 56 | + format, err := cmd.Flags().GetString("format") |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + switch format { |
| 61 | + case "", "table": |
| 62 | + w = tabwriter.NewWriter(cmd.OutOrStdout(), 4, 8, 4, ' ', 0) |
| 63 | + if !quiet { |
| 64 | + fmt.Fprintln(w, "NAME\tMODE") |
| 65 | + } |
| 66 | + case "raw": |
| 67 | + return errors.New("unsupported format: \"raw\"") |
| 68 | + default: |
| 69 | + if quiet { |
| 70 | + return errors.New("format and quiet must not be specified together") |
| 71 | + } |
| 72 | + var err error |
| 73 | + tmpl, err = parseTemplate(format) |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + profiles, err := apparmorutil.Profiles() |
| 80 | + if err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + |
| 84 | + for _, f := range profiles { |
| 85 | + if tmpl != nil { |
| 86 | + var b bytes.Buffer |
| 87 | + if err := tmpl.Execute(&b, f); err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + if _, err = fmt.Fprintf(w, b.String()+"\n"); err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + } else if quiet { |
| 94 | + fmt.Fprintln(w, f.Name) |
| 95 | + } else { |
| 96 | + fmt.Fprintf(w, "%s\t%s\n", f.Name, f.Mode) |
| 97 | + } |
| 98 | + } |
| 99 | + if f, ok := w.(Flusher); ok { |
| 100 | + return f.Flush() |
| 101 | + } |
| 102 | + return nil |
| 103 | +} |
0 commit comments