Created
May 10, 2019 13:46
-
-
Save kyriediculous/bc1d80f20fea3d433428f88ce2689f3b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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