<commit_before>package main

import (
	"encoding/json"
	"errors"
	"fmt"
	"reflect"

	"github.com/koding/kite"
)

// Builder is used to create and provisiong a single image or machine for a
// given Provider.
type Builder interface {
	Prepare(...interface{}) error
	Build() error
}

// Controller manages a machine
type Controller interface {
	Start() error
	Stop() error
	Restart() error
	Destroy() error
}

type buildArgs struct {
	Provider   string
	Credential map[string]interface{}
	Builder    map[string]interface{}
}

var providers = map[string]Builder{
	"digitalocean": &DigitalOcean{},
}

func build(r *kite.Request) (interface{}, error) {
	args := &buildArgs{}
	if err := r.Args.One().Unmarshal(args); err != nil {
		return nil, err
	}

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

	provider, ok := providers[args.Provider]
	if !ok {
		return nil, errors.New("provider not supported")
	}

	if err := provider.Prepare(args.Credential, args.Builder); err != nil {
		return nil, err
	}

	if err := provider.Build(); err != nil {
		return nil, err
	}

	return true, nil
}

// templateData includes our klient converts the given raw interface to a
// []byte data that can used to pass into packer.Template().
func templateData(raw interface{}) ([]byte, error) {
	rawMapData, err := toMap(raw, "mapstructure")
	if err != nil {
		return nil, err
	}
	fmt.Printf("rawMapData %+v\n", rawMapData)

	packerTemplate := map[string]interface{}{}
	packerTemplate["builders"] = []interface{}{rawMapData}
	packerTemplate["provisioners"] = klientProvisioner
	fmt.Printf("packerTemplate %+v\n", packerTemplate)

	return json.Marshal(packerTemplate)
}

// toMap converts a struct defined by `in` to a map[string]interface{}. It only<commit_msg>kloud: change the controller interface until we found a middleway<commit_after>package main

import (
	"encoding/json"
	"errors"
	"fmt"
	"reflect"

	"github.com/koding/kite"
)

// Builder is used to create and provisiong a single image or machine for a
// given Provider.
type Builder interface {
	Prepare(...interface{}) error
	Build() error
}

// Controller manages a machine
type Controller interface {
	Start(...interface{}) error
	Stop(...interface{}) error
	Restart(...interface{}) error
	Destroy(...interface{}) error
}

type buildArgs struct {
	Provider   string
	Credential map[string]interface{}
	Builder    map[string]interface{}
}

var providers = map[string]interface{}{
	"digitalocean": &DigitalOcean{},
}

func build(r *kite.Request) (interface{}, error) {
	args := &buildArgs{}
	if err := r.Args.One().Unmarshal(args); err != nil {
		return nil, err
	}

	p, ok := providers[args.Provider]
	if !ok {
		return nil, errors.New("provider not supported")
	}

	provider, ok := p.(Builder)
	if !ok {
		return nil, errors.New("provider doesn't satisfy/support this method.")
	}

	if err := provider.Prepare(args.Credential, args.Builder); err != nil {
		return nil, err
	}

	if err := provider.Build(); err != nil {
		return nil, err
	}

	return true, nil
}

// templateData includes our klient converts the given raw interface to a
// []byte data that can used to pass into packer.Template().
func templateData(raw interface{}) ([]byte, error) {
	rawMapData, err := toMap(raw, "mapstructure")
	if err != nil {
		return nil, err
	}

	packerTemplate := map[string]interface{}{}
	packerTemplate["builders"] = []interface{}{rawMapData}
	packerTemplate["provisioners"] = klientProvisioner

	return json.Marshal(packerTemplate)
}

// toMap converts a struct defined by `in` to a map[string]interface{}. It only
{'fraction_non_alphanumeric': 0.13869232946504387, 'fraction_numerical': 0.0, 'mean_word_length': 4.322289156626506, 'pattern_counts': {'":': 2, '<': 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': '3206570', 'n_tokens_mistral': 1161, 'n_tokens_neox': 1121, 'n_words': 349}