goc/pkg/build/install.go

80 lines
2.2 KiB
Go
Raw Normal View History

2020-06-12 09:51:10 +00:00
/*
Copyright 2020 Qiniu Cloud (qiniu.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package build
import (
"fmt"
"os"
"os/exec"
2020-06-14 11:38:41 +00:00
log "github.com/sirupsen/logrus"
2020-06-12 09:51:10 +00:00
)
2020-06-13 10:15:51 +00:00
// NewInstall creates a Build struct which can install from goc temporary directory
2020-06-16 05:21:28 +00:00
func NewInstall(buildflags string, packages string) (*Build, error) {
2020-06-13 10:15:51 +00:00
b := &Build{
BuildFlags: buildflags,
Packages: packages,
}
if false == b.validatePackageForInstall() {
2020-06-16 05:21:28 +00:00
log.Errorln(ErrWrongPackageTypeForInstall)
return nil, ErrWrongPackageTypeForInstall
2020-06-13 10:15:51 +00:00
}
b.MvProjectsToTmp()
2020-06-16 05:21:28 +00:00
return b, nil
2020-06-13 10:15:51 +00:00
}
2020-06-16 05:21:28 +00:00
func (b *Build) Install() error {
2020-06-12 09:51:10 +00:00
log.Println("Go building in temp...")
2020-06-13 10:15:51 +00:00
cmd := exec.Command("/bin/bash", "-c", "go install "+b.BuildFlags+" "+b.Packages)
2020-06-12 09:51:10 +00:00
cmd.Dir = b.TmpWorkingDir
2020-06-16 05:21:28 +00:00
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
2020-06-12 09:51:10 +00:00
2020-06-16 05:21:28 +00:00
whereToInstall, err := b.findWhereToInstall()
if err != nil {
// ignore the err
log.Errorf("No place to install: %v", err)
}
2020-06-12 09:51:10 +00:00
// Change the temp GOBIN, to force binary install to original place
2020-06-16 05:21:28 +00:00
cmd.Env = append(os.Environ(), fmt.Sprintf("GOBIN=%v", whereToInstall))
2020-06-12 09:51:10 +00:00
if b.NewGOPATH != "" {
// Change to temp GOPATH for go install command
cmd.Env = append(cmd.Env, fmt.Sprintf("GOPATH=%v", b.NewGOPATH))
}
2020-06-16 05:21:28 +00:00
log.Infof("go install cmd is: %v", cmd.Args)
err = cmd.Start()
2020-06-12 09:51:10 +00:00
if err != nil {
2020-06-16 05:21:28 +00:00
log.Errorf("Fail to execute: %v. The error is: %v", cmd.Args, err)
return fmt.Errorf("fail to execute: %v: %w", cmd.Args, err)
}
if err = cmd.Wait(); err != nil {
log.Errorf("go install failed. The error is: %v", err)
return fmt.Errorf("go install failed: %w", err)
2020-06-12 09:51:10 +00:00
}
2020-06-16 05:21:28 +00:00
log.Infof("Go install successful. Binary installed in: %v", whereToInstall)
return nil
2020-06-12 09:51:10 +00:00
}
2020-06-13 10:15:51 +00:00
func (b *Build) validatePackageForInstall() bool {
if b.Packages == "." || b.Packages == "./..." {
return true
}
2020-06-14 11:38:41 +00:00
return false
2020-06-13 10:15:51 +00:00
}