Skip to content

Instantly share code, notes, and snippets.

@kyriediculous
Created May 10, 2019 13:46
Show Gist options
  • Save kyriediculous/bc1d80f20fea3d433428f88ce2689f3b to your computer and use it in GitHub Desktop.
Save kyriediculous/bc1d80f20fea3d433428f88ce2689f3b to your computer and use it in GitHub Desktop.
// listCmd represents the read command
var listCmd = &cobra.Command{
Use: "list",
Short: "List all blog posts",
RunE: func(cmd *cobra.Command, args []string) error {
// Create the request (this can be inline below too)
req := &blogpb.ListBlogsReq{}
// Call ListBlogs that returns a stream
stream, err := client.ListBlogs(context.Background(), req)
// Check for errors
if err != nil {
return err
}
// Start iterating
for {
// stream.Recv returns a pointer to a ListBlogRes at the current iteration
res, err := stream.Recv()
// If end of stream, break the loop
if err == io.EOF {
break
}
// if err, return an error
if err != nil {
return err
}
// If everything went well use the generated getter to print the blog message
fmt.Println(res.GetBlog())
}
return nil
},
}
func init() {
rootCmd.AddCommand(listCmd)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment