-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
137 lines (118 loc) · 3.06 KB
/
main.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
package main
import (
"fmt"
"log"
"os"
"github.com/urfave/cli/v2"
"golang.org/x/term"
)
func main() {
var username string
var dstBlog string
var srcPath string
var uploadImages bool
var instanceUrl string
app := &cli.App{
Name: "Write.as Hugo Importer",
Usage: "Import a Hugo source directory into Write.as/WriteFreely by running this importer from the root directory of your Hugo site.",
Version: "1.0.1",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "user",
Aliases: []string{"u"},
Usage: "The username for the Write.as/WriteFreely account",
Required: true,
Destination: &username,
},
&cli.StringFlag{
Name: "blog",
Aliases: []string{"b"},
Usage: "The alias of the destination blog for importing your Hugo content.",
Required: true,
Destination: &dstBlog,
},
&cli.StringFlag{
Name: "content-dir",
Usage: "The name of the path to your source Hugo content (e.g., to import /content/news, use --content-dir news)",
Required: true,
Destination: &srcPath,
},
&cli.StringFlag{
Name: "instance",
Aliases: []string{"i"},
Usage: "Provide the URL of your WriteFreely instance (e.g., '--instance https://pencil.writefree.ly')",
Destination: &instanceUrl,
},
&cli.BoolFlag{
Name: "images",
Usage: "Use this flag to import local images to Snap.as (only for Write.as accounts with Snap.as add-on).",
Destination: &uploadImages,
},
},
Action: func(c *cli.Context) error {
if uploadImages && len(instanceUrl) > 0 {
fmt.Println("Uploading images to Snap.as is only available on Write.as!")
return nil
}
fmt.Println("Hello", username)
fmt.Println("Please enter your password:")
var enteredPassword string
for {
password, err := term.ReadPassword(0)
if err != nil {
panic(err)
}
if len(password) != 0 {
fmt.Println("Press Return to log in and start the migration.")
enteredPassword = string(password)
} else {
break
}
}
err := SignIn(username, enteredPassword, instanceUrl)
if err != nil {
log.Fatal(err)
}
fmt.Println("Importing content from content ->", srcPath)
fmt.Println("Importing content into blog alias ->", dstBlog)
if uploadImages {
fmt.Println("Uploading local images to Snap.as")
}
posts, err := ParseContentDirectory(srcPath, uploadImages)
if err != nil {
SignOut()
log.Fatal(err)
}
if uploadImages {
wd, err := os.Getwd()
if err != nil {
fmt.Println(err)
}
err = WriteUploadErrorsTo(wd)
if err != nil {
fmt.Println(err)
}
}
fmt.Println("")
fmt.Println("Publishing posts to", dstBlog)
for _, post := range posts {
err := PublishPost(post, dstBlog)
if err != nil {
SignOut()
log.Fatal(err)
}
}
fmt.Println("Posts published.")
SignOut()
err = WriteResponsesToDisk()
if err != nil {
log.Fatal(err)
}
return nil
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}