added basic http routing; fixed git integration

This commit is contained in:
lourenco
2025-09-25 12:22:10 +02:00
parent 97adcc923c
commit 9696cafce5
3 changed files with 35 additions and 26 deletions

View File

@@ -8,19 +8,11 @@ import (
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",
_, err := options.Clone()
if err != nil {
fmt.Errorf("%w", err)
} }
_, err = options.Open() err := options.Fetch()
if err != nil {
fmt.Errorf("%w", err)
}
err = options.Fetch()
if err != nil { if err != nil {
fmt.Errorf("%w", err) fmt.Errorf("%w", err)
} }

View File

@@ -1,6 +1,8 @@
package git package git
import ( import (
"fmt"
"io"
"os" "os"
gogit "github.com/go-git/go-git/v5" gogit "github.com/go-git/go-git/v5"
@@ -12,25 +14,25 @@ type GitOptions struct {
RecurseSubmodule bool RecurseSubmodule bool
} }
func (g *GitOptions) Clone() (*gogit.Repository, 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 nil, err return err
} }
if err := os.Mkdir(g.Name, 0750); err != nil && !os.IsExist(err) { if err := os.Mkdir(g.Name, 0750); err != nil && !os.IsExist(err) {
return nil, err return err
} }
result, err := gogit.PlainClone(g.Name, false, a) _, err := gogit.PlainClone(g.Name, false, a)
if err != nil { if err != nil {
return nil, err return err
} }
return result, nil return nil
} }
func (g *GitOptions) Open() (*gogit.Repository, error) { func (g *GitOptions) Open() (*gogit.Repository, error) {
@@ -38,20 +40,24 @@ func (g *GitOptions) Open() (*gogit.Repository, error) {
} }
func (g *GitOptions) Fetch() error { func (g *GitOptions) Fetch() error {
a := &gogit.FetchOptions{
RemoteName: g.Name,
RemoteURL: g.URL,
}
if err := a.Validate(); err != nil {
return err
}
repo, err := g.Open() repo, err := g.Open()
if err != nil { if err != nil {
return err return err
} }
if err := repo.Fetch(a); err != nil { 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 err
} }

11
pkg/http/http.go Normal file
View 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()
}