<commit_before>package definition

import (
	"github.com/bronzdoc/symbiote/template"
	"gopkg.in/yaml.v2"
	"io/ioutil"
	"log"
	"os"
)

type definition struct {
	Context   string
	Resources []map[string]interface{}
	vars      map[string]string
}

func New(definition_file string, vars map[string]string) *definition {
	def := definition{}
	def.vars = vars
	data, err := ioutil.ReadFile(definition_file)

	if err != nil {
		log.Fatal(err)
	}

	if err = yaml.Unmarshal(data, &def); err != nil {
		log.Fatal(err)
	}

	return &def
}

func (s *definition) Create() {
	resources := s.Resources
	context := s.Context

	for i := range resources {
		for key, val := range resources[i] {
			if key == "dir" {
				data := val.(map[interface{}]interface{})
				dir_name := data["name"]
				os.Mkdir(context+"/"+dir_name.(string), 0777)
				if _, ok := data["files"]; ok {
					files := data["files"].([]interface{})
					for j := range files {
						filename := files[j].(string)
						file_path := context + "/" + dir_name.(string) + "/" + filename
						f := create_file(file_path)
						defer f.Close()
						template_path := os.Getenv("HOME") + "/" + ".definitions/templates" + "/" + filename
						_, err := template.New(filename, template_path, s.vars).Create(f)
						if err != nil {
							log.Fatal(err)
						}
					}
				}
			}
		}
	}
}

func create_file(filename string) *os.File {
	f, err := os.Create(filename)
	if err != nil {
		log.Fatal(err)
	}
	return f
}<commit_msg>Delegate resource creation to each resource node<commit_after>package definition

import (
	"io/ioutil"
	"log"

	"gopkg.in/yaml.v2"
)

type definition struct {
	Context      string
	Resources    []map[interface{}]interface{}
	ResourceTree *tree
	Options      map[string]interface{}
}

func New(definition_file string, options map[string]interface{}) *definition {
	def := definition{Options: options}

	definition_content, err := ioutil.ReadFile(definition_file)
	if err != nil {
		log.Fatal(err)
	}

	if err = yaml.Unmarshal(definition_content, &def); err != nil {
		log.Fatal(err)
	}

	def.ResourceTree = newTree(def.Context, def.Resources)

	return &def
}

func (d *definition) Create() {
	tree := d.ResourceTree
	tree.Traverse(func(node Resource) {
		node.Create(d.Options)
	})
}
{'fraction_non_alphanumeric': 0.15527950310559005, 'fraction_numerical': 0.0026619343389529724, 'mean_word_length': 3.4742063492063493, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5866448', 'n_tokens_mistral': 891, 'n_tokens_neox': 782, 'n_words': 174}