-
Notifications
You must be signed in to change notification settings - Fork 0
/
writeas-sync.go
221 lines (186 loc) · 5.93 KB
/
writeas-sync.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
package main
import (
"fmt"
"github.com/snapas/go-snapas"
"github.com/spf13/cobra"
"github.com/studio-b12/gowebdav"
"github.com/writeas/go-writeas/v2"
"log/slog"
"os"
)
func doSync(conv ImageSyncer, ps *PostSynchronizer, doDownload, doUpload bool) error {
slog.Default().Info("Retrieving remote image names")
err := conv.BuildImageMap()
if err != nil {
return err
}
slog.Default().Info("Enumerating local posts", slog.String("rootDir", ps.rootDir))
err = ps.FindFiles()
if err != nil {
return err
}
slog.Default().Info("Found local posts", slog.Int("num", len(ps.posts)))
slog.Default().Info("Fetching the remote posts")
remotePosts, err := ps.LoadRemotePosts()
if err != nil {
return err
}
slog.Default().Info("Found remote posts", slog.Int("num", len(remotePosts)))
if doDownload {
slog.Default().Info("Downloading new or changed remote posts")
err = ps.UpdateOrCreateLocalPosts(remotePosts)
if err != nil {
return err
}
}
if doUpload {
slog.Default().Info("Uploading new or changed images")
imageMap, err := ps.UploadLocalImages()
if err != nil {
return err
}
slog.Default().Info("Uploading new or changed local posts")
err = ps.UpdateOrCreateRemotePosts(remotePosts, imageMap)
if err != nil {
return err
}
}
return nil
}
type Application struct {
conv ImageSyncer
ps *PostSynchronizer
}
type Settings struct {
Alias string
RootDirectory string
Login string
Password string
ImageHostingType string
ImageLogin string
ImagePassword string
WebDavEndpoint string
WebDavImageUrl string
SnapAsEndpoint string
WriteAsEndpoint string
}
func initApp(sets *Settings) (*Application, error) {
writeAsClient := writeas.NewClientWith(writeas.Config{
URL: sets.WriteAsEndpoint,
})
slog.Default().Info("Logging into Write.as")
user, err := writeAsClient.LogIn(sets.Login, sets.Password)
if err != nil {
return nil, fmt.Errorf("failed to login: %w", err)
}
writeAsClient.SetToken(user.AccessToken)
snapAsClient := snapas.NewClient(user.AccessToken)
var conv ImageSyncer
if sets.ImageHostingType == "webdav" {
client := gowebdav.NewClient(sets.WebDavEndpoint, sets.ImageLogin, sets.ImagePassword)
err = client.Connect()
if err != nil {
return nil, fmt.Errorf("failed to connect to WebDAV: %w", err)
}
conv = NewWebDAVSync(client, sets.RootDirectory, sets.WebDavImageUrl)
} else if sets.ImageHostingType == "snapas" {
conv = NewSnapasSync(snapAsClient, sets.RootDirectory)
} else {
panic("invalid image hosting type")
}
ps := NewPostSynchronizer(conv, writeAsClient, sets.RootDirectory, sets.Alias)
return &Application{
conv: conv,
ps: ps,
}, nil
}
func main() {
var rootCmd = &cobra.Command{
Use: "writeas-sync",
Short: "writeas-sync is a bidirectional synchronizer for your blog",
Long: `Synchronizer for your Markdown-based blog`,
}
setts := &Settings{}
wd, _ := os.Getwd()
if wd == "" {
wd = "."
}
rootCmd.PersistentFlags().StringVarP(&setts.Alias, "alias", "a",
os.Getenv("WRITEAS_ALIAS"), "Write.as alias")
rootCmd.PersistentFlags().StringVarP(&setts.RootDirectory, "root", "r",
wd, "Root directory of your blog")
rootCmd.PersistentFlags().StringVarP(&setts.Login, "login", "l", os.Getenv("WRITEAS_LOGIN"),
"Write.as login")
rootCmd.PersistentFlags().StringVarP(&setts.Password, "password", "p", "",
"Write.as password (uses WRITEAS_PASS environment variable if not specified)")
cobra.OnInitialize(func() {
if setts.Password == "" {
setts.Password = os.Getenv("WRITEAS_PASS")
}
if setts.ImageLogin == "" {
setts.ImageLogin = setts.Login
}
if setts.ImagePassword == "" {
setts.ImagePassword = setts.Password
}
})
rootCmd.PersistentFlags().StringVarP(&setts.ImageHostingType, "image-hosting-type", "t",
"snapas", "Image hosting type: snapas (default), webdav")
rootCmd.PersistentFlags().StringVarP(&setts.ImageLogin, "image-login", "i", "",
"Image hosting login, the same as WriteAs login if not specified")
rootCmd.PersistentFlags().StringVarP(&setts.ImagePassword, "image-password", "v", "",
"Image hosting password, the same as WriteAs password if not specified")
rootCmd.PersistentFlags().StringVarP(&setts.WebDavEndpoint, "webdav-endpoint", "",
os.Getenv("WRITEAS_WEBDAV_URL"), "WebDAV endpoint URL")
rootCmd.PersistentFlags().StringVarP(&setts.WebDavImageUrl, "webdav-published-url", "",
os.Getenv("WRITEAS_WEBDAV_PUBLISHED_URL"), "URL for publicly accessible WebDAV images")
rootCmd.PersistentFlags().StringVarP(&setts.SnapAsEndpoint, "snapas-endpoint", "s",
"https://snap.as/api", "Snap.as API endpoint")
rootCmd.PersistentFlags().StringVarP(&setts.WriteAsEndpoint, "writeas-endpoint", "w",
"https://write.as/api", "Write.as API endpoint")
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
if setts.ImageHostingType != "webdav" && setts.ImageHostingType != "snapas" {
return fmt.Errorf("invalid image hosting type: %s", setts.ImageHostingType)
}
return nil
}
syncCmd := &cobra.Command{
Use: "sync",
Short: "Synchronize your blog (upload and download)",
RunE: func(cmd *cobra.Command, args []string) error {
app, err := initApp(setts)
if err != nil {
return err
}
return doSync(app.conv, app.ps, true, true)
},
}
uploadCmd := &cobra.Command{
Use: "upload",
Short: "Push your local changes to the remote blog",
RunE: func(cmd *cobra.Command, args []string) error {
app, err := initApp(setts)
if err != nil {
return err
}
return doSync(app.conv, app.ps, false, true)
},
}
downloadCmd := &cobra.Command{
Use: "download",
Short: "Pull remote changes to your local blog",
RunE: func(cmd *cobra.Command, args []string) error {
app, err := initApp(setts)
if err != nil {
return err
}
return doSync(app.conv, app.ps, true, false)
},
}
rootCmd.AddCommand(syncCmd, uploadCmd, downloadCmd)
err := rootCmd.Execute()
if err != nil {
slog.Default().Error("Command failed", "error", err)
os.Exit(1)
}
}