Add runtime => run
This commit is contained in:
87
runtime/source/git/git.go
Normal file
87
runtime/source/git/git.go
Normal file
@@ -0,0 +1,87 @@
|
||||
// Package git provides a git source
|
||||
package git
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/micro/go-run/source"
|
||||
"gopkg.in/src-d/go-git.v4"
|
||||
)
|
||||
|
||||
// Source retrieves source code
|
||||
// An empty struct can be used
|
||||
type Source struct {
|
||||
Options source.Options
|
||||
}
|
||||
|
||||
func (g *Source) Fetch(url string) (*source.Repository, error) {
|
||||
purl := url
|
||||
|
||||
if parts := strings.Split(url, "://"); len(parts) > 1 {
|
||||
purl = parts[len(parts)-1]
|
||||
}
|
||||
|
||||
name := filepath.Base(url)
|
||||
path := filepath.Join(g.Options.Path, purl)
|
||||
|
||||
_, err := git.PlainClone(path, false, &git.CloneOptions{
|
||||
URL: url,
|
||||
})
|
||||
if err == nil {
|
||||
return &source.Repository{
|
||||
Name: name,
|
||||
Path: path,
|
||||
URL: url,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// repo already exists
|
||||
if err != git.ErrRepositoryAlreadyExists {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// open repo
|
||||
re, err := git.PlainOpen(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// update it
|
||||
if err := re.Fetch(nil); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &source.Repository{
|
||||
Name: name,
|
||||
Path: path,
|
||||
URL: url,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (g *Source) Commit(r *source.Repository) error {
|
||||
repo := filepath.Join(r.Path)
|
||||
re, err := git.PlainOpen(repo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return re.Push(nil)
|
||||
}
|
||||
|
||||
func (g *Source) String() string {
|
||||
return "git"
|
||||
}
|
||||
|
||||
func NewSource(opts ...source.Option) *Source {
|
||||
options := source.Options{
|
||||
Path: os.TempDir(),
|
||||
}
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
|
||||
return &Source{
|
||||
Options: options,
|
||||
}
|
||||
}
|
||||
93
runtime/source/go/golang.go
Normal file
93
runtime/source/go/golang.go
Normal file
@@ -0,0 +1,93 @@
|
||||
// Package golang is a source for Go
|
||||
package golang
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/micro/go-run/source"
|
||||
)
|
||||
|
||||
type Source struct {
|
||||
Options source.Options
|
||||
// Go Command
|
||||
Cmd string
|
||||
Path string
|
||||
}
|
||||
|
||||
func (g *Source) Fetch(url string) (*source.Repository, error) {
|
||||
purl := url
|
||||
|
||||
if parts := strings.Split(url, "://"); len(parts) > 1 {
|
||||
purl = parts[len(parts)-1]
|
||||
}
|
||||
|
||||
// name of repo
|
||||
name := filepath.Base(url)
|
||||
// local path of repo
|
||||
path := filepath.Join(g.Path, purl)
|
||||
args := []string{"get", "-d", url, path}
|
||||
|
||||
cmd := exec.Command(g.Cmd, args...)
|
||||
if err := cmd.Run(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &source.Repository{
|
||||
Name: name,
|
||||
Path: path,
|
||||
URL: url,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Commit is not yet supported
|
||||
func (g *Source) Commit(r *source.Repository) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Source) String() string {
|
||||
return "golang"
|
||||
}
|
||||
|
||||
// whichGo locates the go command
|
||||
func whichGo() string {
|
||||
// check GOROOT
|
||||
if gr := os.Getenv("GOROOT"); len(gr) > 0 {
|
||||
return filepath.Join(gr, "bin", "go")
|
||||
}
|
||||
|
||||
// check path
|
||||
for _, p := range filepath.SplitList(os.Getenv("PATH")) {
|
||||
bin := filepath.Join(p, "go")
|
||||
if _, err := os.Stat(bin); err == nil {
|
||||
return bin
|
||||
}
|
||||
}
|
||||
|
||||
// best effort
|
||||
return "go"
|
||||
}
|
||||
|
||||
func NewSource(opts ...source.Option) source.Source {
|
||||
options := source.Options{
|
||||
Path: os.TempDir(),
|
||||
}
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
|
||||
cmd := whichGo()
|
||||
path := options.Path
|
||||
|
||||
// point of no return
|
||||
if len(cmd) == 0 {
|
||||
panic("Could not find Go executable")
|
||||
}
|
||||
|
||||
return &Source{
|
||||
Options: options,
|
||||
Cmd: cmd,
|
||||
Path: path,
|
||||
}
|
||||
}
|
||||
15
runtime/source/options.go
Normal file
15
runtime/source/options.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package source
|
||||
|
||||
type Options struct {
|
||||
// local path to download source
|
||||
Path string
|
||||
}
|
||||
|
||||
type Option func(o *Options)
|
||||
|
||||
// Local path for repository
|
||||
func Path(p string) Option {
|
||||
return func(o *Options) {
|
||||
o.Path = p
|
||||
}
|
||||
}
|
||||
22
runtime/source/source.go
Normal file
22
runtime/source/source.go
Normal file
@@ -0,0 +1,22 @@
|
||||
// Package source retrieves source code
|
||||
package source
|
||||
|
||||
// Source retrieves source code
|
||||
type Source interface {
|
||||
// Fetch repo from a url
|
||||
Fetch(url string) (*Repository, error)
|
||||
// Commit and upload repo
|
||||
Commit(*Repository) error
|
||||
// The sourcerer
|
||||
String() string
|
||||
}
|
||||
|
||||
// Repository is the source repository
|
||||
type Repository struct {
|
||||
// Name or repo
|
||||
Name string
|
||||
// Local path where repo is stored
|
||||
Path string
|
||||
// URL from which repo was retrieved
|
||||
URL string
|
||||
}
|
||||
Reference in New Issue
Block a user