mirror of https://github.com/cyp0633/WP2Artran
add conf parser
This commit is contained in:
parent
1ad47e3146
commit
2057c89c93
|
@ -18,4 +18,6 @@
|
|||
# vendor/
|
||||
|
||||
# Go workspace file
|
||||
go.work
|
||||
go.work
|
||||
|
||||
*.yaml
|
|
@ -1,5 +1,13 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/cyp0633/wp-comment-converter/internal/conf"
|
||||
)
|
||||
|
||||
func main() {
|
||||
println("Hello World")
|
||||
// read the first cli parameter as confPath
|
||||
confPath := os.Args[1]
|
||||
conf.ParseConf(confPath)
|
||||
}
|
||||
|
|
2
go.mod
2
go.mod
|
@ -1,3 +1,5 @@
|
|||
module github.com/cyp0633/wp-comment-converter
|
||||
|
||||
go 1.20
|
||||
|
||||
require gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
|
@ -0,0 +1,49 @@
|
|||
package conf
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
var Conf struct {
|
||||
Old struct {
|
||||
Hostname string
|
||||
PermalinkPrefix string `yaml:"permalink-prefix"`
|
||||
}
|
||||
New struct {
|
||||
Hostname string
|
||||
PermalinkPrefix string `yaml:"permalink-prefix"`
|
||||
}
|
||||
OutputPath string `yaml:"output-path"`
|
||||
Auth struct {
|
||||
Username string
|
||||
Password string
|
||||
}
|
||||
}
|
||||
|
||||
func ParseConf(confPath string) {
|
||||
// open the file and save as byte stream
|
||||
confBytes, err := os.ReadFile(confPath)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = yaml.Unmarshal(confBytes, &Conf)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if Conf.Old.PermalinkPrefix[0] != '/' {
|
||||
Conf.Old.PermalinkPrefix = "/" + Conf.Old.PermalinkPrefix
|
||||
}
|
||||
if Conf.New.PermalinkPrefix[0] != '/' {
|
||||
Conf.New.PermalinkPrefix = "/" + Conf.New.PermalinkPrefix
|
||||
}
|
||||
|
||||
if Conf.Old.PermalinkPrefix[len(Conf.Old.PermalinkPrefix)-1] != '/' {
|
||||
Conf.Old.PermalinkPrefix += "/"
|
||||
}
|
||||
if Conf.New.PermalinkPrefix[len(Conf.New.PermalinkPrefix)-1] != '/' {
|
||||
Conf.New.PermalinkPrefix += "/"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package conf
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestParseConf(t *testing.T) {
|
||||
confPath := "conf.yaml"
|
||||
ParseConf(confPath)
|
||||
t.Log(Conf)
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
package fetch
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/cyp0633/wp-comment-converter/internal/conf"
|
||||
)
|
||||
|
||||
type WPComment struct {
|
||||
}
|
||||
|
||||
func FetchComments() (comments []WPComment) {
|
||||
for page := 1; ; page++ {
|
||||
body := request(page)
|
||||
newComments := parse(body)
|
||||
if len(newComments) < 20 {
|
||||
break
|
||||
}
|
||||
comments = append(comments, newComments...)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func request(page int) (body []byte) {
|
||||
url := conf.Conf.Old.Hostname + "/wp-json/wp/v2/comments?per_page=20&page=" + strconv.Itoa(page) + "&context=edit"
|
||||
user := conf.Conf.Auth.Username
|
||||
pass := conf.Conf.Auth.Password
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
req.SetBasicAuth(user, pass)
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err = io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func parse(body []byte) (comments []WPComment) {
|
||||
return
|
||||
}
|
Loading…
Reference in New Issue