Compare commits
4 Commits
6a53e4841b
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4626f92bb9 | ||
|
|
ab277aa4ea | ||
|
|
9696cafce5 | ||
|
|
97adcc923c |
@@ -1,14 +1,17 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"git.assilvestrar.club/lourenco/go-hugo-cms.git/pkg/hugo"
|
||||||
|
|
||||||
"git.assilvestrar.club/lourenco/go-hugo-cms.git/pkg/git"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
options := git.GitOptions{
|
// options := git.GitOptions{
|
||||||
URL: "https://git.assilvestrar.club/lourenco/go-hugo-cms.git",
|
// URL: "https://git.assilvestrar.club/lourenco/go-hugo-cms.git",
|
||||||
}
|
// Name: "go-hugo-cms",
|
||||||
fmt.Println(options.Clone())
|
// }
|
||||||
|
|
||||||
|
hugo.List()
|
||||||
|
//if err != nil {
|
||||||
|
// fmt.Errorf("%w", err)
|
||||||
|
//}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package git
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
gogit "github.com/go-git/go-git/v5"
|
gogit "github.com/go-git/go-git/v5"
|
||||||
@@ -9,29 +10,56 @@ import (
|
|||||||
|
|
||||||
type GitOptions struct {
|
type GitOptions struct {
|
||||||
URL string
|
URL string
|
||||||
|
Name string
|
||||||
RecurseSubmodule bool
|
RecurseSubmodule bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GitOptions) Clone() (string, error) {
|
func (g *GitOptions) Clone() error {
|
||||||
a := &gogit.CloneOptions{
|
a := &gogit.CloneOptions{
|
||||||
URL: g.URL,
|
URL: g.URL,
|
||||||
Progress: os.Stdout,
|
Progress: os.Stdout,
|
||||||
}
|
}
|
||||||
if err := a.Validate(); err != nil {
|
if err := a.Validate(); err != nil {
|
||||||
return fmt.Sprintln("Validation failed"), err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
dir, err := os.MkdirTemp("", "clone")
|
if err := os.Mkdir(g.Name, 0750); err != nil && !os.IsExist(err) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := gogit.PlainClone(g.Name, false, a)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer os.RemoveAll(dir)
|
return nil
|
||||||
|
}
|
||||||
_, err = gogit.PlainClone(dir, false, a)
|
|
||||||
if err != nil {
|
func (g *GitOptions) Open() (*gogit.Repository, error) {
|
||||||
return "", err
|
return gogit.PlainOpen(g.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
return "Success", nil
|
func (g *GitOptions) Fetch() error {
|
||||||
|
repo, err := g.Open()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
a := &gogit.FetchOptions{
|
||||||
|
RemoteName: "origin",
|
||||||
|
Progress: os.Stdout,
|
||||||
|
}
|
||||||
|
if err := a.Validate(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = repo.Fetch(a); err != nil {
|
||||||
|
if err == gogit.NoErrAlreadyUpToDate { // not an actual error
|
||||||
|
io.WriteString(os.Stdout, fmt.Sprintln("already up to date")) // keeping this here just for debug
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
11
pkg/http/http.go
Normal file
11
pkg/http/http.go
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
func (c *ginContext) HttpHandler() {
|
||||||
|
r := gin.Default()
|
||||||
|
|
||||||
|
r.GET("/list", g.listPosts())
|
||||||
|
|
||||||
|
r.Run()
|
||||||
|
}
|
||||||
74
pkg/hugo/hugo.go
Normal file
74
pkg/hugo/hugo.go
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
package hugo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Metadata struct {
|
||||||
|
Path string
|
||||||
|
Slug string
|
||||||
|
Title string
|
||||||
|
Date *time.Time
|
||||||
|
ExpiryDate *time.Time
|
||||||
|
PublishDate *time.Time
|
||||||
|
Draft bool
|
||||||
|
Permalink string
|
||||||
|
Kind string
|
||||||
|
Section string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewBlog(name string) {
|
||||||
|
if empty, _ := CheckIfEmpty(name); !empty { // we need dir to be empty
|
||||||
|
log.Fatal("blog dir is not empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := exec.Command("hugo", "new", "site", name)
|
||||||
|
if err := cmd; err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd.Run()
|
||||||
|
}
|
||||||
|
|
||||||
|
func CheckIfEmpty(name string) (bool, error) {
|
||||||
|
dir, err := os.Open(name)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
defer dir.Close()
|
||||||
|
|
||||||
|
_, err = dir.Readdirnames(1)
|
||||||
|
if err == io.EOF {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func Build() {
|
||||||
|
cmd := exec.Command("hugo", "build")
|
||||||
|
if err := cmd; cmd != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd.Run()
|
||||||
|
}
|
||||||
|
|
||||||
|
func List() {
|
||||||
|
if err := os.Chdir("/home/lv/dev/assilvestrar-blog"); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := exec.Command("hugo", "list", "all")
|
||||||
|
if err := cmd; cmd != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := cmd.Run(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user