示例#1
0
文件: path.go 项目: essentialkaos/ek
func evalHome(path string) string {
	if path == "" || path[0:1] != "~" {
		return path
	}

	return env.Get()["HOME"] + path[1:]
}
示例#2
0
// renderTemplate read template and render to file
func renderTemplate(doc *Document) {
	projectDir := env.Get().GetS("GOPATH")
	templateFile := path.Join(
		projectDir, "src/github.com/essentialkaos/shdoc/templates",
		arg.GetS(ARG_TEMPLATE)+".tpl",
	)

	if !fsutil.CheckPerms("FRS", templateFile) {
		printError("Can't read template %s - file is not exist or empty", templateFile)
		os.Exit(1)
	}

	outputFile := arg.GetS(ARG_OUTPUT)

	if fsutil.IsExist(outputFile) {
		os.Remove(outputFile)
	}

	fd, err := os.OpenFile(outputFile, os.O_CREATE|os.O_WRONLY, 0644)

	if err != nil {
		printError(err.Error())
		os.Exit(1)
	}

	defer fd.Close()

	tpl, err := ioutil.ReadFile(templateFile)

	if err != nil {
		printError(err.Error())
		os.Exit(1)
	}

	t := template.New("Template")
	t, err = t.Parse(string(tpl[:]))

	err = t.Execute(fd, doc)

	if err != nil {
		printError(err.Error())
		os.Exit(1)
	}

	fmtutil.Separator(false, doc.Title)

	fmtc.Printf("  {*}Constants:{!} %d\n", len(doc.Constants))
	fmtc.Printf("  {*}Variables:{!} %d\n", len(doc.Variables))
	fmtc.Printf("  {*}Methods:{!}   %d\n", len(doc.Methods))
	fmtc.NewLine()
	fmtc.Printf(
		"  {*}Output:{!} %s {s-}(%s){!}\n", outputFile,
		fmtutil.PrettySize(fsutil.GetSize(outputFile)),
	)

	fmtutil.Separator(false)
}
示例#3
0
文件: user.go 项目: essentialkaos/ek
// getRealUserFromEnv try to find info about real user in environment variables
func getRealUserFromEnv() (string, int, int) {
	envMap := env.Get()

	if envMap["SUDO_USER"] == "" || envMap["SUDO_UID"] == "" || envMap["SUDO_GID"] == "" {
		return "", -1, -1
	}

	user := envMap["SUDO_USER"]
	uid, _ := strconv.Atoi(envMap["SUDO_UID"])
	gid, _ := strconv.Atoi(envMap["SUDO_GID"])

	return user, uid, gid
}
示例#4
0
func (s *ReqSuite) SetUpSuite(c *C) {
	s.url = "http://127.0.0.1:" + _DEFAULT_PORT

	envVars := env.Get()

	if envVars["EK_TEST_PORT"] != "" {
		s.url = "http://127.0.0.1:" + envVars["EK_TEST_PORT"]
	}

	SetDialTimeout(60.0)
	SetRequestTimeout(60.0)
	SetUserAgent("req-test", "5", "Test/5.1.1", "Magic/4.2.1")

	go runHTTPServer(s, c)

	time.Sleep(time.Second)
}
示例#5
0
func runHTTPServer(s *ReqSuite, c *C) {
	server := &http.Server{
		Handler:        http.NewServeMux(),
		ReadTimeout:    10 * time.Second,
		WriteTimeout:   10 * time.Second,
		MaxHeaderBytes: 1 << 20,
	}

	port := _DEFAULT_PORT
	envVars := env.Get()

	if envVars["EK_TEST_PORT"] != "" {
		port = envVars["EK_TEST_PORT"]
	}

	listener, err := net.Listen("tcp", ":"+port)

	if err != nil {
		c.Fatal(err.Error())
	}

	s.listener = listener

	server.Handler.(*http.ServeMux).HandleFunc(_URL_GET, getRequestHandler)
	server.Handler.(*http.ServeMux).HandleFunc(_URL_POST, postRequestHandler)
	server.Handler.(*http.ServeMux).HandleFunc(_URL_PUT, putRequestHandler)
	server.Handler.(*http.ServeMux).HandleFunc(_URL_HEAD, headRequestHandler)
	server.Handler.(*http.ServeMux).HandleFunc(_URL_PATCH, patchRequestHandler)
	server.Handler.(*http.ServeMux).HandleFunc(_URL_DELETE, deleteRequestHandler)
	server.Handler.(*http.ServeMux).HandleFunc(_URL_QUERY, queryRequestHandler)
	server.Handler.(*http.ServeMux).HandleFunc(_URL_HEADERS, headersRequestHandler)
	server.Handler.(*http.ServeMux).HandleFunc(_URL_CONTENT_TYPE, contentTypeRequestHandler)
	server.Handler.(*http.ServeMux).HandleFunc(_URL_ACCEPT, acceptRequestHandler)
	server.Handler.(*http.ServeMux).HandleFunc(_URL_USER_AGENT, uaRequestHandler)
	server.Handler.(*http.ServeMux).HandleFunc(_URL_BASIC_AUTH, basicAuthRequestHandler)
	server.Handler.(*http.ServeMux).HandleFunc(_URL_STRING_RESP, stringRespRequestHandler)
	server.Handler.(*http.ServeMux).HandleFunc(_URL_JSON_RESP, jsonRespRequestHandler)
	server.Handler.(*http.ServeMux).HandleFunc(_URL_DISCARD, discardRequestHandler)

	err = server.Serve(listener)

	if err != nil {
		c.Fatal(err.Error())
	}
}
示例#6
0
func (s *PathUtilSuite) TestEvalHome(c *C) {
	homeDir := env.Get()["HOME"]

	c.Assert(Clean("~/path"), Equals, homeDir+"/path")
	c.Assert(Clean("/path"), Equals, "/path")
}