コード例 #1
0
ファイル: watcher.go プロジェクト: ernestoalejo/cb
// Dirs add a new set of directories &files to the watched ones under
// the key name identification.
func Dirs(dirs []string, key string) error {
	walkersMutex.Lock()
	defer walkersMutex.Unlock()

	for _, dir := range dirs {
		w := utils.NewWalker(dir)
		walkers[key] = append(walkers[key], w)
		if *config.Verbose {
			log.Printf("watching `%s`\n", dir)
		}
	}

	// First check to store the initial times
	if _, err := CheckModified(key); err != nil {
		return fmt.Errorf("check cache failed: %s", err)
	}

	return nil
}
コード例 #2
0
ファイル: ngtemplates.go プロジェクト: ernestoalejo/cb
func readTemplates(paths []string) (map[string]string, error) {
	rootPath := "temp"
	templates := map[string]string{}

	walkFn := func(path string, info os.FileInfo) error {
		if info.IsDir() {
			return nil
		}

		// Rel path and ignore already cached templates
		rel, err := filepath.Rel(rootPath, path)
		if err != nil {
			return fmt.Errorf("cannot rel path: %s", err)
		}
		if templates[rel] != "" {
			return nil
		}

		// Read template contents
		contents, err := ioutil.ReadFile(path)
		if err != nil {
			return fmt.Errorf("read file failed: %s", err)
		}

		if *config.Verbose {
			log.Printf("registering template `%s`\n", rel)
		}
		templates[rel] = string(contents)

		return nil
	}
	for _, path := range paths {
		path = filepath.Join(rootPath, path)
		if err := utils.NewWalker(path).Walk(walkFn); err != nil {
			return nil, fmt.Errorf("walk path `%s` failed: %s", path, err)
		}
	}

	return templates, nil
}
コード例 #3
0
ファイル: deploy.go プロジェクト: ernestoalejo/cb
func organizeResult(c *config.Config) error {
	excludes := c.GetListDefault("deploy.exclude")
	includes := c.GetListDefault("deploy.include")
	moves := c.GetListDefault("deploy.moves")

	// Extract list of paths to remove
	removePaths := map[string]bool{}
	walkFn := func(path string, info os.FileInfo) error {
		removePaths[path] = true
		if *config.Verbose {
			log.Printf("flag to remove `%s`...\n", path)
		}
		return nil
	}
	for _, exclude := range excludes {
		exclude = filepath.Join("..", "deploy", exclude)
		if err := utils.NewWalker(exclude).Walk(walkFn); err != nil {
			fmt.Errorf("deploy exclude walker failed: %s", err)
		}
	}

	// Cancel removing of files that are included again
	walkFn = func(path string, info os.FileInfo) error {
		cur := path
		for cur != "." {
			removePaths[cur] = false
			cur = filepath.Dir(cur)
		}
		if *config.Verbose {
			log.Printf("include `%s`...\n", path)
		}
		return nil
	}
	for _, include := range includes {
		include = filepath.Join("..", "deploy", include)
		if err := utils.NewWalker(include).Walk(walkFn); err != nil {
			fmt.Errorf("deploy include walker failed: %s", err)
		}
	}

	// Remove flagged files & folders
	for path, remove := range removePaths {
		if remove {
			if *config.Verbose {
				log.Printf("removing `%s`...\n", path)
			}
			if err := os.RemoveAll(path); err != nil {
				return fmt.Errorf("cannot remove deploy entry: %s", err)
			}
		}
	}

	// Execute move operations
	for _, move := range moves {
		parts := strings.Split(move, "->")
		origin := filepath.Join("..", "deploy", strings.TrimSpace(parts[0]))
		dest := filepath.Join("..", "deploy", strings.TrimSpace(parts[1]))

		if err := os.MkdirAll(filepath.Dir(dest), 0755); err != nil {
			return fmt.Errorf("cannot create dest tree structure: %s", err)
		}

		output, err := utils.Exec("cp", []string{"-r", origin, dest})
		if err != nil {
			fmt.Println(output)
			return fmt.Errorf("copy error: %s", err)
		}
	}

	return nil
}