<commit_before>package CIScriptsDocker

import (
	"fmt"
	c "github.com/bcaldwell/ci-scripts/internal/CIScriptsHelpers"
	"strings"
)

type BuildAndPushImage struct{}

func (BuildAndPushImage) Run() error {
	folder, _ := c.ConfigFetch("docker.images.folder", ".")
	dockerRepo := c.RequiredConfigFetch("docker.images.dockerRepo")

	c.LogInfo("Build %s from folder %s", dockerRepo, folder)

	dockerUser, _ := c.ConfigFetch("docker.user")

	gitSha, _ := c.CaptureCommand("git", "rev-parse", "HEAD")
	dockerTag, _ := c.ConfigFetch("docker.tag", strings.TrimSpace(string(gitSha)))

	dockerBuildCommand := []string{"docker", "build", "-t", dockerRepo, folder}

	if args, ok := c.ConfigFetch("docker.image.build_args"); ok {
		dockerBuildCommand = append(dockerBuildCommand, strings.Split(args, " ")...)
	}

	err := c.Command(dockerBuildCommand...)
	if err != nil {
		return err
	}

	dockerRepoWithTag := fmt.Sprintf("%s:%s", dockerRepo, dockerTag)
	c.Command("sh", "-c", fmt.Sprintf("docker login -u %s -p $DOCKER_PASS", dockerUser))
	err = c.Command("docker", "tag", dockerRepo, dockerRepoWithTag)
	if err != nil {
		return err
	}
	err = c.Command("docker", "push", dockerRepo)
	if err != nil {
		return err
	}
	err = c.Command("docker", "push", dockerRepoWithTag)
	if err != nil {
		return err
	}

	return nil
}<commit_msg>dynamically create docker image tags from tags list and string parsing<commit_after>package CIScriptsDocker

import (
	"fmt"
	c "github.com/bcaldwell/ci-scripts/internal/CIScriptsHelpers"
	"os"
	"strings"
)

type BuildAndPushImage struct {
	Folder     string
	DockerRepo string
	DockerUser string
	DockerTags []string
}

func (b *BuildAndPushImage) Run() error {
	b.Folder, _ = c.ConfigFetch("docker.images.folder", ".")
	b.DockerRepo = c.RequiredConfigFetch("docker.images.dockerRepo")

	c.LogInfo("Build %s from folder %s", b.DockerRepo, b.Folder)

	b.DockerUser, _ = c.ConfigFetch("docker.user")

	dockerTagsString, _ := c.ConfigFetch("docker.tags", "_tags, _sha, latest")

	b.DockerTags = strings.Split(dockerTagsString, ",")
	b.parseTags()

	fmt.Println(b.DockerTags)

	dockerBuildCommand := []string{"docker", "build", "-t", b.DockerRepo, b.Folder}

	if args, ok := c.ConfigFetch("docker.image.build_args"); ok {
		dockerBuildCommand = append(dockerBuildCommand, strings.Split(args, " ")...)
	}

	err := c.Command(dockerBuildCommand...)
	if err != nil {
		return err
	}

	if b.DockerUser != "" && os.Getenv("DOCKER_PASS") != "" {
		c.Command("sh", "-c", fmt.Sprintf("docker login -u %s -p $DOCKER_PASS", b.DockerUser))
	}

	for _, tag := range b.DockerTags {
		dockerRepoWithTag := fmt.Sprintf("%s:%s", b.DockerRepo, tag)
		err = c.Command("docker", "tag", b.DockerRepo, dockerRepoWithTag)
		if err != nil {
			return err
		}
		err = c.Command("docker", "push", dockerRepoWithTag)
		if err != nil {
			return err
		}
	}

	return nil
}

func (b *BuildAndPushImage) parseTags() {
	tags := []string{}
	for _, tag := range b.DockerTags {
		switch strings.TrimSpace(tag) {
		case "_tags":
			tagList, _ := c.CaptureCommand("git", "tag", "-l", "--points-at", "HEAD")
			for _, tag := range strings.Split(string(tagList), "\n") {
				if tag != "" {
					tags = append(tags, strings.TrimSpace(tag))
				}
			}
		case "_sha":
			tag, _ := c.CaptureCommand("git", "rev-parse", "HEAD")
			tags = append(tags, strings.TrimSpace(string(tag)))
		default:
			tags = append(tags, strings.TrimSpace(tag))
		}
	}
	b.DockerTags = tags
}
{'fraction_non_alphanumeric': 0.17122093023255813, 'fraction_numerical': 0.0, 'mean_word_length': 4.310185185185185, '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': '5755020', 'n_tokens_mistral': 1330, 'n_tokens_neox': 1231, 'n_words': 261}