2019-05-31 02:26:34 +03:00
|
|
|
// Package golang is a go package manager
|
|
|
|
package golang
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/build"
|
2019-05-31 02:26:34 +03:00
|
|
|
)
|
|
|
|
|
2020-08-11 18:51:58 +03:00
|
|
|
type goBuild struct {
|
2019-11-19 19:09:43 +03:00
|
|
|
Options build.Options
|
2019-05-31 02:26:34 +03:00
|
|
|
Cmd string
|
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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"
|
|
|
|
}
|
|
|
|
|
2020-08-11 18:51:58 +03:00
|
|
|
func (g *goBuild) Package(name string, src *build.Source) (*build.Package, error) {
|
|
|
|
binary := filepath.Join(g.Path, name)
|
|
|
|
source := src.Path
|
2019-05-31 02:26:34 +03:00
|
|
|
|
|
|
|
cmd := exec.Command(g.Cmd, "build", "-o", binary, source)
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-11 18:51:58 +03:00
|
|
|
|
2019-11-19 19:09:43 +03:00
|
|
|
return &build.Package{
|
2020-08-11 18:51:58 +03:00
|
|
|
Name: name,
|
2019-05-31 02:26:34 +03:00
|
|
|
Path: binary,
|
2020-08-11 18:51:58 +03:00
|
|
|
Type: g.String(),
|
|
|
|
Source: src,
|
2019-05-31 02:26:34 +03:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-08-11 18:51:58 +03:00
|
|
|
func (g *goBuild) Remove(b *build.Package) error {
|
2019-05-31 02:26:34 +03:00
|
|
|
binary := filepath.Join(b.Path, b.Name)
|
|
|
|
return os.Remove(binary)
|
|
|
|
}
|
|
|
|
|
2020-08-11 18:51:58 +03:00
|
|
|
func (g *goBuild) String() string {
|
|
|
|
return "golang"
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBuild(opts ...build.Option) build.Build {
|
2019-11-19 19:09:43 +03:00
|
|
|
options := build.Options{
|
2019-05-31 02:26:34 +03:00
|
|
|
Path: os.TempDir(),
|
|
|
|
}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
2020-08-11 18:51:58 +03:00
|
|
|
return &goBuild{
|
2019-05-31 02:26:34 +03:00
|
|
|
Options: options,
|
|
|
|
Cmd: whichGo(),
|
|
|
|
Path: options.Path,
|
|
|
|
}
|
|
|
|
}
|