<commit_before>package cleanup

import (
	"fmt"
	"strings"

	"github.com/flant/werf/pkg/docker_registry"
	"github.com/flant/werf/pkg/logger"
)

type CommonRepoOptions struct {
	StagesStorage string
	ImagesRepo    string
	ImagesNames   []string
	DryRun        bool
}

func repoImages(options CommonRepoOptions) ([]docker_registry.RepoImage, error) {
	var repoImages []docker_registry.RepoImage

	isNamelessImage := len(options.ImagesNames) == 0
	if isNamelessImage {
		namelessImages, err := docker_registry.ImagesByWerfImageLabel(options.ImagesRepo, "true")
		if err != nil {
			return nil, err
		}

		repoImages = append(repoImages, namelessImages...)
	} else {
		for _, imageName := range options.ImagesNames {
			repository := fmt.Sprintf("%s/%s", options.ImagesRepo, imageName)
			images, err := docker_registry.ImagesByWerfImageLabel(repository, "true")
			if err != nil {
				return nil, err
			}

			repoImages = append(repoImages, images...)
		}
	}

	return repoImages, nil
}

func repoImageStagesImages(options CommonRepoOptions) ([]docker_registry.RepoImage, error) {
	return docker_registry.ImagesByWerfImageLabel(options.StagesStorage, "false")
}

func repoImagesRemove(images []docker_registry.RepoImage, options CommonRepoOptions) error {
	isGCR, err := docker_registry.IsGCR(options.ImagesRepo)
	if err != nil {
		return err
	}

	for _, image := range images {
		if isGCR {
			if err := GCRImageRemove(image, options); err != nil {
				return err
			}
		} else {
			if err := repoImageRemove(image, options); err != nil {
				return err
			}
		}
	}

	return nil
}

func GCRImageRemove(image docker_registry.RepoImage, options CommonRepoOptions) error {
	reference := strings.Join([]string{image.Repository, image.Tag}, ":")
	if err := repoReferenceRemove(reference, options); err != nil {
		return err
	}

	return nil
}

func repoImageRemove(image docker_registry.RepoImage, options CommonRepoOptions) error {
	digest, err := image.Digest()
	if err != nil {
		return err
	}

	reference := strings.Join([]string{image.Repository, digest.String()}, "@")
	if err != nil {
		return err
	}

	if err := repoReferenceRemove(reference, options); err != nil {
		return err
	}
	logger.LogInfoF("  tag: %s\n", image.Tag)
	logger.LogOptionalLn()

	return nil
}

func repoReferenceRemove(reference string, options CommonRepoOptions) error {
	logger.LogLn(reference)
	if !options.DryRun {
		err := docker_registry.ImageDelete(reference)
		if err != nil {
			return err
		}
	}

	return nil
}

func exceptRepoImages(repoImages []docker_registry.RepoImage, repoImagesToExclude ...docker_registry.RepoImage) []docker_registry.RepoImage {
	var newRepoImages []docker_registry.RepoImage

Loop:
	for _, repoImage := range repoImages {
		reference := strings.Join([]string{repoImage.Repository, repoImage.Tag}, ":")
		for _, repoImageToExclude := range repoImagesToExclude {
			referenceToExclude := strings.Join([]string{repoImageToExclude.Repository, repoImageToExclude.Tag}, ":")
			if reference == referenceToExclude {
				continue Loop
			}
		}

		newRepoImages = append(newRepoImages, repoImage)
	}

	return newRepoImages
}
<commit_msg>Fix removing nameless images in repo<commit_after>package cleanup

import (
	"strings"

	"github.com/flant/werf/pkg/docker_registry"
	"github.com/flant/werf/pkg/logger"
)

type CommonRepoOptions struct {
	StagesStorage string
	ImagesRepo    string
	ImagesNames   []string
	DryRun        bool
}

func repoImages(options CommonRepoOptions) ([]docker_registry.RepoImage, error) {
	var repoImages []docker_registry.RepoImage

	isNamelessImage := len(options.ImagesNames) == 0
	if isNamelessImage {
		namelessImages, err := docker_registry.ImagesByWerfImageLabel(options.ImagesRepo, "true")
		if err != nil {
			return nil, err
		}

		repoImages = append(repoImages, namelessImages...)
	} else {
		for _, imageName := range options.ImagesNames {
			repository := strings.Join([]string{options.ImagesRepo, imageName}, "/")
			images, err := docker_registry.ImagesByWerfImageLabel(repository, "true")
			if err != nil {
				return nil, err
			}

			repoImages = append(repoImages, images...)
		}
	}

	return repoImages, nil
}

func repoImageStagesImages(options CommonRepoOptions) ([]docker_registry.RepoImage, error) {
	return docker_registry.ImagesByWerfImageLabel(options.StagesStorage, "false")
}

func repoImagesRemove(images []docker_registry.RepoImage, options CommonRepoOptions) error {
	isGCR, err := docker_registry.IsGCR(options.ImagesRepo)
	if err != nil {
		return err
	}

	for _, image := range images {
		if isGCR {
			if err := GCRImageRemove(image, options); err != nil {
				return err
			}
		} else {
			if err := repoImageRemove(image, options); err != nil {
				return err
			}
		}
	}

	return nil
}

func GCRImageRemove(image docker_registry.RepoImage, options CommonRepoOptions) error {
	reference := strings.Join([]string{image.Repository, image.Tag}, ":")
	if err := repoReferenceRemove(reference, options); err != nil {
		return err
	}

	return nil
}

func repoImageRemove(image docker_registry.RepoImage, options CommonRepoOptions) error {
	digest, err := image.Digest()
	if err != nil {
		return err
	}

	reference := strings.Join([]string{image.Repository, digest.String()}, "@")
	if err != nil {
		return err
	}

	if err := repoReferenceRemove(reference, options); err != nil {
		return err
	}
	logger.LogInfoF("  tag: %s\n", image.Tag)
	logger.LogOptionalLn()

	return nil
}

func repoReferenceRemove(reference string, options CommonRepoOptions) error {
	logger.LogLn(reference)
	if !options.DryRun {
		err := docker_registry.ImageDelete(reference)
		if err != nil {
			return err
		}
	}

	return nil
}

func exceptRepoImages(repoImages []docker_registry.RepoImage, repoImagesToExclude ...docker_registry.RepoImage) []docker_registry.RepoImage {
	var newRepoImages []docker_registry.RepoImage

Loop:
	for _, repoImage := range repoImages {
		reference := strings.Join([]string{repoImage.Repository, repoImage.Tag}, ":")
		for _, repoImageToExclude := range repoImagesToExclude {
			referenceToExclude := strings.Join([]string{repoImageToExclude.Repository, repoImageToExclude.Tag}, ":")
			if reference == referenceToExclude {
				continue Loop
			}
		}

		newRepoImages = append(newRepoImages, repoImage)
	}

	return newRepoImages
}

{'fraction_non_alphanumeric': 0.11504846655013506, 'fraction_numerical': 0.0003178134435086604, 'mean_word_length': 4.540492957746479, 'pattern_counts': {'":': 6, '<': 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': '4908026', 'n_tokens_mistral': 2156, 'n_tokens_neox': 2019, 'n_words': 451}