-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit_latest.go
67 lines (62 loc) · 1.53 KB
/
edit_latest.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
package main
import (
"bufio"
"errors"
"fmt"
"os"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/srvc/fail/v4"
)
func NewEditLatestSubCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "latest",
Short: "edit latest post",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
fs := afero.NewOsFs()
diApp, err := NewDiApp(ctx, fs)
if err != nil {
return fail.Wrap(err)
}
client := diApp.Client
posts, err := client.GetPosts(cmd.Context(), 1, Updated)
if err != nil {
return fail.Wrap(err)
}
if len(posts) == 0 {
return fail.New("no posts")
}
postId := posts[0].Number
postSrv := diApp.PostService
if err := postSrv.EditPost(ctx, postId); err != nil {
if errors.Is(err, ErrPostCacheAlreadyExists) {
scanner := bufio.NewScanner(os.Stdin)
fmt.Printf("The file being edited exists. %s\n", postSrv.CacheDirPath(ctx, postId))
fmt.Printf("Do you want to edit thid file? (y/n):")
scanner.Scan()
ans := scanner.Text()
if ans == "y" {
err = postSrv.EditPostFromCache(ctx, postId)
if err != nil {
return fail.Wrap(err)
}
} else if ans == "n" {
if err := postSrv.DeletePostCache(ctx, postId); err != nil {
return fail.Wrap(err)
}
if err := postSrv.EditPost(ctx, postId); err != nil {
return fail.Wrap(err)
}
} else {
return fail.New("please input y or n.")
}
} else {
return fail.Wrap(err)
}
}
return nil
},
}
return cmd
}