github.com/QuangHoangHao/kafka-go@v0.4.36/topics/list_topics.go (about) 1 // Package topics is an experimental package that provides additional tooling 2 // around Kafka Topics. This package does not make any promises around 3 // backwards compatibility. 4 package topics 5 6 import ( 7 "context" 8 "errors" 9 "regexp" 10 11 "github.com/QuangHoangHao/kafka-go" 12 ) 13 14 // List returns a slice of all the Topics. 15 func List(ctx context.Context, client *kafka.Client) (topics []kafka.Topic, err error) { 16 if client == nil { 17 return nil, errors.New("client is required") 18 } 19 response, err := client.Metadata(ctx, &kafka.MetadataRequest{ 20 Addr: client.Addr, 21 }) 22 if err != nil { 23 return nil, err 24 } 25 26 return response.Topics, nil 27 } 28 29 // ListRe returns a slice of Topics that match a regex. 30 func ListRe(ctx context.Context, cli *kafka.Client, re *regexp.Regexp) (topics []kafka.Topic, err error) { 31 alltopics, err := List(ctx, cli) 32 if err != nil { 33 return nil, err 34 } 35 36 for _, val := range alltopics { 37 if re.MatchString(val.Name) { 38 topics = append(topics, val) 39 } 40 } 41 return topics, nil 42 }