示例#1
0
文件: config.go 项目: uwork/gorond
// 設定を読み込んで返す。
func LoadConfig(configPath string, includeDir string) (*Config, error) {
	root, err := newGoronConfig(configPath, &Config{})
	if err != nil {
		return nil, err
	}

	confFiles, err := util.FileList(includeDir, `.+\.conf$`)
	if err != nil {
		return nil, err
	}

	for _, confPath := range confFiles {
		config, err := newGoronConfig(includeDir+"/"+confPath, root)
		if err != nil {
			return nil, err
		}

		root.Childs = append(root.Childs, config)
	}

	return root, nil
}
示例#2
0
文件: fswatch.go 项目: uwork/gorond
func (self *Watcher) watchFileSystem() (Event, error) {
	event := Event{}

	watchFiles := []string{}
	for path, pattern := range self.paths {
		stat, err := os.Stat(path)
		if err != nil {
			return event, err
		}

		if stat.Mode().IsDir() {
			files, err := util.FileList(path, pattern)
			if err != nil {
				return event, err
			}

			for _, file := range files {
				filePath := filepath.Join(path, file)
				watchFiles = append(watchFiles, filePath)
			}
		} else if stat.Mode().IsRegular() {
			watchFiles = append(watchFiles, path)
		}
	}

	// 変更点を調査
	modifiedFiles := []string{}
	addedFiles := []string{}
	removedFiles := []string{}
	timestamps := map[string]int64{}
	for _, file := range watchFiles {
		stat, err := os.Stat(file)
		if err != nil {
			return event, err
		}

		// タイムスタンプを過去と比較
		ts, exists := self.context[file]
		if !exists {
			// 前回のチェック時に存在しなかった場合は追加として扱う
			addedFiles = append(addedFiles, file)
		} else if ts != stat.ModTime().Unix() {
			// タイムスタンプが異なる場合は変更として扱う
			modifiedFiles = append(modifiedFiles, file)
		}

		timestamps[file] = stat.ModTime().Unix()
	}

	// 削除されたファイルを検知する
	for file, _ := range self.context {
		_, exists := timestamps[file]
		if !exists {
			removedFiles = append(removedFiles, file)
		}
	}

	// コンテキストを更新する
	self.context = timestamps

	// イベントに設定する
	event.Added = addedFiles
	event.Modified = modifiedFiles
	event.Removed = removedFiles

	return event, nil
}