-
Notifications
You must be signed in to change notification settings - Fork 2
/
unwip.go
101 lines (79 loc) · 2.17 KB
/
unwip.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
package subcmd
import (
"fmt"
"sort"
"github.com/Songmu/prompter"
"github.com/kanmu/kasa"
"github.com/kanmu/kasa/esa/model"
)
type UnwipCmd struct {
Path string `arg:"" help:"Post name/Post category/Post tag."`
Search bool `short:"s" help:"Search posts. see https://docs.esa.io/posts/104"`
Force bool `short:"f" help:"Skip confirmation of files to move."`
Notice bool `negatable:"" help:"Unwip with notify."`
Page int `short:"p" default:"1" help:"Page number."`
Recursive bool `short:"r" default:"true" negatable:"" help:"Recursively list posts."`
}
func (cmd *UnwipCmd) Run(ctx *kasa.Context) error {
var posts []*model.Post
var hasMore bool
var err error
if cmd.Search {
posts, hasMore, err = ctx.Driver.Search(cmd.Path, cmd.Page)
} else {
posts, hasMore, err = ctx.Driver.ListOrTagSearch(cmd.Path, cmd.Page, cmd.Recursive)
}
if err != nil {
return err
}
sort.Slice(posts, func(i, j int) bool { return posts[i].FullName < posts[j].FullName })
if err != nil {
return err
}
newPosts := make([]*model.WipPostBody, len(posts))
hasWip := false
for i, v := range posts {
if v.Wip {
hasWip = true
}
newPost := &model.WipPostBody{
Wip: false,
UpdatedAt: v.UpdatedAt,
}
newPosts[i] = newPost
}
if !hasWip {
ctx.Fmt.Println("WIP posts missing.")
return nil
}
if !cmd.Force {
for _, oldPost := range posts {
if oldPost.Wip {
ctx.Fmt.Printf("unwip '%s'\n", oldPost.FullNameWithoutTags())
}
}
if hasMore {
ctx.Fmt.Printf("(has more pages. current page is %d, try '-p %d')\n", cmd.Page, cmd.Page+1)
}
approval := prompter.YN("Do you want to unwip posts?", false)
if !approval {
ctx.Fmt.Println("Unwip cancelled.")
return nil
}
}
for i, oldPost := range posts {
if !oldPost.Wip {
continue
}
newPost := newPosts[i]
ctx.Fmt.Printf("unwip '%s'\n", oldPost.FullNameWithoutTags())
err := ctx.Driver.Wip(newPost, oldPost.Number, cmd.Notice)
if err != nil {
return fmt.Errorf("failed to unwip '%s':%w", oldPost.FullNameWithoutTags(), err)
}
}
if hasMore {
ctx.Fmt.Printf("(has more pages. current page is %d, try '-p %d')\n", cmd.Page, cmd.Page+1)
}
return nil
}