Skip to content

Commit

Permalink
add new command that crete post from template
Browse files Browse the repository at this point in the history
  • Loading branch information
taxio committed May 29, 2021
1 parent c06e373 commit d36f9e4
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
54 changes: 54 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,57 @@ func (c *Client) UpdatePost(ctx context.Context, post *Post) error {

return nil
}

type NewPostRequest struct {
Post NewPostRequestPost `json:"post"`
}

type NewPostRequestPost struct {
Name string `json:"name,omitempty"`
BodyMd string `json:"body_md,omitempty"`
Tags []string `json:"tags,omitempty"`
Category string `json:"category,omitempty"`
Wip bool `json:"wip,omitempty"`
Message string `json:"message,omitempty"`
User string `json:"user,omitempty"`
TemplatePostId int `json:"template_post_id,omitempty"`
}

func (c *Client) CreatePostFromTemplate(ctx context.Context, templateId int) (*Post, error) {
req := NewPostRequest{
Post: NewPostRequestPost{
TemplatePostId: templateId,
},
}
var res GetPostResponse

err := c.client.Post(
ctx,
hx.Path("posts"),
hx.JSON(req),
hx.WhenSuccess(hx.AsJSON(&res)),
hx.WhenFailure(hx.AsError()),
)
if err != nil {
return nil, fail.Wrap(err)
}

post := Post{
Number: res.Number,
Name: res.Name,
FullName: res.FullName,
BodyMd: res.BodyMd,
CreatedAt: res.CreatedAt,
UpdatedAt: res.UpdatedAt,
Url: res.Url,
Tags: res.Tags,
Category: res.Category,
OriginalRevision: PostRevision{
BodyMd: res.BodyMd,
Number: res.Number,
User: res.UpdatedBy.Name,
},
}

return &post, nil
}
60 changes: 60 additions & 0 deletions new.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

import (
"fmt"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/srvc/fail/v4"
)

func NewNewSubCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "new",
Short: "create new post from template",
RunE: func(cmd *cobra.Command, args []string) error {
// Validation
if len(args) > 1 {
return fail.New("Invalid arguments")
}

fs := afero.NewOsFs()
cfg, err := LoadConfig(fs)
if err != nil {
return fail.Wrap(err)
}
client, err := NewClient(cfg.AccessToken, cfg.TeamName)
if err != nil {
return fail.Wrap(err)
}
ctx := cmd.Context()

template, err := cmd.Flags().GetString("template")
if err != nil {
return fail.Wrap(err)
}

if template != "" {
templatePostId, err := ParsePostIdFromArg(template)
if err != nil {
return fail.Wrap(err)
}

post, err := client.CreatePostFromTemplate(ctx, templatePostId)
if err != nil {
return fail.Wrap(err)
}

fmt.Printf("Created: %s\n", post.FullName)
fmt.Println(post.Url)
} else {
panic("Unimplemented")
}

return nil
},
}

cmd.Flags().StringP("template", "t", "", "the id of template post")

return cmd
}
1 change: 1 addition & 0 deletions root.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func NewRootCmd() *cobra.Command {
NewConfigSubCmd(),
NewListSubCmd(),
NewEditSubCmd(),
NewNewSubCmd(),
}
cmd.AddCommand(subCmds...)

Expand Down

0 comments on commit d36f9e4

Please sign in to comment.