<commit_before>package main

import (
	"fmt"
	"log"

	"github.com/mitchellh/packer/builder/digitalocean"
	"github.com/mitchellh/packer/packer"
)

type DigitalOcean struct {
	ClientID string
	ApiKey   string
}

func (d *DigitalOcean) Build(path string) error {
	log.Printf("Digitalocean: Reading template: %s", path)
	template, err := packer.ParseTemplateFile(path, nil)
	if err != nil {
		return fmt.Errorf("Failed to parse template: %s", err)
	}

	fmt.Printf("template %#v\n", template)

	if len(template.BuildNames()) != 1 {
		return fmt.Errorf("Failed to find build in the template: %v", template.BuildNames())
	}

	buildName := template.BuildNames()[0]
	if buildName != "digitalocean" {
		return fmt.Errorf("Build name is different than 'digitalocean': %v", buildName)
	}

	builder := digitalocean.Builder{}
	fmt.Printf("builder %+v\n", builder)

	s, err := builder.Prepare(template.Builders["digitalocean"].RawConfig)
	if err != nil {
		return err
	}

	fmt.Printf("s %+v\n", s)

	// log.Println("Digitalocean: Preparing environment")
	// envConfig := packer.DefaultEnvironmentConfig()
	// env, err := packer.NewEnvironment(envConfig)
	// if err != nil {
	// 	return err
	// }
	//
	// components := &packer.ComponentFinder{
	// 	Builder:       env.Builder,
	// 	Hook:          env.Hook,
	// 	PostProcessor: env.PostProcessor,
	// 	Provisioner:   env.Provisioner,
	// }

	// log.Println("Digitalocean: Creating build interface")
	// build, err := template.Build(buildName, components)
	// if err != nil {
	// 	return err
	// }

	return nil
}

func (d *DigitalOcean) Provision() error {
	return nil
}

func (d *DigitalOcean) Start() error   { return nil }
func (d *DigitalOcean) Stop() error    { return nil }
func (d *DigitalOcean) Restart() error { return nil }
func (d *DigitalOcean) Destroy() error { return nil }<commit_msg>kloud: still reverse enginnering packer<commit_after>package main

import (
	"errors"
	"fmt"
	"log"

	"github.com/mitchellh/packer/builder/digitalocean"
	"github.com/mitchellh/packer/packer"
	"github.com/mitchellh/packer/provisioner/file"
	"github.com/mitchellh/packer/provisioner/shell"
)

type DigitalOcean struct {
	ClientID string
	ApiKey   string
}

func (d *DigitalOcean) Build(path string) error {
	log.Printf("Digitalocean: Reading template: %s", path)

	userVars := map[string]string{
		"klient_deb":     "klient_0.0.1_amd64.deb",
		"klient_keyname": "kite.key",
		"klient_keydir":  "/opt/kite/klient/key",
	}

	template, err := packer.ParseTemplateFile(path, userVars)
	if err != nil {
		return fmt.Errorf("Failed to parse template: %s", err)
	}

	if len(template.Builders) != 1 {
		return fmt.Errorf("Failed to find build in the template: %v", template.BuildNames())
	}

	if _, ok := template.Builders["digitalocean"]; !ok {
		return errors.New("Build 'digitalocean' does not exist")
	}

	components := &packer.ComponentFinder{
		Builder:     builderFunc,
		Provisioner: provisionerFunc,
	}

	build, err := template.Build("digitalocean", components)
	if err != nil {
		return err
	}

	_, err = build.Prepare()
	if err != nil {
		return err
	}

	return nil
}

func builderFunc(name string) (packer.Builder, error) {
	switch name {
	case "digitalocean":
		return &digitalocean.Builder{}, nil
	}

	return nil, errors.New("no suitable build found")
}

func provisionerFunc(name string) (packer.Provisioner, error) {
	switch name {
	case "file":
		return &file.Provisioner{}, nil
	case "shell":
		return &shell.Provisioner{}, nil
	}

	return nil, errors.New("no suitable provisioner found")
}

func (d *DigitalOcean) Provision() error {
	return nil
}

func (d *DigitalOcean) Start() error   { return nil }
func (d *DigitalOcean) Stop() error    { return nil }
func (d *DigitalOcean) Restart() error { return nil }
func (d *DigitalOcean) Destroy() error { return nil }

func deleteLater() {
	// _, err = build.Run()
	// if err != nil {
	// 	return err
	// }

	// log.Println("Digitalocean: Creating build interface")
	// build, err := template.Build(buildName, components)
	// if err != nil {
	// 	return err
	// }

	// for _, b := range template.Builders {
	// 	fmt.Printf("b.Type %+v\n", b.Type)
	// }
	//
	// variables := make(map[string]string)
	// for k, v := range template.Variables {
	// 	fmt.Printf("v %+v\n", v)
	// 	variables[k] = v.Value
	// }
	//
	// if len(template.BuildNames()) != 1 {
	// 	return fmt.Errorf("Failed to find build in the template: %v", template.BuildNames())
	// }
	//
	// buildName := template.BuildNames()[0]
	// if buildName != "digitalocean" {
	// 	return fmt.Errorf("Build name is different than 'digitalocean': %v", buildName)
	// }
	//
	// builder := digitalocean.Builder{}
	//
	// packerConfig := map[string]interface{}{
	// 	packer.BuildNameConfigKey:     b.name,
	// 	packer.BuilderTypeConfigKey:   template.Builders["digitalocan"].Type
	// 	packer.UserVariablesConfigKey: variables,
	// }
	//
	// // Prepare the builder
	// _, err = builder.Prepare(template.Builders["digitalocean"].RawConfig, packerConfig)
	// if err != nil {
	// 	return err
	// }
	//
	// for _, p := range template.Provisioners {
	// 	fmt.Printf("p.Type %+v\n", p.Type)
	//
	// 	switch p.Type {
	// 	case "file":
	// 		f := file.Provisioner{}
	// 		if err := f.Prepare(p.RawConfig); err != nil {
	// 			return err
	// 		}
	// 	case "shell":
	// 		s := shell.Provisioner{}
	// 		if err := s.Prepare(p.RawConfig); err != nil {
	// 			return err
	// 		}
	// 	}
	//
	// }
}
{'fraction_non_alphanumeric': 0.16279069767441862, 'fraction_numerical': 0.0018456995201181247, 'mean_word_length': 3.3491171749598716, 'pattern_counts': {'":': 8, '<': 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': '4811441', 'n_tokens_mistral': 2036, 'n_tokens_neox': 1959, 'n_words': 455}