-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit.go
92 lines (81 loc) · 1.86 KB
/
edit.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
package main
import (
"bufio"
"errors"
"fmt"
"os"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/srvc/fail/v4"
)
func NewEditSubCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "edit",
Short: "edit post",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
// Validation
if len(args) > 1 {
return fail.New("Invalid arguments")
}
fs := afero.NewOsFs()
diApp, err := NewDiApp(ctx, fs)
if err != nil {
return fail.Wrap(err)
}
postSrv := diApp.PostService
// Get Post ID
var postId int
if len(args) == 0 {
// Search for post incrementally.
pId, err := searchPostId()
if err != nil {
return fail.Wrap(err)
}
postId = pId
} else {
pId, err := ParsePostIdFromArg(args[0])
if err != nil {
return fail.Wrap(err)
}
postId = pId
}
err = postSrv.EditPost(ctx, postId)
if 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
},
}
subCmds := []*cobra.Command{
NewEditLatestSubCmd(),
}
cmd.AddCommand(subCmds...)
return cmd
}
func searchPostId() (int, error) {
return 0, fail.New("Unimplemented")
}