当前位置: 首页>>代码示例>>Golang>>正文


Golang file.SelfDir函数代码示例

本文整理汇总了Golang中github.com/toolkits/file.SelfDir函数的典型用法代码示例。如果您正苦于以下问题:Golang SelfDir函数的具体用法?Golang SelfDir怎么用?Golang SelfDir使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了SelfDir函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: InsureNewVersionFiles

func InsureNewVersionFiles(da *model.DesiredAgent) error {
	if FilesReady(da) {
		return nil
	}

	downloadTarballCmd := BuildCommand(da.RunUser, "wget", "-q", da.TarballUrl, "-O", path.Join(da.AgentVersionDir, da.TarballFilename))
	downloadTarballCmd.Dir = file.SelfDir()
	_, err := ExecuteCommandWithOutput(downloadTarballCmd)
	if err != nil {
		logger.Errorln("wget -q", da.TarballUrl, "-O", da.TarballFilename, "fail", err)
		return err
	}

	downloadMd5Cmd := BuildCommand(da.RunUser, "wget", "-q", da.Md5Url, "-O", path.Join(da.AgentVersionDir, da.Md5Filename))
	downloadMd5Cmd.Dir = file.SelfDir()
	_, err = ExecuteCommandWithOutput(downloadMd5Cmd)
	if err != nil {
		log.Println("wget -q", da.Md5Url, "-O", da.Md5Filename, "fail", err)
		return err
	}

	if "" != da.ConfigFileName && "" != da.ConfigRemoteUrl {
		downloadConfigCmd := BuildCommand(da.RunUser, "wget", "-q", da.ConfigRemoteUrl, "-O", path.Join(da.AgentVersionDir, da.ConfigFileName))
		downloadConfigCmd.Dir = file.SelfDir()
		_, err := ExecuteCommandWithOutput(downloadConfigCmd)
		if err != nil {
			logger.Errorln("wget -q", da.ConfigRemoteUrl, "-O", da.ConfigFileName, "fail", err)
		}
		return err
	}

	return Md5sumCheck(da.RunUser, da.AgentVersionDir, da.TarballFilename, da.Md5Filename)
}
开发者ID:coraldane,项目名称:ops-updater,代码行数:33,代码来源:start.go

示例2: WriteVersion

func WriteVersion(da *model.DesiredAgent) (err error) {
	if CurrentUser == da.RunUser {
		file.WriteString(path.Join(da.AgentDir, ".version"), da.Version)
	} else {
		file.WriteString(path.Join(file.SelfDir(), ".version"), da.Version)

		_, err = utils.ExecuteCommand(file.SelfDir(), fmt.Sprintf("sudo mv .version %s/", da.AgentDir))
		if nil != err {
			return
		}
		_, err = utils.ExecuteCommand(file.SelfDir(), fmt.Sprintf("sudo chown -R %s:%s %s", da.RunUser, da.RunUser, path.Join(da.AgentDir, ".version")))
	}
	return
}
开发者ID:coraldane,项目名称:ops-updater,代码行数:14,代码来源:start.go

示例3: StopAgentOf

func StopAgentOf(da *model.DesiredAgent, lastRunUser, lastWorkDir string) error {
	agentDir := path.Join(lastWorkDir, da.Name)
	version := ReadVersion(lastRunUser, agentDir)

	if "" == version {
		version = da.Version
	}
	if version == da.Version && da.RunUser == lastRunUser && da.WorkDir == lastWorkDir {
		// do nothing
		return nil
	}

	versionDir := path.Join(agentDir, version)
	if !CheckDirectoryExists(lastRunUser, versionDir) {
		logger.Warn("user: %s, %s nonexistent", lastRunUser, versionDir)
		return nil
	}

	err := ControlStopIn(lastRunUser, versionDir)
	if nil != err {
		return err
	}

	cmd := BuildCommand(lastRunUser, "rm", "-rf", versionDir)
	cmd.Dir = file.SelfDir()
	_, err = ExecuteCommandWithOutput(cmd)
	return err
}
开发者ID:coraldane,项目名称:ops-updater,代码行数:28,代码来源:stop.go

示例4: Md5sumCheck

func Md5sumCheck(runUser, workdir, tarfile, md5file string) error {
	var cmd *exec.Cmd
	var md5Actual string
	if "darwin" == runtime.GOOS {
		cmd = BuildCommand(runUser, "md5", "-q", path.Join(workdir, tarfile))
	} else {
		cmd = BuildCommand(runUser, "md5sum", path.Join(workdir, tarfile))
	}
	cmd.Dir = file.SelfDir()
	bs, err := cmd.CombinedOutput()
	if err != nil {
		return fmt.Errorf("cd %s; md5sum -c %s fail", workdir, md5file)
	}
	strMd5file, _ := file.ToString(path.Join(workdir, md5file))
	if "" == strMd5file {
		return fmt.Errorf("md5file is empty")
	}

	if "darwin" == runtime.GOOS {
		md5Actual = strings.Replace(string(bs), "\n", "", -1)
	} else {
		md5Actual = strings.Fields(string(bs))[0]
	}

	md5Except := strings.Fields(strMd5file)[0]

	if md5Actual == md5Except {
		return nil
	}
	return fmt.Errorf("md5Actual:%s, md5Except:%s<<<===end", md5Actual, md5Except)
}
开发者ID:coraldane,项目名称:ops-updater,代码行数:31,代码来源:start.go

示例5: configCommonRoutes

func configCommonRoutes() {
	http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("ok"))
	})

	http.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte(g.VERSION))
	})

	http.HandleFunc("/workdir", func(w http.ResponseWriter, r *http.Request) {
		RenderDataJson(w, file.SelfDir())
	})

	http.HandleFunc("/config", func(w http.ResponseWriter, r *http.Request) {
		RenderDataJson(w, g.Config())
	})

	http.HandleFunc("/config/reload", func(w http.ResponseWriter, r *http.Request) {
		if strings.HasPrefix(r.RemoteAddr, "127.0.0.1") {
			g.ParseConfig(g.ConfigFile)
			RenderDataJson(w, "ok")
		} else {
			RenderDataJson(w, "no privilege")
		}
	})
}
开发者ID:masato25,项目名称:graph,代码行数:26,代码来源:common.go

示例6: CheckUserExists

func CheckUserExists(username string) bool {
	_, err := ExecuteCommand(file.SelfDir(), fmt.Sprintf("id -u %s", username))
	if nil != err {
		return false
	}
	return true
}
开发者ID:coraldane,项目名称:ops-common,代码行数:7,代码来源:system.go

示例7: configAdminRoutes

func configAdminRoutes() {
	http.HandleFunc("/exit", func(w http.ResponseWriter, r *http.Request) {
		if g.IsTrustable(r.RemoteAddr) {
			w.Write([]byte("exiting..."))
			go func() {
				time.Sleep(time.Second)
				os.Exit(0)
			}()
		} else {
			w.Write([]byte("no privilege"))
		}
	})

	http.HandleFunc("/config/reload", func(w http.ResponseWriter, r *http.Request) {
		if g.IsTrustable(r.RemoteAddr) {
			g.ParseConfig(g.ConfigFile)
			RenderDataJson(w, g.Config())
		} else {
			w.Write([]byte("no privilege"))
		}
	})

	http.HandleFunc("/workdir", func(w http.ResponseWriter, r *http.Request) {
		RenderDataJson(w, file.SelfDir())
	})

	http.HandleFunc("/ips", func(w http.ResponseWriter, r *http.Request) {
		RenderDataJson(w, g.TrustableIps())
	})
}
开发者ID:tplink-zgc,项目名称:falcon-agent,代码行数:30,代码来源:admin.go

示例8: Control

func Control(runUser, workdir, arg string) (string, error) {
	cmd := BuildCommand(runUser, path.Join(workdir, "control"), arg)
	cmd.Dir = file.SelfDir()
	out, err := ExecuteCommandWithOutput(cmd)
	if err != nil {
		logger.Error("cd %s; ./control %s fail %v. output: %s\n", workdir, arg, err, out)
	}
	return out, err
}
开发者ID:coraldane,项目名称:ops-updater,代码行数:9,代码来源:control.go

示例9: GetUserByPid

func GetUserByPid(pid int) string {
	cmd := exec.Command("sh", "-c", fmt.Sprintf("ps aux|awk '{if(%d==$2){print $1}}'", pid))
	cmd.Dir = file.SelfDir()
	bs, err := cmd.CombinedOutput()
	if nil != err {
		log.Println("getUserByPid error", err)
		return ""
	}
	return strings.Replace(string(bs), "\n", "", -1)
}
开发者ID:coraldane,项目名称:ops-common,代码行数:10,代码来源:system.go

示例10: Untar

func Untar(da *model.DesiredAgent) error {
	cmd := BuildCommand(da.RunUser, "tar", "zxf", path.Join(da.AgentVersionDir, da.TarballFilename), "-C", da.AgentVersionDir)
	cmd.Dir = file.SelfDir()
	err := cmd.Run()
	if err != nil {
		log.Println("tar zxf", da.TarballFilename, "fail", err)
		return err
	}
	return nil
}
开发者ID:coraldane,项目名称:ops-updater,代码行数:10,代码来源:start.go

示例11: ReadVersion

func ReadVersion(username, agentDir string) string {
	versionFile := path.Join(agentDir, ".version")
	cmd := BuildCommand(username, "sh", "read_file.sh", versionFile)
	cmd.Dir = file.SelfDir()
	version, err := ExecuteCommandWithOutput(cmd)
	if err != nil {
		logger.Warn("%s is nonexistent,error: %v", versionFile, err)
		return ""
	}
	return version
}
开发者ID:coraldane,项目名称:ops-updater,代码行数:11,代码来源:control.go

示例12: InsureRunUserExists

func InsureRunUserExists(da *model.DesiredAgent) error {
	if CurrentUser == da.RunUser { //ops-updater和 Agent运行用户一致
		return nil
	} else if HasSudoPermission {
		if utils.CheckUserExists(da.RunUser) {
			return nil
		}
		_, err := utils.ExecuteCommand(file.SelfDir(), fmt.Sprintf("sudo useradd %s", da.RunUser))
		return err
	}
	return fmt.Errorf("you donot have permission to insure user %s", da.RunUser)
}
开发者ID:coraldane,项目名称:ops-updater,代码行数:12,代码来源:start.go

示例13: InsureUserDir

func InsureUserDir(fp, username string, createByRoot bool) error {
	var err error
	if CheckDirectoryExists(username, fp) {
		return nil
	}

	if CurrentUser == username {
		return os.MkdirAll(fp, os.ModePerm)
	} else if HasSudoPermission {
		if createByRoot {
			_, err = utils.ExecuteCommand(file.SelfDir(), fmt.Sprintf("sudo mkdir -p %s", fp))
			if nil != err {
				return err
			}
			_, err = utils.ExecuteCommand(file.SelfDir(), fmt.Sprintf("sudo chown -R %s:%s %s", username, username, fp))
		} else {
			_, err = utils.ExecuteCommand(file.SelfDir(), fmt.Sprintf("sudo -u %s mkdir -p %s", username, fp))
		}
	}
	return err
}
开发者ID:coraldane,项目名称:ops-updater,代码行数:21,代码来源:start.go

示例14: CheckFileOrDirExists

func CheckFileOrDirExists(username, fp, fileType string) bool {
	if CurrentUser == username {
		return file.IsExist(fp)
	} else {
		cmd := BuildCommand(username, "sh", "check_file.sh", fileType, fp)
		cmd.Dir = file.SelfDir()
		strOut, err := ExecuteCommandWithOutput(cmd)
		if nil != err {
			logger.Errorln("check dir exists", strOut, err)
			return false
		}
		result, _ := strconv.ParseBool(strings.Replace(strOut, "\n", "", -1))
		return result
	}
	return false
}
开发者ID:coraldane,项目名称:ops-updater,代码行数:16,代码来源:control.go

示例15: configCommonRoutes

func configCommonRoutes() {
	http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("ok\n"))
	})

	http.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte(fmt.Sprintf("%s\n", g.VERSION)))
	})

	http.HandleFunc("/workdir", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte(fmt.Sprintf("%s\n", file.SelfDir())))
	})

	http.HandleFunc("/config", func(w http.ResponseWriter, r *http.Request) {
		RenderDataJson(w, g.Config())
	})
}
开发者ID:niean,项目名称:anteye,代码行数:17,代码来源:common.go


注:本文中的github.com/toolkits/file.SelfDir函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。