admin: Create a GitImporter based on git binaries
This commit is contained in:
parent
23c43ad667
commit
43be59b97d
|
@ -28,6 +28,7 @@ steps:
|
|||
image: golang:alpine
|
||||
commands:
|
||||
- apk --no-cache add build-base
|
||||
- go vet -v -tags gitgo srs.epita.fr/fic-server/admin
|
||||
- go vet -v srs.epita.fr/fic-server/admin
|
||||
- go vet -v srs.epita.fr/fic-server/backend
|
||||
- go vet -v srs.epita.fr/fic-server/frontend
|
||||
|
@ -38,6 +39,7 @@ steps:
|
|||
- name: build admin
|
||||
image: golang:alpine
|
||||
commands:
|
||||
- go build -v -tags gitgo -o deploy/admin-gitgo-${DRONE_STAGE_OS}-${DRONE_STAGE_ARCH} srs.epita.fr/fic-server/admin
|
||||
- go build -v -o deploy/admin-${DRONE_STAGE_OS}-${DRONE_STAGE_ARCH} srs.epita.fr/fic-server/admin
|
||||
- tar chjf deploy/htdocs-admin.tar.bz2 htdocs-admin
|
||||
environment:
|
||||
|
|
|
@ -15,7 +15,12 @@ RUN go build -v -o admin/admin ./admin
|
|||
|
||||
FROM alpine
|
||||
|
||||
RUN apk add --no-cache openssl ca-certificates
|
||||
RUN apk add --no-cache \
|
||||
ca-certificates \
|
||||
git \
|
||||
git-lfs \
|
||||
openssh-client-default \
|
||||
openssl
|
||||
|
||||
EXPOSE 8081
|
||||
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
//go:build gitgo
|
||||
// +build gitgo
|
||||
|
||||
package sync
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
|
@ -62,15 +64,6 @@ func (i GitImporter) Kind() string {
|
|||
return "git originated from " + i.Remote + " on " + i.li.Kind() + ", currently on commit " + gitinfo
|
||||
}
|
||||
|
||||
func countFileInDir(dirname string) (int, error) {
|
||||
files, err := os.ReadDir(dirname)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return len(files), nil
|
||||
}
|
||||
|
||||
func (i GitImporter) Init() error {
|
||||
// Check if the directory exists, create it if needed
|
||||
if err := i.li.Init(); err != nil {
|
||||
|
@ -130,27 +123,3 @@ func (i GitImporter) Sync() error {
|
|||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (i GitImporter) exists(filename string) bool {
|
||||
return i.li.exists(filename)
|
||||
}
|
||||
|
||||
func (i GitImporter) toURL(filename string) string {
|
||||
return i.li.toURL(filename)
|
||||
}
|
||||
|
||||
func (i GitImporter) importFile(URI string, next func(string, string) (interface{}, error)) (interface{}, error) {
|
||||
return i.li.importFile(URI, next)
|
||||
}
|
||||
|
||||
func (i GitImporter) getFile(filename string, writer *bufio.Writer) error {
|
||||
return i.li.getFile(filename, writer)
|
||||
}
|
||||
|
||||
func (i GitImporter) listDir(filename string) ([]string, error) {
|
||||
return i.li.listDir(filename)
|
||||
}
|
||||
|
||||
func (i GitImporter) stat(filename string) (os.FileInfo, error) {
|
||||
return i.li.stat(filename)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
package sync
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
)
|
||||
|
||||
func countFileInDir(dirname string) (int, error) {
|
||||
files, err := os.ReadDir(dirname)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return len(files), nil
|
||||
}
|
||||
|
||||
func (i GitImporter) exists(filename string) bool {
|
||||
return i.li.exists(filename)
|
||||
}
|
||||
|
||||
func (i GitImporter) toURL(filename string) string {
|
||||
return i.li.toURL(filename)
|
||||
}
|
||||
|
||||
func (i GitImporter) importFile(URI string, next func(string, string) (interface{}, error)) (interface{}, error) {
|
||||
return i.li.importFile(URI, next)
|
||||
}
|
||||
|
||||
func (i GitImporter) getFile(filename string, writer *bufio.Writer) error {
|
||||
return i.li.getFile(filename, writer)
|
||||
}
|
||||
|
||||
func (i GitImporter) listDir(filename string) ([]string, error) {
|
||||
return i.li.listDir(filename)
|
||||
}
|
||||
|
||||
func (i GitImporter) stat(filename string) (os.FileInfo, error) {
|
||||
return i.li.stat(filename)
|
||||
}
|
|
@ -0,0 +1,83 @@
|
|||
//go:build !gitgo
|
||||
// +build !gitgo
|
||||
|
||||
package sync
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// GitImporter implements an Importer, where files to imports are located
|
||||
// inside a local directory from your filesystem, backed by git (binary).
|
||||
type GitImporter struct {
|
||||
li LocalImporter
|
||||
Remote string
|
||||
}
|
||||
|
||||
func NewGitImporter(li LocalImporter, remote string) GitImporter {
|
||||
return GitImporter{
|
||||
li: li,
|
||||
Remote: remote,
|
||||
}
|
||||
}
|
||||
|
||||
func (i GitImporter) Kind() string {
|
||||
cmdshow := exec.Command("git", "-C", i.li.Base, "show")
|
||||
var outshow bytes.Buffer
|
||||
cmdshow.Stdout = &outshow
|
||||
err := cmdshow.Run()
|
||||
|
||||
var commit string
|
||||
if err != nil {
|
||||
commit = fmt.Sprintf("error (%s)", err.Error())
|
||||
} else {
|
||||
commit, err = outshow.ReadString('\n')
|
||||
if err == nil {
|
||||
commit = strings.TrimPrefix(commit, "commit ")
|
||||
} else {
|
||||
commit = fmt.Sprintf("error (%s)", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
return "git originated from " + i.Remote + " on " + i.li.Kind() + ", currently on commit " + commit
|
||||
}
|
||||
|
||||
func (i GitImporter) Init() error {
|
||||
// Check if the directory exists, create it if needed
|
||||
if err := i.li.Init(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// If the directory is empty, clone it
|
||||
if n, err := countFileInDir(i.li.Base); err != nil {
|
||||
return err
|
||||
} else if n == 0 {
|
||||
cmdclone := exec.Command("git", "-C", i.li.Base, "clone", "--recursive", i.Remote)
|
||||
stdout, err := cmdclone.CombinedOutput()
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w:\n%s", err, stdout)
|
||||
}
|
||||
}
|
||||
|
||||
// Check if the .git directory exists, change the origin remote to our
|
||||
cmdremote := exec.Command("git", "-C", i.li.Base, "remote", "set-url", "origin", i.Remote)
|
||||
stdout, err := cmdremote.CombinedOutput()
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w:\n%s", err, stdout)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i GitImporter) Sync() error {
|
||||
cmdremote := exec.Command("git", "-C", i.li.Base, "pull", "--rebase", "--force", "--recurse-submodules=yes", "origin")
|
||||
stdout, err := cmdremote.CombinedOutput()
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w:\n%s", err, stdout)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue