Beispiel #1
0
func LinuxGetAllProcessByBinName(binName string) []*Process {
	if !kmgPlatform.LinuxAmd64.Compatible(kmgPlatform.GetCompiledPlatform()) {
		panic(kmgSys.ErrPlatformNotSupport)
	}
	b, err := kmgCmd.CmdBash(CmdProcessByBinName(binName)).GetExecCmd().CombinedOutput()
	if err != err {
		fmt.Println(err)
	}
	return Extract(string(b))
}
Beispiel #2
0
func goRunPackageName(goPath string, pathOrPkg string) {
	//goRunInstall(goPath, pathOrPkg)
	goRunInstallSimple(goPath, pathOrPkg)
	//run
	outPath := filepath.Join(goPath, "bin", filepath.Base(pathOrPkg))
	p := kmgPlatform.GetCompiledPlatform()
	if p.Compatible(kmgPlatform.WindowsAmd64) {
		outPath += ".exe"
	}
	if !kmgFile.MustFileExist(outPath) {
		kmgConsole.ExitOnErr(fmt.Errorf("please make sure you are kmg gorun a main package. (binary file not exist. %s)", outPath))
	}
	runCmdSliceWithGoPath(goPath, append([]string{outPath}, os.Args[2:]...))
}
Beispiel #3
0
func installGolangWithUrlMap(urlMap map[string]string) {
	p := kmgPlatform.GetCompiledPlatform()
	if p.Compatible(kmgPlatform.WindowsAmd64) {
		contentB, err := kmgHttp.UrlGetContent(urlMap["windows_amd64"])
		kmgConsole.ExitOnErr(err)
		kmgFile.MustDelete(`c:\go`)
		err = kmgCompress.ZipUncompressFromBytesToDir(contentB, `c:\go`, "go")
		kmgConsole.ExitOnErr(err)
		err = kmgFile.CopyFile(`c:\go\bin\go.exe`, `c:\windows\system32\go.exe`)
		kmgConsole.ExitOnErr(err)
		err = kmgFile.CopyFile(`c:\go\bin\godoc.exe`, `c:\windows\system32\godoc.exe`)
		kmgConsole.ExitOnErr(err)
		err = kmgFile.CopyFile(`c:\go\bin\gofmt.exe`, `c:\windows\system32\gofmt.exe`)
		kmgConsole.ExitOnErr(err)
		return
	}
	tmpPath := kmgFile.MustChangeToTmpPath()
	defer kmgFile.MustDelete(tmpPath)
	if !kmgSys.MustIsRoot() {
		fmt.Println("you need to be root to install golang")
		return
	}

	url, ok := urlMap[p.String()]
	if !ok {
		kmgConsole.ExitOnErr(fmt.Errorf("not support platform [%s]", p))
	}
	packageName := path.Base(url)
	contentB := kmgHttp.MustUrlGetContentProcess(url)

	kmgFile.MustWriteFile(packageName, contentB)
	kmgCmd.ProxyRun("tar -xf " + packageName)
	if kmgFile.MustFileExist("/usr/local/go") {
		kmgCmd.ProxyRun("mv /usr/local/go /usr/local/go.bak." + time.Now().Format(kmgTime.FormatFileNameV2))
	}
	kmgCmd.ProxyRun("cp -rf go /usr/local")
	kmgFile.MustDeleteFile("/bin/go")
	kmgCmd.ProxyRun("ln -s /usr/local/go/bin/go /bin/go")
	kmgFile.MustDeleteFile("/bin/godoc")
	kmgCmd.ProxyRun("ln -s /usr/local/go/bin/godoc /bin/godoc")
	kmgFile.MustDeleteFile("/bin/gofmt")
	kmgCmd.ProxyRun("ln -s /usr/local/go/bin/gofmt /bin/gofmt")
	kmgFile.MustEnsureBinPath("/bin/go")
	kmgFile.MustEnsureBinPath("/bin/godoc")
	kmgFile.MustEnsureBinPath("/bin/gofmt")
}
Beispiel #4
0
func runCommand(kmgc *kmgConfig.Env, args []string) {
	os.Chdir(kmgc.ProjectPath)
	logDir := filepath.Join(kmgc.LogPath, "run")
	kmgFile.MustMkdirAll(logDir)
	thisLogFilePath := filepath.Join(logDir, time.Now().Format(kmgTime.FormatFileName)+".log")
	kmgFile.MustWriteFile(thisLogFilePath, []byte{})
	if !kmgPlatform.GetCompiledPlatform().Compatible(kmgPlatform.WindowsAmd64) {
		lastLogPath := filepath.Join(logDir, "last.log")
		if kmgFile.MustFileExist(lastLogPath) {
			kmgFile.MustSymlink(kmgFile.MustReadSymbolLink(lastLogPath), filepath.Join(logDir, "last2.log"))
		}
		kmgFile.MustSymlink(filepath.Base(thisLogFilePath), lastLogPath)
	}
	//TODO 大部分命令是 kmg gorun xxx 在这个地方可以直接调用gorun解决问题,这样可以少开一个进程加快了一些速度
	// 问题: 上述做法不靠谱,会导致last.log没有用处.
	//if len(args) >= 2 && args[0] == "kmg" && strings.EqualFold(args[1], "gorun") {
	//	os.Args = append(args[1:], os.Args[1:]...)
	//	goCmd.GoRunCmd()
	//	return
	//}
	// 下面的做法不靠谱,会让signle无法传递
	//err := kmgCmd.CmdSlice(append(args, os.Args[1:]...)).
	//	SetDir(kmgc.ProjectPath).
	//	RunAndTeeOutputToFile(thisLogFilePath)
	// TODO bash转义
	bashCmd := strings.Join(append(args, os.Args[1:]...), " ")
	bashCmdStr := bashCmd + " 2>&1 | tee -i " + thisLogFilePath + " ; test ${PIPESTATUS[0]} -eq 0"
	if kmgPlatform.IsWindows() {
		err := kmgCmd.CmdString(bashCmdStr).SetDir(kmgc.ProjectPath).StdioRun()
		if err != nil {
			err = fmt.Errorf("kmg make: %s", err)
			kmgConsole.ExitOnErr(err)
		}
		return
	} else {
		err := kmgCmd.CmdBash(bashCmdStr).SetDir(kmgc.ProjectPath).StdioRun()
		if err != nil {
			err = fmt.Errorf("kmg make: %s", err)
			kmgConsole.ExitOnErr(err)
		}
		return
	}
}
Beispiel #5
0
func goRunInstall(goPath string, pathOrPkg string) {
	//只能更新本GOPATH里面的pkg,不能更多多个GOPATH里面其他GOPATH的pkg缓存.
	// go install 已知bug1 删除某个package里面的部分文件,然后由于引用到了旧的实现的代码,不会报错.删除pkg解决问题.
	// go install 已知bug2 如果一个package先是main,然后build了一个东西,然后又改成了非main,再gorun会使用旧的缓存/bin/里面的缓存.
	// TODO 已知bug3 当同一个项目的多个编译目标使用了同一个pkg,然后这个pkg变化了,缓存会出现A/B 问题,导致缓存完全无用.
	ctx := &goRunInstallContext{
		platform:          kmgPlatform.GetCompiledPlatform().String(),
		targetPkgMapCache: map[string]bool{},
		pkgMapCache:       map[string]*gorunCachePkgInfo{},
	}
	ctx.targetCachePath = filepath.Join(goPath, "tmp", "gorun", kmgCrypto.Md5HexFromString("target_"+pathOrPkg+"_"+ctx.platform))
	ctx.pkgCachePath = filepath.Join(goPath, "tmp", "gorun", "allPkgCache")
	ok := ctx.goRunInstallIsValidAndInvalidCache(goPath, pathOrPkg)
	if ok {
		//fmt.Println("use cache")
		return
	}
	//fmt.Println("not use cache")
	runCmdSliceWithGoPath(goPath, []string{"go", "install", pathOrPkg})
	// 填充缓存
	platform := ctx.platform
	ctx.targetPkgMapCache = map[string]bool{}
	outputJson := kmgCmd.CmdSlice([]string{"go", "list", "-json", pathOrPkg}).
		MustSetEnv("GOPATH", goPath).MustCombinedOutput()
	listObj := &struct {
		Deps []string
		Name string
	}{}
	kmgJson.MustUnmarshal(outputJson, &listObj)
	if listObj.Name != "main" {
		fmt.Printf("run non main package %s\n", pathOrPkg)
		return
	}
	listObj.Deps = append(listObj.Deps, pathOrPkg)
	for _, pkgName := range listObj.Deps {
		srcpkgPath := filepath.Join(goPath, "src", pkgName)
		fileList, err := kmgFile.ReadDirFileOneLevel(srcpkgPath)
		if err != nil {
			if !os.IsNotExist(err) {
				panic(err)
			}
			// 没有找到pkg,可能是这个pkg在GOROOT出现过,此处暂时不管.
			continue
		}
		ctx.targetPkgMapCache[pkgName] = true
		pkgInfo := &gorunCachePkgInfo{
			GoFileMap: map[string]uint64{},
		}
		for _, file := range fileList {
			ext := filepath.Ext(file)
			if ext == ".go" {
				pkgInfo.GoFileMap[file] = mustCheckSumFile(filepath.Join(srcpkgPath, file))
			}
		}
		pkgInfo.IsMain = pkgName == pathOrPkg
		pkgInfo.Name = pkgName
		pkgBinPath := pkgInfo.getPkgBinPath(goPath, platform)
		pkgInfo.PkgMd5 = mustCheckSumFile(pkgBinPath)
		ctx.pkgMapCache[pkgName] = pkgInfo
	}
	kmgGob.MustWriteFile(ctx.targetCachePath, ctx.targetPkgMapCache)
	kmgGob.MustWriteFile(ctx.pkgCachePath, ctx.pkgMapCache)
}