github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/event-list.go (about) 1 // Copyright (c) 2015-2022 MinIO, Inc. 2 // 3 // This file is part of MinIO Object Storage stack 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package cmd 19 20 import ( 21 "context" 22 "fmt" 23 24 "github.com/fatih/color" 25 "github.com/minio/cli" 26 json "github.com/minio/colorjson" 27 "github.com/minio/mc/pkg/probe" 28 "github.com/minio/pkg/v2/console" 29 ) 30 31 var eventListFlags = []cli.Flag{} 32 33 var eventListCmd = cli.Command{ 34 Name: "list", 35 ShortName: "ls", 36 Usage: "list bucket notifications", 37 Action: mainEventList, 38 OnUsageError: onUsageError, 39 Before: setGlobalsFromContext, 40 Flags: append(eventListFlags, globalFlags...), 41 CustomHelpTemplate: `NAME: 42 {{.HelpName}} - {{.Usage}} 43 44 USAGE: 45 {{.HelpName}} TARGET ARN [FLAGS] 46 47 FLAGS: 48 {{range .VisibleFlags}}{{.}} 49 {{end}} 50 EXAMPLES: 51 1. List notification configurations associated to a specific arn 52 {{.Prompt}} {{.HelpName}} myminio/mybucket arn:aws:sqs:us-west-2:444455556666:your-queue 53 54 2. List all notification configurations 55 {{.Prompt}} {{.HelpName}} s3/mybucket 56 `, 57 } 58 59 // checkEventListSyntax - validate all the passed arguments 60 func checkEventListSyntax(ctx *cli.Context) { 61 if len(ctx.Args()) != 2 && len(ctx.Args()) != 1 { 62 showCommandHelpAndExit(ctx, 1) // last argument is exit code 63 } 64 } 65 66 // eventListMessage container 67 type eventListMessage struct { 68 Status string `json:"status"` 69 ID string `json:"id"` 70 Event []string `json:"event"` 71 Prefix string `json:"prefix"` 72 Suffix string `json:"suffix"` 73 Arn string `json:"arn"` 74 } 75 76 func (u eventListMessage) JSON() string { 77 u.Status = "success" 78 eventListMessageJSONBytes, e := json.MarshalIndent(u, "", " ") 79 fatalIf(probe.NewError(e), "Unable to marshal into JSON.") 80 return string(eventListMessageJSONBytes) 81 } 82 83 func (u eventListMessage) String() string { 84 msg := console.Colorize("ARN", fmt.Sprintf("%s ", u.Arn)) 85 for i, event := range u.Event { 86 msg += console.Colorize("Event", event) 87 if i != len(u.Event)-1 { 88 msg += "," 89 } 90 } 91 msg += console.Colorize("Filter", " Filter: ") 92 if u.Prefix != "" { 93 msg += console.Colorize("Filter", fmt.Sprintf("prefix=\"%s\"", u.Prefix)) 94 } 95 if u.Suffix != "" { 96 msg += console.Colorize("Filter", fmt.Sprintf("suffix=\"%s\"", u.Suffix)) 97 } 98 return msg 99 } 100 101 func mainEventList(cliCtx *cli.Context) error { 102 ctx, cancelEventList := context.WithCancel(globalContext) 103 defer cancelEventList() 104 105 console.SetColor("ARN", color.New(color.FgGreen, color.Bold)) 106 console.SetColor("Event", color.New(color.FgCyan, color.Bold)) 107 console.SetColor("Filter", color.New(color.Bold)) 108 109 checkEventListSyntax(cliCtx) 110 111 args := cliCtx.Args() 112 path := args[0] 113 arn := "" 114 if len(args) > 1 { 115 arn = args[1] 116 } 117 118 client, err := newClient(path) 119 if err != nil { 120 fatalIf(err.Trace(), "Unable to parse the provided url.") 121 } 122 123 s3Client, ok := client.(*S3Client) 124 if !ok { 125 fatalIf(errDummy().Trace(), "The provided url doesn't point to a S3 server.") 126 } 127 128 configs, err := s3Client.ListNotificationConfigs(ctx, arn) 129 fatalIf(err, "Unable to list notifications on the specified bucket.") 130 131 for _, config := range configs { 132 printMsg(eventListMessage{ 133 Event: config.Events, 134 Prefix: config.Prefix, 135 Suffix: config.Suffix, 136 Arn: config.Arn, 137 ID: config.ID, 138 }) 139 } 140 141 return nil 142 }