/
post.go
108 lines (84 loc) · 1.94 KB
/
post.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package subcmd
import (
"errors"
"io"
"os"
"github.com/winebarrel/kasa"
"github.com/winebarrel/kasa/esa"
"github.com/winebarrel/kasa/esa/model"
"github.com/winebarrel/kasa/utils"
)
type PostCmd struct {
Name string `short:"n" help:"Post title."`
Body string `short:"b" help:"Post body file." predictor:"file"`
Path string `arg:"" optional:"" help:"Post number to update."`
Tags []string `short:"t" help:"Post tags."`
Category string `short:"c" help:"Post category."`
Wip bool `negatable:"" help:"Post as WIP."`
Message string `short:"m" help:"Post message."`
Notice bool `negatable:"" help:"Post with notify."`
}
func (cmd *PostCmd) Run(ctx *kasa.Context) error {
postNum := 0
if cmd.Path == "" {
if cmd.Name == "" {
return errors.New("missing flags: --name=STRING")
}
if cmd.Body == "" {
return errors.New("missing flags: --body=STRING")
}
} else {
num, err := utils.GetPostNum(ctx.Team, cmd.Path)
if err != nil {
return err
}
var post *model.Post
if num > 0 {
post, err = ctx.Driver.GetFromPageNum(num)
} else {
post, err = ctx.Driver.Get(cmd.Path)
}
if err != nil {
return err
}
if post == nil {
return errors.New("post not found")
}
postNum = post.Number
}
var bodyMd []byte
if cmd.Body != "" {
var file io.ReadCloser
if cmd.Body == "-" {
file = os.Stdin
} else {
var err error
file, err = os.OpenFile(cmd.Body, os.O_RDONLY, 0)
if err != nil {
return err
}
defer file.Close()
}
var err error
bodyMd, err = io.ReadAll(file)
if err != nil {
return err
}
} else {
bodyMd = []byte{}
}
newPost := &model.NewPostBody{
Name: cmd.Name,
BodyMd: string(bodyMd),
Tags: cmd.Tags,
Category: cmd.Category,
Wip: esa.Bool(cmd.Wip),
Message: cmd.Message,
}
url, err := ctx.Driver.Post(newPost, postNum, cmd.Notice)
if err != nil {
return err
}
ctx.Fmt.Println(url)
return nil
}