Merge branch 'master' of github.com:ehlxr/go-utils

This commit is contained in:
ehlxr 2020-07-20 10:08:51 +08:00
commit 796e503959
4 changed files with 199 additions and 1 deletions

2
go.mod
View File

@ -3,11 +3,13 @@ module github.com/ehlxr/go-utils
go 1.13 go 1.13
require ( require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect
github.com/dgrijalva/jwt-go v3.1.0+incompatible github.com/dgrijalva/jwt-go v3.1.0+incompatible
github.com/jessevdk/go-flags v1.4.1-0.20181221193153-c0795c8afcf4 github.com/jessevdk/go-flags v1.4.1-0.20181221193153-c0795c8afcf4
github.com/manifoldco/promptui v0.3.2 github.com/manifoldco/promptui v0.3.2
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
github.com/nicksnyder/go-i18n v1.10.1 // indirect
github.com/onsi/ginkgo v1.10.2 // indirect github.com/onsi/ginkgo v1.10.2 // indirect
github.com/onsi/gomega v1.7.0 // indirect github.com/onsi/gomega v1.7.0 // indirect
github.com/sirupsen/logrus v1.4.2 github.com/sirupsen/logrus v1.4.2

7
go.sum
View File

@ -1,3 +1,5 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/alecthomas/gometalinter v2.0.11+incompatible h1:ENdXMllZNSVDTJUUVIzBW9CSEpntTrQa76iRsEFLX/M= github.com/alecthomas/gometalinter v2.0.11+incompatible h1:ENdXMllZNSVDTJUUVIzBW9CSEpntTrQa76iRsEFLX/M=
github.com/alecthomas/gometalinter v2.0.11+incompatible/go.mod h1:qfIpQGGz3d+NmgyPBqv+LSh50emm1pt72EtcX2vKYQk= github.com/alecthomas/gometalinter v2.0.11+incompatible/go.mod h1:qfIpQGGz3d+NmgyPBqv+LSh50emm1pt72EtcX2vKYQk=
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d h1:UQZhZ2O0vMHr2cI+DC1Mbh0TJxzA3RcLoMsFw+aXw7E= github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d h1:UQZhZ2O0vMHr2cI+DC1Mbh0TJxzA3RcLoMsFw+aXw7E=
@ -58,12 +60,15 @@ github.com/mattn/go-isatty v0.0.10 h1:qxFzApOv4WsAL965uUPIsXzAKCZxN2p9UqdhFS4ZW1
github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84= github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84=
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b h1:j7+1HpAFS1zy5+Q4qx1fWh90gTKwiN4QCGoY9TWyyO4= github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b h1:j7+1HpAFS1zy5+Q4qx1fWh90gTKwiN4QCGoY9TWyyO4=
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
github.com/nicksnyder/go-i18n v2.0.2+incompatible h1:Xt6dluut3s2zBUha8/3sj6atWMQbFioi9OMqUGH9khg= github.com/nicksnyder/go-i18n v1.10.1 h1:isfg77E/aCD7+0lD/D00ebR2MV5vgeQ276WYyDaCRQc=
github.com/nicksnyder/go-i18n v1.10.1/go.mod h1:e4Di5xjP9oTVrC6y3C7C0HoSYXjSbhh/dU0eUV32nB4=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.10.2 h1:uqH7bpe+ERSiDa34FDOF7RikN6RzXgduUF8yarlZp94= github.com/onsi/ginkgo v1.10.2 h1:uqH7bpe+ERSiDa34FDOF7RikN6RzXgduUF8yarlZp94=
github.com/onsi/ginkgo v1.10.2/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.10.2/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.7.0 h1:XPnZz8VVBHjVsy1vzJmRwIcSwiUO+JFfrv/xGiigmME= github.com/onsi/gomega v1.7.0 h1:XPnZz8VVBHjVsy1vzJmRwIcSwiUO+JFfrv/xGiigmME=
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.0.4 h1:gzbtLsZC3Ic5PptoRG+kQj4L60qjK7H7XszrU163JNQ= github.com/sirupsen/logrus v1.0.4 h1:gzbtLsZC3Ic5PptoRG+kQj4L60qjK7H7XszrU163JNQ=

187
utils/blogmigrate/main.go Normal file
View File

@ -0,0 +1,187 @@
package main
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/manifoldco/promptui"
)
func main() {
prompt := promptui.SelectWithAdd{
// Label: "What's your source file path",
// Items: []string{"/Users/ehlxr/ehlxr/blog/Hexo/source/resume/index.md"},
AddLabel: "Input your source file path",
}
_, source, err := prompt.Run()
if err != nil {
fmt.Printf("prompt failed %v\n", err)
return
}
files := getFiles(source)
for _, file := range files {
// has, err := hasSlug(file)
// if err != nil {
// fmt.Printf("hasSlug file: %s failed: %v\n", file, err)
// continue
// }
// if has {
// println("file have content slug already")
// continue
// }
//
// bytes, err := handleText(file)
// if err != nil {
// fmt.Printf("handleText file: %s failed: %v\n", file, err)
// continue
// }
//
// if len(bytes) > 0 {
// err = writeFile(file, bytes)
// if err != nil {
// fmt.Printf("writeFile file: %s failed: %v\n", file, err)
// continue
// }
//
// fmt.Printf("deal file: %s done!!!\n", file)
// }
desc, err := genDesc(file)
if err != nil {
fmt.Printf("genDesc file: %s failed: %v\n", file, err)
}
fmt.Printf("file: %s desc: %s\n", file, desc)
}
}
func hasSlug(file string) (bool, error) {
f, err := os.OpenFile(file, os.O_RDONLY, 0644)
if err != nil {
return false, err
}
defer f.Close()
fd, err := ioutil.ReadAll(f)
if err != nil {
return false, err
}
if strings.Index(string(fd), "slug:") > -1 {
return true, nil
}
return false, nil
}
func handleText(path string) ([]byte, error) {
file, err := os.OpenFile(path, os.O_RDONLY, 0644)
if err != nil {
log.Printf("cannot open text file: %s, err: [%v]", path, err)
return nil, err
}
defer file.Close()
output := make([]byte, 0)
// 按行读取文件
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
// line := scanner.Bytes()
output = append(output, line...)
output = append(output, []byte("\n")...)
if strings.Index(line, "title:") > -1 {
newByte := strings.Replace(line, "title:", "slug:", 1)
output = append(output, newByte...)
output = append(output, []byte("\n")...)
}
}
if err := scanner.Err(); err != nil {
log.Printf("cannot scanner text file: %s, err: [%v]", path, err)
return nil, err
}
return output, nil
}
func genDesc(path string) ([]byte, error) {
file, err := os.OpenFile(path, os.O_RDONLY, 0644)
if err != nil {
log.Printf("cannot open text file: %s, err: [%v]", path, err)
return nil, err
}
defer file.Close()
// 按行读取文件
scanner := bufio.NewScanner(file)
var identifier int
output := make([]byte, 0)
for scanner.Scan() {
line := scanner.Text()
// line := scanner.Bytes()
if line == "<!--more-->" {
break
}
if identifier < 2 && strings.Index(line, "---") > -1 {
identifier++
continue
}
if identifier == 2 && line != "" {
output = append(output, line...)
output = append(output, []byte("\n")...)
output = append(output, []byte("\n")...)
}
}
if err := scanner.Err(); err != nil {
log.Printf("cannot scanner text file: %s, err: [%v]", path, err)
return nil, err
}
return output, nil
}
func writeFile(path string, b []byte) error {
file, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0777)
if err != nil {
return err
}
defer file.Close()
_, err = file.Write(b)
if err != nil {
return err
}
return nil
}
func getFiles(root string) (files []string) {
err := filepath.Walk(root, func(p string, f os.FileInfo, err error) error {
if f == nil {
return nil
}
if p == root || f.IsDir() {
return nil
}
files = append(files, p)
return nil
})
if err != nil {
fmt.Printf("filepath.Walk() returned %v\n", err)
}
return files
}

4
utils/blogmigrate/t.txt Normal file
View File

@ -0,0 +1,4 @@
[?25l ✔ Input your source file path: █
    
[?25h  ✔ Input your source file path: █
  prompt failed ^C