github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/system_events.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 newEventsCommand() *cobra.Command { 27 shortHelp := `Get real time events from the server` 28 longHelp := shortHelp + "\nNOTE: The output format is not compatible with Docker." 29 var eventsCommand = &cobra.Command{ 30 Use: "events", 31 Args: cobra.NoArgs, 32 Short: shortHelp, 33 Long: longHelp, 34 RunE: eventsAction, 35 SilenceUsage: true, 36 SilenceErrors: true, 37 } 38 eventsCommand.Flags().String("format", "", "Format the output using the given Go template, e.g, '{{json .}}'") 39 eventsCommand.RegisterFlagCompletionFunc("format", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { 40 return []string{"json"}, cobra.ShellCompDirectiveNoFileComp 41 }) 42 eventsCommand.Flags().StringSliceP("filter", "f", []string{}, "Filter matches containers based on given conditions") 43 return eventsCommand 44 } 45 46 func processSystemEventsOptions(cmd *cobra.Command) (types.SystemEventsOptions, error) { 47 globalOptions, err := processRootCmdFlags(cmd) 48 if err != nil { 49 return types.SystemEventsOptions{}, err 50 } 51 format, err := cmd.Flags().GetString("format") 52 if err != nil { 53 return types.SystemEventsOptions{}, err 54 } 55 filters, err := cmd.Flags().GetStringSlice("filter") 56 if err != nil { 57 return types.SystemEventsOptions{}, err 58 } 59 return types.SystemEventsOptions{ 60 Stdout: cmd.OutOrStdout(), 61 GOptions: globalOptions, 62 Format: format, 63 Filters: filters, 64 }, nil 65 } 66 67 func eventsAction(cmd *cobra.Command, args []string) error { 68 options, err := processSystemEventsOptions(cmd) 69 if err != nil { 70 return err 71 } 72 client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), options.GOptions.Namespace, options.GOptions.Address) 73 if err != nil { 74 return err 75 } 76 defer cancel() 77 return system.Events(ctx, client, options) 78 }