goc install cmd

This commit is contained in:
lyyyuna 2021-06-23 12:51:25 +08:00
parent fb3bb02c2f
commit 64504ae67b
2 changed files with 84 additions and 0 deletions

22
cmd/install.go Normal file
View File

@ -0,0 +1,22 @@
package cmd
import (
"github.com/qiniu/goc/v2/pkg/build"
"github.com/spf13/cobra"
)
var installCmd = &cobra.Command{
Use: "install",
Run: installAction,
DisableFlagParsing: true, // install 命令需要用原生 go 的方式处理 flags
}
func init() {
rootCmd.AddCommand(installCmd)
}
func installAction(cmd *cobra.Command, args []string) {
b := build.NewInstall(cmd, args)
b.Install()
}

62
pkg/build/install.go Normal file
View File

@ -0,0 +1,62 @@
package build
import (
"os"
"os/exec"
"github.com/qiniu/goc/v2/pkg/config"
"github.com/qiniu/goc/v2/pkg/cover"
"github.com/qiniu/goc/v2/pkg/log"
"github.com/spf13/cobra"
)
func NewInstall(cmd *cobra.Command, args []string) *Build {
return NewBuild(cmd, args)
}
// Install starts go install
//
// 1. copy project to temp,
// 2. inject cover variables and functions into the project,
// 3. install the project in temp.
func (b *Build) Install() {
// 1. 拷贝至临时目录
b.copyProjectToTmp()
defer b.clean()
log.Donef("project copied to temporary directory")
// 2. inject cover vars
cover.Inject()
// 3. install in the temp project
b.doInstallInTemp()
}
func (b *Build) doInstallInTemp() {
log.StartWait("installing the injected project")
goflags := config.GocConfig.Goflags
pacakges := config.GocConfig.Packages
goflags = append(goflags, pacakges...)
args := []string{"install"}
args = append(args, goflags...)
// go 命令行由 go install [build flags] [packages] 组成
cmd := exec.Command("go", args...)
cmd.Dir = config.GocConfig.TmpWd
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
log.Infof("go install cmd is: %v, in path [%v]", cmd.Args, cmd.Dir)
if err := cmd.Start(); err != nil {
log.Fatalf("fail to execute go install: %v", err)
}
if err := cmd.Wait(); err != nil {
log.Fatalf("fail to execute go install: %v", err)
}
// done
log.StopWait()
log.Donef("go install done")
}