<commit_before>package main

import (
	"flag"
	"fmt"
	"log"
	"os"
	"path"
	"path/filepath"
	"strings"

	"github.com/mlafeldt/chef-runner.go/berkshelf"
	"github.com/mlafeldt/chef-runner.go/exec"
	"github.com/mlafeldt/chef-runner.go/metadata"
	"github.com/mlafeldt/chef-runner.go/rsync"
	"github.com/mlafeldt/chef-runner.go/util"
	"github.com/mlafeldt/chef-runner.go/vagrant"
)

const (
	CookbookPath    = "vendor/cookbooks"
	VagrantChefPath = "/tmp/vagrant-chef-1"
)

// Install cookbook dependencies with Berkshelf. If the cookbooks are already
// in place, use lightning-fast rsync to update the current cookbook only.
func installCookbooks(cookbookName, installDir string) error {
	if util.FileExist(installDir) {
		// TODO: filter files more diligently
		files, err := filepath.Glob("[a-zA-Z]*")
		if err != nil {
			return err
		}
		dst := path.Join(installDir, cookbookName)
		opts := rsync.Options{
			Archive: true,
			Delete:  true,
			Exclude: []string{installDir},
		}
		return rsync.Copy(files, dst, opts)

	}
	return berkshelf.Install(installDir)
}

func openSSH(host, command string) error {
	return exec.RunCommand([]string{"ssh", host, "-c", command})
}

func provision(host, machine, format, logLevel, jsonFile string, runlist string) error {
	config_file := VagrantChefPath + "/solo.rb"
	json_file := VagrantChefPath + "/dna.json"
	cookbooks_path := "/vagrant/" + CookbookPath

	setup_dir := fmt.Sprintf("sudo mkdir -p %s", VagrantChefPath)
	setup_config := fmt.Sprintf("test -f %s || echo 'cookbook_path \"%s\"' | sudo tee %s >/dev/null", config_file, cookbooks_path, config_file)
	setup_json := fmt.Sprintf("test -f %s || echo '{}' | sudo tee %s >/dev/null", json_file, json_file)

	if jsonFile != "" {
		json_file = "/vagrant/" + jsonFile<commit_msg>Filter cookbook files more diligently<commit_after>package main

import (
	"flag"
	"fmt"
	"log"
	"os"
	"path"
	"path/filepath"
	"strings"

	"github.com/mlafeldt/chef-runner.go/berkshelf"
	"github.com/mlafeldt/chef-runner.go/exec"
	"github.com/mlafeldt/chef-runner.go/metadata"
	"github.com/mlafeldt/chef-runner.go/rsync"
	"github.com/mlafeldt/chef-runner.go/util"
	"github.com/mlafeldt/chef-runner.go/vagrant"
)

const (
	CookbookPath    = "vendor/cookbooks"
	VagrantChefPath = "/tmp/vagrant-chef-1"
)

func cookbookFiles() ([]string, error) {
	filesGlob := []string{
		"README.*",
		"metadata.*",
		"attributes",
		"definitions",
		"files",
		"libraries",
		"providers",
		"recipes",
		"resources",
		"templates",
	}

	var files []string
	for _, glob := range filesGlob {
		matches, err := filepath.Glob(glob)
		if err != nil {
			return nil, err
		}
		files = append(files, matches...)
	}

	return files, nil
}

func installCookbooks(cookbookName, installDir string) error {
	if !util.FileExist(installDir) {
		return berkshelf.Install(installDir)
	}

	files, err := cookbookFiles()
	if err != nil {
		return err
	}
	opts := rsync.Options{
		Archive: true,
		Delete:  true,
		Verbose: true,
	}
	return rsync.Copy(files, path.Join(installDir, cookbookName), opts)
}

func openSSH(host, command string) error {
	return exec.RunCommand([]string{"ssh", host, "-c", command})
}

func provision(host, machine, format, logLevel, jsonFile string, runlist string) error {
	config_file := VagrantChefPath + "/solo.rb"
	json_file := VagrantChefPath + "/dna.json"
	cookbooks_path := "/vagrant/" + CookbookPath

	setup_dir := fmt.Sprintf("sudo mkdir -p %s", VagrantChefPath)
	setup_config := fmt.Sprintf("test -f %s || echo 'cookbook_path \"%s\"' | sudo tee %s >/dev/null", config_file, cookbooks_path, config_file)
	setup_json := fmt.Sprintf("test -f %s || echo '{}' | sudo tee %s >/dev/null", json_file, json_file)

	if jsonFile != "" {
		json_file = "/vagrant/" + jsonFile
{'fraction_non_alphanumeric': 0.15613283342260312, 'fraction_numerical': 0.0005356186395286556, 'mean_word_length': 4.223776223776224, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2190400', 'n_tokens_mistral': 1502, 'n_tokens_neox': 1427, 'n_words': 301}