Пример #1
0
func (s *PidSuite) TestPidFuncs(c *C) {
	Dir = s.Dir

	err := Create("test.pid")

	c.Assert(err, IsNil)

	c.Assert(fsutil.IsExist(Dir+"/test.pid"), Equals, true)
	c.Assert(fsutil.IsReadable(Dir+"/test.pid"), Equals, true)
	c.Assert(fsutil.IsReadable(Dir+"/test.pid"), Equals, true)
	c.Assert(fsutil.IsNonEmpty(Dir+"/test.pid"), Equals, true)

	err = Create("test")

	c.Assert(err, IsNil)

	pid := Get("test")

	c.Assert(pid, Not(Equals), -1)
	c.Assert(os.Getpid(), Equals, pid)

	Remove("test")

	c.Assert(fsutil.IsExist(Dir+"/test.pid"), Equals, false)
	c.Assert(IsWorks("test"), Equals, false)
}
Пример #2
0
// NewTemp create new Temp structure
func NewTemp(args ...string) (*Temp, error) {
	tempDir := path.Clean(Dir)

	if len(args) != 0 {
		tempDir = path.Clean(args[0])
	}

	if !fsutil.IsExist(tempDir) {
		return nil, fmt.Errorf("Directory %s is not exist", tempDir)
	}

	if !fsutil.IsDir(tempDir) {
		return nil, fmt.Errorf("%s is not a directory", tempDir)
	}

	if !fsutil.IsWritable(tempDir) {
		return nil, fmt.Errorf("Directory %s is not writable", tempDir)
	}

	return &Temp{
		Dir:       tempDir,
		DirPerms:  DefaultDirPerms,
		FilePerms: DefaultFilePerms,
	}, nil
}
Пример #3
0
// IsWorks return if process with pid from pid file is works
func IsWorks(name string) bool {
	pid := Get(name)

	if pid == -1 {
		return false
	}

	return fsutil.IsExist(fmt.Sprintf("/proc/%d", pid))
}
Пример #4
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)
}
Пример #5
0
func process(file string, pattern string) {
	if !fsutil.IsExist(file) {
		printError("File %s is not exist", file)
		os.Exit(1)
	}

	if !fsutil.IsReadable(file) {
		printError("File %s is not readable", file)
		os.Exit(1)
	}

	if !fsutil.IsNonEmpty(file) {
		printError("File %s is empty", file)
		os.Exit(1)
	}

	doc, errs := Parse(file)

	if len(errs) != 0 {
		printError("Shell script docs parsing errors:")

		for _, err := range errs {
			printError("  %s", err.Error())
		}

		os.Exit(1)
	}

	if !doc.IsValid() {
		printWarn("File %s doesn't contains documentation", file)
		os.Exit(2)
	}

	if arg.GetS(ARG_NAME) != "" {
		doc.Title = arg.GetS(ARG_NAME)
	}

	if arg.GetS(ARG_OUTPUT) == "" {
		if pattern == "" {
			simpleRender(doc)
		} else {
			findInfo(doc, pattern)
		}
	} else {
		renderTemplate(doc)
	}
}
Пример #6
0
// getTempName return name of temporary file
func getTempName(dir, name string) string {
	var result string

	for {
		if name != "" {
			result = path.Join(dir, "_"+rand.String(12)+"_"+name)
		} else {
			result = path.Join(dir, "_tmp_"+rand.String(12))
		}

		if !fsutil.IsExist(result) {
			break
		}
	}

	return result
}
Пример #7
0
func findInfo(dir string, userMap map[int]string) ([]*ProcessInfo, error) {
	var result []*ProcessInfo

	dirs := fsutil.List(dir, true, &fsutil.ListingFilter{Perms: "DRX"})

	for _, pid := range dirs {
		if !isPID(pid) {
			continue
		}

		taskDir := dir + "/" + pid + "/task"

		if fsutil.IsExist(taskDir) {
			threads, err := findInfo(taskDir, userMap)

			if err != nil {
				return nil, err
			}

			if len(threads) == 0 {
				continue
			}

			result = append(result, threads...)

			continue
		}

		info, err := readProcessInfo(dir+"/"+pid, pid, userMap)

		if err != nil {
			return nil, err
		}

		if info == nil {
			continue
		}

		result = append(result, info)
	}

	return result, nil
}
Пример #8
0
// Read reads and parse config file
func Read(file string) (*Config, error) {
	switch {
	case fsutil.IsExist(file) == false:
		return nil, errors.New("File " + file + " is not exist")
	case fsutil.IsNonEmpty(file) == false:
		return nil, errors.New("File " + file + " is empty")
	case fsutil.IsReadable(file) == false:
		return nil, errors.New("File " + file + " is not readable")
	}

	config := &Config{
		data:     make(map[string]string),
		sections: make([]string, 0),
		props:    make([]string, 0),
		file:     file,
	}

	fd, err := os.Open(path.Clean(file))

	if err != nil {
		return nil, err
	}

	defer fd.Close()

	var sectionName = ""

	reader := bufio.NewReader(fd)
	scanner := bufio.NewScanner(reader)

	for scanner.Scan() {
		line := scanner.Text()

		if line == "" || strings.Replace(line, " ", "", -1) == "" {
			continue
		}

		if strings.HasPrefix(strings.Trim(line, " "), _COMMENT_SYMBOL) {
			continue
		}

		if strings.HasPrefix(strings.Trim(line, " "), _SECTION_SYMBOL) {
			sectionName = strings.Trim(line, "[ ]")
			config.data[sectionName+_DELIMITER] = "true"
			config.sections = append(config.sections, sectionName)
			continue
		}

		if sectionName == "" {
			return nil, errors.New("Configuration file " + file + " is malformed")
		}

		propName, propValue := parseRecord(line, config)
		fullPropName := sectionName + _DELIMITER + propName

		config.props = append(config.props, fullPropName)
		config.data[fullPropName] = propValue
	}

	if err := scanner.Err(); err != nil {
		return nil, err
	}

	return config, nil
}