-
Notifications
You must be signed in to change notification settings - Fork 0
/
new.go
70 lines (59 loc) · 1.42 KB
/
new.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
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")
}
ctx := cmd.Context()
fs := afero.NewOsFs()
diApp, err := NewDiApp(ctx, fs)
if err != nil {
return fail.Wrap(err)
}
client := diApp.Client
postSrv := diApp.PostService
template, err := cmd.Flags().GetString("template")
if err != nil {
return fail.Wrap(err)
}
noEdit, err := cmd.Flags().GetBool("no-edit")
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)
}
if noEdit {
fmt.Printf("Created: %s\n", post.FullName)
fmt.Println(post.Url)
} else {
if err := postSrv.EditPost(ctx, post.Number); err != nil {
return fail.Wrap(err)
}
}
} else {
panic("Unimplemented")
}
return nil
},
}
cmd.Flags().StringP("template", "t", "", "the id of template post")
cmd.Flags().Bool("no-edit", false, "run edit mode after created")
return cmd
}