github.com/containerd/nerdctl/v2@v2.0.0-beta.5.0.20240520001846-b5758f54fa28/pkg/cmd/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 apparmor 18 19 import ( 20 "bytes" 21 "errors" 22 "fmt" 23 "text/tabwriter" 24 "text/template" 25 26 "github.com/containerd/nerdctl/v2/pkg/api/types" 27 "github.com/containerd/nerdctl/v2/pkg/apparmorutil" 28 "github.com/containerd/nerdctl/v2/pkg/formatter" 29 ) 30 31 func List(options types.ApparmorListOptions) error { 32 quiet := options.Quiet 33 w := options.Stdout 34 var tmpl *template.Template 35 format := options.Format 36 switch format { 37 case "", "table", "wide": 38 w = tabwriter.NewWriter(w, 4, 8, 4, ' ', 0) 39 if !quiet { 40 fmt.Fprintln(w, "NAME\tMODE") 41 } 42 case "raw": 43 return errors.New("unsupported format: \"raw\"") 44 default: 45 if quiet { 46 return errors.New("format and quiet must not be specified together") 47 } 48 var err error 49 tmpl, err = formatter.ParseTemplate(format) 50 if err != nil { 51 return err 52 } 53 } 54 55 profiles, err := apparmorutil.Profiles() 56 if err != nil { 57 return err 58 } 59 60 for _, f := range profiles { 61 if tmpl != nil { 62 var b bytes.Buffer 63 if err := tmpl.Execute(&b, f); err != nil { 64 return err 65 } 66 if _, err = fmt.Fprintln(w, b.String()); err != nil { 67 return err 68 } 69 } else if quiet { 70 fmt.Fprintln(w, f.Name) 71 } else { 72 fmt.Fprintf(w, "%s\t%s\n", f.Name, f.Mode) 73 } 74 } 75 if f, ok := w.(formatter.Flusher); ok { 76 return f.Flush() 77 } 78 return nil 79 }