52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
|
package build
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"os"
|
||
|
"path/filepath"
|
||
|
)
|
||
|
|
||
|
type Fetcher interface {
|
||
|
Fetch(tempDir string) error
|
||
|
}
|
||
|
|
||
|
type BlankFetcher struct{}
|
||
|
|
||
|
func (BlankFetcher) Fetch(tempDir string) error {
|
||
|
return fmt.Errorf("no sources were configured")
|
||
|
}
|
||
|
|
||
|
var _ Fetcher = (*GitFetcher)(nil)
|
||
|
|
||
|
type GitFetcher struct {
|
||
|
Repository, Ref string
|
||
|
}
|
||
|
|
||
|
func (g GitFetcher) Fetch(tempDir string) error {
|
||
|
|
||
|
repoDir := filepath.Join(tempDir, "repo")
|
||
|
exitCode, out := execCommand("git", "clone", "--depth=1", g.Repository, repoDir)
|
||
|
if exitCode != 0 {
|
||
|
return fmt.Errorf("git clone returned %d, output: %v", exitCode, string(out))
|
||
|
}
|
||
|
|
||
|
err := os.Chdir(repoDir)
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("changing to repository directory: %w", err)
|
||
|
}
|
||
|
|
||
|
if g.Ref != "" {
|
||
|
exitCode, out = execCommand("git", "fetch", "--depth=1", "origin", g.Ref)
|
||
|
if exitCode != 0 {
|
||
|
return fmt.Errorf("fetch clone returned %d, output: %v", exitCode, string(out))
|
||
|
}
|
||
|
|
||
|
exitCode, out = execCommand("git", "checkout", g.Ref)
|
||
|
if exitCode != 0 {
|
||
|
return fmt.Errorf("git checkout returned %d, output: %v", exitCode, string(out))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|