當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。