예제 #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
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)
	}
}
예제 #3
0
파일: knf.go 프로젝트: essentialkaos/ek
// 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
}