<commit_before>package dockerpush

import (
	"fmt"
	"github.com/mitchellh/packer/common"
	"github.com/mitchellh/packer/packer"
	"os/exec"
)

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

	Registry string `mapstructure:"registry"`
	Username string `mapstructure:"username"`
	Password string `mapstructure:"password"`
	Email    string `mapstructure:"email"`

	tpl *packer.ConfigTemplate
}

type PostProcessor struct {
	config Config
}

func (p *PostProcessor) Configure(raws ...interface{}) error {
	_, err := common.DecodeConfig(&p.config, raws...)
	if err != nil {
		return err
	}

	p.config.tpl, err = packer.NewConfigTemplate()
	if err != nil {
		return err
	}
	p.config.tpl.UserVars = p.config.PackerUserVars

	// Accumulate any errors
	errs := new(packer.MultiError)

	templates := map[string]*string{
		"username": &p.config.Username,
		"password": &<PASSWORD>,
	}

	for key, ptr := range templates {
		if *ptr == "" {
			errs = packer.MultiErrorAppend(
				errs, fmt.Errorf("%s must be set", key))
		}

		*ptr, err = p.config.tpl.Process(*ptr, nil)
		if err != nil {
			errs = packer.MultiErrorAppend(
				errs, fmt.Errorf("Error processing %s: %s", key, err))
		}
	}

	if len(errs.Errors) > 0 {
		return errs
	}

	return nil

}

func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
	id := artifact.Id()
	ui.Say("Pushing image: " + id)

	if p.config.Registry == "" {

		if p.config.Email == "" {
			cmd := exec.Command("docker",
				"login",
				"-u="+p.config.Username,
				"-p="+p.config.Password)

			if err := cmd.Run(); err != nil {
				ui.Say("Login to the registry " + p.config.Registry + " failed")
				return nil, false, err
			}

		} else {
			cmd := exec.Command("docker",
				"login",
				"-u="+p.config.Username,
				"-p="+p.config.Password,
				"-e="+p.config.Email)

			if err := cmd.Run(); err != nil {
				ui.Say("Login to the registry " + p.config.Registry + " failed")
				return nil, false, err
			}

		}

	} else {
		if p.config.Email == "" {
			cmd := exec.Command("docker",
				"login",
				"-u="+p.config.Username,
				"-p="+p.config.Password,
				p.config.Registry)

			if err := cmd.Run(); err != nil {
				ui.Say("Login to the registry " + p.config.Registry + " failed")
				return nil, false, err
			}

		} else {
			cmd := exec.Command("docker",
				"login",
				"-u="+p.config.Username,
				"-p="+p.config.Password,
				"-e="+p.config.Email,
				p.config.Registry)

			if err := cmd.Run(); err != nil {
				ui.Say("Login to the registry " + p.config.Registry + " failed")
				return nil, false, err
			}

		}
	}

	cmd := exec.Command("docker", "push", id)
	if err := cmd.Run(); err != nil {
		ui.Say("Failed to push image: " + id)
		return nil, false, err
	}

	return nil, false, nil
}
<commit_msg>docker-push: add code to handle seperate registry, push a specific repository/tag #774<commit_after>package dockerpush

import (
	"fmt"
	"github.com/mitchellh/packer/common"
	"github.com/mitchellh/packer/packer"
	"os/exec"
)

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

	Repository string `mapstructure:"repository"`
	Tag        string `mapstructure:"tag"`
	Registry   string `mapstructure:"registry"`
	Username   string `mapstructure:"username"`
	Password   string `mapstructure:"password"`
	Email      string `mapstructure:"email"`

	tpl *packer.ConfigTemplate
}

type PostProcessor struct {
	config Config
}

func (p *PostProcessor) Configure(raws ...interface{}) error {
	_, err := common.DecodeConfig(&p.config, raws...)
	if err != nil {
		return err
	}

	p.config.tpl, err = packer.NewConfigTemplate()
	if err != nil {
		return err
	}
	p.config.tpl.UserVars = p.config.PackerUserVars

	// Accumulate any errors
	errs := new(packer.MultiError)

	templates := map[string]*string{
		"username":   &p.config.Username,
		"password":   &<PASSWORD>,
		"repository": &p.config.Repository,
	}

	for key, ptr := range templates {
		if *ptr == "" {
			errs = packer.MultiErrorAppend(
				errs, fmt.Errorf("%s must be set", key))
		}

		*ptr, err = p.config.tpl.Process(*ptr, nil)
		if err != nil {
			errs = packer.MultiErrorAppend(
				errs, fmt.Errorf("Error processing %s: %s", key, err))
		}
	}

	if len(errs.Errors) > 0 {
		return errs
	}

	return nil

}

func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
	id := artifact.Id()
	ui.Say("Pushing image: " + id)

	if p.config.Registry == "" {

		if p.config.Email == "" {
			cmd := exec.Command("docker",
				"login",
				"-u="+p.config.Username,
				"-p="+p.config.Password)

			if err := cmd.Run(); err != nil {
				ui.Say("Login to the registry " + p.config.Registry + " failed")
				return nil, false, err
			}

		} else {
			cmd := exec.Command("docker",
				"login",
				"-u="+p.config.Username,
				"-p="+p.config.Password,
				"-e="+p.config.Email)

			if err := cmd.Run(); err != nil {
				ui.Say("Login to the registry " + p.config.Registry + " failed")
				return nil, false, err
			}

		}

	} else {
		if p.config.Email == "" {
			cmd := exec.Command("docker",
				"login",
				"-u="+p.config.Username,
				"-p="+p.config.Password,
				p.config.Registry)

			if err := cmd.Run(); err != nil {
				ui.Say("Login to the registry " + p.config.Registry + " failed")
				return nil, false, err
			}

		} else {
			cmd := exec.Command("docker",
				"login",
				"-u="+p.config.Username,
				"-p="+p.config.Password,
				"-e="+p.config.Email,
				p.config.Registry)

			if err := cmd.Run(); err != nil {
				ui.Say("Login to the registry " + p.config.Registry + " failed")
				return nil, false, err
			}

		}
	}

	if p.config.Tag != "" {

		cmd := exec.Command("docker", "push", p.config.Repository+":"+p.config.Tag)
		if err := cmd.Run(); err != nil {
			ui.Say("Failed to push image: " + id)
			return nil, false, err
		}

	} else {

		cmd := exec.Command("docker", "push", p.config.Repository)
		if err := cmd.Run(); err != nil {
			ui.Say("Failed to push image: " + id)
			return nil, false, err
		}

	}

	return nil, false, nil
}

{'fraction_non_alphanumeric': 0.1744205161926681, 'fraction_numerical': 0.000821962847279303, 'mean_word_length': 3.039840637450199, 'pattern_counts': {'":': 6, '<': 5, '<?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': '2880285', 'n_tokens_mistral': 2498, 'n_tokens_neox': 2162, 'n_words': 508}