<commit_before>package main

import (
	"bytes"
	"errors"
	"fmt"
	"log"
	"os"
	"regexp"

	"github.com/hashicorp/packer/common"
	"github.com/hashicorp/packer/helper/config"
	"github.com/hashicorp/packer/packer"
	"github.com/hashicorp/packer/packer/plugin"
	"github.com/hashicorp/packer/template/interpolate"
)

type Config struct {
	common.PackerConfig `mapstructure:",squash"`

	MitamaeVersion string `mapstructure:"mitamae_version"`

	BinDir string `mapstructure:"bin_dir"`

	Option string

	RecipePath string `mapstructure:"recipe_path"`

	ctx interpolate.Context
}

type MitamaeProvisioner struct {
	config Config
}

func (mp *MitamaeProvisioner) Prepare(raws ...interface{}) error {
	err := config.Decode(&mp.config, &config.DecodeOpts{
		Interpolate:        true,
		InterpolateContext: &mp.config.ctx,
		InterpolateFilter: &interpolate.RenderFilter{
			Exclude: []string{},
		},
	}, raws...)
	if err != nil {
		return err
	}

	if mp.config.RecipePath == "" {
		return errors.New("recipe_path is required")
	}

	if mp.config.BinDir == "" {
		mp.config.BinDir = "/usr/local/bin"
	}

	if mp.config.MitamaeVersion == "" {
		mp.config.MitamaeVersion = "v1.4.5"
	}

	return nil
}

func (mp *MitamaeProvisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
	ui.Say("MItamae provisioning start")

	filename, err := mp.getMItamaeFileName(comm)
	if err != nil {
		return err
	}

	err = mp.downloadMItamae(ui, comm, filename)
	if err != nil {
		return err
	}

	err = mp.execRecipe(ui, comm, filename)
	if err != nil {
		return err
	}

	ui.Say("MItamae provisioning end")

	return nil
}

func (mp *MitamaeProvisioner) getMItamaeFileName(comm packer.Communicator) (string, error) {
	var cmd packer.RemoteCmd
	cmd.Command = "uname -s -m | awk '{printf(\"%s-%s\",$2,tolower($1))}'"

	var stdout, stderr bytes.Buffer
	cmd.Stdout = &stdout
	cmd.Stderr = &stderr

	if err := comm.Start(&cmd); err != nil {
		return "", err
	}
	cmd.Wait()
	if stderr.String() != "" {
		return "", errors.New(stderr.String())
	}
	os_arch := stdout.String()

	os := regexp.MustCompile(`^[^-]+-`).ReplaceAllString(os_arch, "")
	switch os {
	case "linux", "darwin":
		return fmt.Sprintf("mitamae-%s", os_arch), nil
	default:
		return "", fmt.Errorf("%s is not support.", os_arch)
	}
}

func (mp *MitamaeProvisioner) downloadMItamae(ui packer.Ui, comm packer.Communicator, filename string) error {
	download_url := fmt.Sprintf("https://github.com/itamae-kitchen/mitamae/releases/download/%s/%s", mp.config.MitamaeVersion, filename)
	bin_path := fmt.Sprintf("%s/%s", mp.config.BinDir, filename)

	log.Printf("Start MItamae Download from %s to %s", download_url, bin_path)

	var cmd packer.RemoteCmd
	cmd.Command = fmt.Sprintf("wget %s -q -P %s -O %s && chmod +x %s", download_url, mp.config.BinDir, filename, bin_path)
	var stderr bytes.Buffer
	cmd.Stderr = &stderr
	if err := comm.Start(&cmd); err != nil {
		return err
	}
	cmd.Wait()
	if stderr.String() != "" {
		return errors.New(stderr.String())
	}

	log.Printf("Success MItamae Download")

	return nil
}

func (mp *MitamaeProvisioner) execRecipe(ui packer.Ui, comm packer.Communicator, filename string) error {
	bin_path := fmt.Sprintf("%s/%s", mp.config.BinDir, filename)

	var cmd packer.RemoteCmd
	cmd.Command = fmt.Sprintf("%s local %s %s", bin_path, mp.config.Option, mp.config.RecipePath)
	if err := cmd.StartWithUi(comm, ui); err != nil {
		return err
	}

	if cmd.ExitStatus != 0 {
		return fmt.Errorf("MItamae execution failed")
	}

	return nil
}

func (mp *MitamaeProvisioner) Cancel() {
	os.Exit(0)
}<commit_msg>variable name change snake_case to camelCase<commit_after>package main

import (
	"bytes"
	"errors"
	"fmt"
	"log"
	"os"
	"regexp"

	"github.com/hashicorp/packer/common"
	"github.com/hashicorp/packer/helper/config"
	"github.com/hashicorp/packer/packer"
	"github.com/hashicorp/packer/packer/plugin"
	"github.com/hashicorp/packer/template/interpolate"
)

type Config struct {
	common.PackerConfig `mapstructure:",squash"`

	MitamaeVersion string `mapstructure:"mitamae_version"`

	BinDir string `mapstructure:"bin_dir"`

	Option string

	RecipePath string `mapstructure:"recipe_path"`

	ctx interpolate.Context
}

type MitamaeProvisioner struct {
	config Config
}

func (mp *MitamaeProvisioner) Prepare(raws ...interface{}) error {
	err := config.Decode(&mp.config, &config.DecodeOpts{
		Interpolate:        true,
		InterpolateContext: &mp.config.ctx,
		InterpolateFilter: &interpolate.RenderFilter{
			Exclude: []string{},
		},
	}, raws...)
	if err != nil {
		return err
	}

	if mp.config.RecipePath == "" {
		return errors.New("recipe_path is required")
	}

	if mp.config.BinDir == "" {
		mp.config.BinDir = "/usr/local/bin"
	}

	if mp.config.MitamaeVersion == "" {
		mp.config.MitamaeVersion = "v1.4.5"
	}

	return nil
}

func (mp *MitamaeProvisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
	ui.Say("MItamae provisioning start")

	filename, err := mp.getMItamaeFileName(comm)
	if err != nil {
		return err
	}

	err = mp.downloadMItamae(ui, comm, filename)
	if err != nil {
		return err
	}

	err = mp.execRecipe(ui, comm, filename)
	if err != nil {
		return err
	}

	ui.Say("MItamae provisioning end")

	return nil
}

func (mp *MitamaeProvisioner) getMItamaeFileName(comm packer.Communicator) (string, error) {
	var cmd packer.RemoteCmd
	cmd.Command = "uname -s -m | awk '{printf(\"%s-%s\",$2,tolower($1))}'"

	var stdout, stderr bytes.Buffer
	cmd.Stdout = &stdout
	cmd.Stderr = &stderr

	if err := comm.Start(&cmd); err != nil {
		return "", err
	}
	cmd.Wait()
	if stderr.String() != "" {
		return "", errors.New(stderr.String())
	}
	osArch := stdout.String()

	os := regexp.MustCompile(`^[^-]+-`).ReplaceAllString(osArch, "")
	switch os {
	case "linux", "darwin":
		return fmt.Sprintf("mitamae-%s", osArch), nil
	default:
		return "", fmt.Errorf("%s is not support.", osArch)
	}
}

func (mp *MitamaeProvisioner) downloadMItamae(ui packer.Ui, comm packer.Communicator, filename string) error {
	downloadUrl := fmt.Sprintf("https://github.com/itamae-kitchen/mitamae/releases/download/%s/%s", mp.config.MitamaeVersion, filename)
	binPath := fmt.Sprintf("%s/%s", mp.config.BinDir, filename)

	log.Printf("Start MItamae Download from %s to %s", downloadUrl, binPath)

	var cmd packer.RemoteCmd
	cmd.Command = fmt.Sprintf("wget %s -q -P %s -O %s && chmod +x %s", downloadUrl, mp.config.BinDir, filename, binPath)
	var stderr bytes.Buffer
	cmd.Stderr = &stderr
	if err := comm.Start(&cmd); err != nil {
		return err
	}
	cmd.Wait()
	if stderr.String() != "" {
		return errors.New(stderr.String())
	}

	log.Printf("Success MItamae Download")

	return nil
}

func (mp *MitamaeProvisioner) execRecipe(ui packer.Ui, comm packer.Communicator, filename string) error {
	binPath := fmt.Sprintf("%s/%s", mp.config.BinDir, filename)

	var cmd packer.RemoteCmd
	cmd.Command = fmt.Sprintf("%s local %s %s", binPath, mp.config.Option, mp.config.RecipePath)
	if err := cmd.StartWithUi(comm, ui); err != nil {
		return err
	}

	if cmd.ExitStatus != 0 {
		return fmt.Errorf("MItamae execution failed")
	}

	return nil
}

func (mp *MitamaeProvisioner) Cancel() {
	os.Exit(0)
}
{'fraction_non_alphanumeric': 0.15466666666666667, 'fraction_numerical': 0.0019649122807017545, 'mean_word_length': 4.224340175953079, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1269417', 'n_tokens_mistral': 2786, 'n_tokens_neox': 2655, 'n_words': 581}