github.com/demonoid81/containerd@v1.3.4/cmd/ctr/commands/events/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 events 18 19 import ( 20 "encoding/json" 21 "fmt" 22 23 "github.com/containerd/containerd/cmd/ctr/commands" 24 "github.com/containerd/containerd/events" 25 "github.com/containerd/typeurl" 26 "github.com/urfave/cli" 27 28 // Register grpc event types 29 _ "github.com/containerd/containerd/api/events" 30 ) 31 32 // Command is the cli command for displaying containerd events 33 var Command = cli.Command{ 34 Name: "events", 35 Aliases: []string{"event"}, 36 Usage: "display containerd events", 37 Action: func(context *cli.Context) error { 38 client, ctx, cancel, err := commands.NewClient(context) 39 if err != nil { 40 return err 41 } 42 defer cancel() 43 eventsClient := client.EventService() 44 eventsCh, errCh := eventsClient.Subscribe(ctx, context.Args()...) 45 for { 46 var e *events.Envelope 47 select { 48 case e = <-eventsCh: 49 case err = <-errCh: 50 return err 51 } 52 if e != nil { 53 var out []byte 54 if e.Event != nil { 55 v, err := typeurl.UnmarshalAny(e.Event) 56 if err != nil { 57 return err 58 } 59 out, err = json.Marshal(v) 60 if err != nil { 61 return err 62 } 63 } 64 if _, err := fmt.Println( 65 e.Timestamp, 66 e.Namespace, 67 e.Topic, 68 string(out), 69 ); err != nil { 70 return err 71 } 72 } 73 } 74 }, 75 }