add conf parser

This commit is contained in:
cyp0633 2023-06-16 20:48:36 +08:00
parent 1ad47e3146
commit 2057c89c93
Signed by: cyp0633
GPG Key ID: CF90D09FB1FDCE45
7 changed files with 125 additions and 2 deletions

4
.gitignore vendored
View File

@ -18,4 +18,6 @@
# vendor/
# Go workspace file
go.work
go.work
*.yaml

View File

@ -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
View File

@ -1,3 +1,5 @@
module github.com/cyp0633/wp-comment-converter
go 1.20
require gopkg.in/yaml.v3 v3.0.1 // indirect

3
go.sum Normal file
View File

@ -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=

49
internal/conf/conf.go Normal file
View File

@ -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 += "/"
}
}

View File

@ -0,0 +1,9 @@
package conf
import "testing"
func TestParseConf(t *testing.T) {
confPath := "conf.yaml"
ParseConf(confPath)
t.Log(Conf)
}

50
internal/fetch/fetch.go Normal file
View File

@ -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
}