-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommands.go
241 lines (191 loc) · 4.97 KB
/
commands.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package main
import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
"github.com/adrg/xdg"
"github.com/spf13/cobra"
)
func CommandsRoot() *cobra.Command {
cmd := &cobra.Command{
Use: "mataroa-cli",
Short: "mataroa-cli is a CLI tool for mataroa.blog",
CompletionOptions: cobra.CompletionOptions{HiddenDefaultCmd: true},
DisableAutoGenTag: true,
}
cmd.AddCommand(CommandInit())
cmd.AddCommand(CommandsPosts())
return cmd
}
func CommandInit() *cobra.Command {
run := func(cmd *cobra.Command, args []string) {
_ = cmd.Context()
filePath, err := xdg.ConfigFile(ConfigurationFilePath)
if err != nil {
log.Fatalf("error initializing mataroa-cli: %s", err)
}
if _, err := os.Stat(filePath); err == nil {
log.Fatalf("error initializing mataroa-cli: config.json already exists")
} else if errors.Is(err, os.ErrNotExist) {
body, err := json.MarshalIndent(Config{
ApiUrl: "https://mataroa.blog/api",
Token: "your-api-key-here",
}, "", " ")
if err != nil {
log.Fatalf("error initializing mataroa-cli: couldn't marshal json file: %s", err)
}
err = os.WriteFile(filePath, body, os.FileMode((0o600)))
if err != nil {
log.Fatalf("error initializing mataroa-cli: %s", err)
}
fmt.Printf("mataroa-cli initialized successfully: '%s' file created\n", filePath)
}
}
cmd := &cobra.Command{
Use: "init",
Aliases: []string{"i"},
Short: "Initialize mataroa-cli",
Run: run,
}
return cmd
}
func CommandsPosts() *cobra.Command {
cmd := &cobra.Command{
Use: "posts",
Aliases: []string{"p"},
Short: "Manage posts",
}
config, err := LoadConfiguration()
if err != nil {
log.Fatal(err)
}
client, err := NewClient(config.ApiUrl, config.Token)
if err != nil {
log.Fatal(err)
}
cmd.AddCommand(CommandPostsList(client))
cmd.AddCommand(CommandPostsGet(client))
cmd.AddCommand(CommandPostsDelete(client))
cmd.AddCommand(CommandPostsUpdate(client))
cmd.AddCommand(CommandPostsCreate(client))
return cmd
}
func CommandPostsGet(client *Client) *cobra.Command {
run := func(cmd *cobra.Command, args []string) {
slug := args[0]
response, err := client.Get(cmd.Context(), slug)
if err != nil {
log.Fatal(err)
}
output, err := json.MarshalIndent(response.Post, "", " ")
if err != nil {
log.Fatalf("error marshaling json: %s", err)
}
fmt.Println(string(output))
}
cmd := &cobra.Command{
Use: "get",
Aliases: []string{"g"},
Short: "get a post",
Run: run,
}
return cmd
}
func CommandPostsUpdate(client *Client) *cobra.Command {
run := func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
slug := args[0]
filePath := args[1]
f, err := os.ReadFile(filePath)
if err != nil {
log.Fatalf("error reading markdown file: %s", err)
}
post, err := MarkdownToPost(f, false)
if err != nil {
log.Fatalf("error parsing markdown file: %s", err)
}
response, err := client.Update(ctx, slug, post)
if err != nil {
log.Fatalf("error updating post '%s': %s", slug, err)
}
if !response.OK {
log.Fatalf("error updating post '%s': %s", slug, err)
}
fmt.Printf("updated post '%s'\n", slug)
}
cmd := &cobra.Command{
Use: "update",
Aliases: []string{"u"},
Short: "update a post",
Run: run,
}
return cmd
}
func CommandPostsCreate(client *Client) *cobra.Command {
run := func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
filePath := args[0]
if _, err := os.Stat(filePath); errors.Is(err, os.ErrNotExist) {
log.Fatalf("%s: error creating new post: file '%s' not found\n", cmd.Use, filePath)
}
f, err := os.ReadFile(filePath)
if err != nil {
log.Fatalf("error reading markdown file: %s", err)
}
post, err := MarkdownToPost(f, false)
if err != nil {
log.Fatalf("error creating new post: %s\n", err)
}
response, err := client.Create(ctx, post)
if err != nil {
log.Fatalf("error creating new post: %s\n", err)
}
fmt.Printf("created post '%s': %s\n", response.Slug, response.URL)
}
cmd := &cobra.Command{
Use: "create",
Aliases: []string{"c"},
Short: "create a post",
Run: run,
}
return cmd
}
func CommandPostsDelete(client *Client) *cobra.Command {
run := func(cmd *cobra.Command, args []string) {
slug := args[0]
_, err := client.Delete(cmd.Context(), slug)
if err != nil {
log.Fatal(err)
}
fmt.Printf("deleted post '%s'\n", slug)
}
cmd := &cobra.Command{
Use: "delete",
Aliases: []string{"d"},
Short: "delete a post",
Run: run,
}
return cmd
}
func CommandPostsList(client *Client) *cobra.Command {
run := func(cmd *cobra.Command, args []string) {
response, err := client.ListAll(cmd.Context())
if err != nil {
log.Fatal(err)
}
output, err := json.MarshalIndent(response.PostList, "", " ")
if err != nil {
log.Fatalf("error marshaling json: %s", err)
}
fmt.Println(string(output))
}
cmd := &cobra.Command{
Use: "list",
Aliases: []string{"l"},
Short: "List posts",
Run: run,
}
return cmd
}