Skip to content

Instantly share code, notes, and snippets.

@kyriediculous
Created May 10, 2019 12:54
Show Gist options
  • Save kyriediculous/6b631371b75a8a0e4d944cc73033e9a8 to your computer and use it in GitHub Desktop.
Save kyriediculous/6b631371b75a8a0e4d944cc73033e9a8 to your computer and use it in GitHub Desktop.
// createCmd represents the create command
var createCmd = &cobra.Command{
Use: "create",
Short: "Create a new blog post",
Long: `Create a new blogpost on the server through gRPC.
A blog post requires an AuthorId, Title and Content.`,
RunE: func(cmd *cobra.Command, args []string) error {
// Get the data from our flags
author, err := cmd.Flags().GetString("author")
title, err := cmd.Flags().GetString("title")
content, err := cmd.Flags().GetString("content")
if err != nil {
return err
}
// Create a blog protobuffer message
blog := &blogpb.Blog{
AuthorId: author,
Title: title,
Content: content,
}
// RPC call
res, err := client.CreateBlog(
context.TODO(),
// wrap the blog message in a CreateBlog request message
&blogpb.CreateBlogReq{
Blog: blog,
},
)
if err != nil {
return err
}
fmt.Printf("Blog created: %s\n", res.Blog.Id)
return nil
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment