<commit_before>package sous

import (
	"log"
	"strings"
)

// MissingImageNamesError reports that we couldn't get names for one or more source versions
type MissingImageNamesError struct {
	Causes []error
}

// Resolve drives the Sous deployment resolution process. It calls out to the
// appropriate components to compute the intended deployment set, collect the
// actual set, compute the diffs and then issue the commands to rectify those
// differences.
func Resolve(rc RectificationClient, config State) error {
	gdm, err := config.Deployments()
	if err != nil {
		return err
	}

	err = guardImageNamesKnown(rc, gdm)
	if err != nil {
		return err
	}

	sc := NewSetCollector(rc)
	ads, err := sc.GetRunningDeployment(config.BaseURLs())
	if err != nil {
		return err
	}

	differ := ads.Diff(gdm)

	errs := make(chan RectificationError)

	Rectify(differ, errs, rc)

	for err := range errs {
		log.Printf("err = %+v\n", err)
	}
	return nil
}

func (e *MissingImageNamesError) Error() string {
	causeStrs := make([]string, 0, len(e.Causes)+1)
	causeStrs = append(causeStrs, "Image names are unknown to Sous for source versions")
	for _, c := range e.Causes {
		causeStrs = append(causeStrs, c.Error())
	}
	return strings.Join(causeStrs, "  \n")
}

func guardImageNamesKnown(rc RectificationClient, gdm Deployments) error {
	es := make([]error, 0, len(gdm))
	for _, d := range gdm {
		_, err := rc.ImageName(d)
		if err != nil {
			es = append(es, err)
		}
	}
	if len(es) > 0 {
		return &MissingImageNamesError{es}
	}
	return nil
}

//ResolveFromDir does everything that Resolve does, plus it adds loading the
//Sous config from a directory of YAML files. This use case is important for
//proof-of-concept, but long term we expect to be able to abstract the storage
//of the Sous state away, so this might be deprecated at some point.
func ResolveFromDir(rc RectificationClient, dir string) error {
	config, err := LoadState(dir)
	if err != nil {
		return err
	}

	return Resolve(rc, config)
}<commit_msg>Adds filtered versions of Resolve... methods<commit_after>package sous

import (
	"log"
	"strings"
)

// MissingImageNamesError reports that we couldn't get names for one or more source versions
type MissingImageNamesError struct {
	Causes []error
}

// Resolve drives the Sous deployment resolution process. It calls out to the
// appropriate components to compute the intended deployment set, collect the
// actual set, compute the diffs and then issue the commands to rectify those
// differences.
func Resolve(rc RectificationClient, state State) error {
	return ResolveFilteredDeployments(rc, state, nil)
}

// ResolveFilteredDeployments is similar to Resolve, but also accepts a
// predicate to filter those deployments. See Deploments.Filter for details.
func ResolveFilteredDeployments(rc RectificationClient, state State, predicate func(*Deployment) bool) error {
	gdm, err := state.Deployments()
	gdm = gdm.Filter(predicate)
	if err != nil {
		return err
	}

	err = guardImageNamesKnown(rc, gdm)
	if err != nil {
		return err
	}

	sc := NewSetCollector(rc)
	ads, err := sc.GetRunningDeployment(state.BaseURLs())
	if err != nil {
		return err
	}

	differ := ads.Diff(gdm)

	errs := make(chan RectificationError)
	defer close(errs)

	Rectify(differ, errs, rc)

	for err := range errs {
		log.Printf("err = %+v\n", err)
	}
	return nil
}

func (e *MissingImageNamesError) Error() string {
	causeStrs := make([]string, 0, len(e.Causes)+1)
	causeStrs = append(causeStrs, "Image names are unknown to Sous for source versions")
	for _, c := range e.Causes {
		causeStrs = append(causeStrs, c.Error())
	}
	return strings.Join(causeStrs, "  \n")
}

func guardImageNamesKnown(rc RectificationClient, gdm Deployments) error {
	es := make([]error, 0, len(gdm))
	for _, d := range gdm {
		_, err := rc.ImageName(d)
		if err != nil {
			es = append(es, err)
		}
	}
	if len(es) > 0 {
		return &MissingImageNamesError{es}
	}
	return nil
}

// ResolveFromDir does everything that Resolve does, plus it adds loading the
// Sous config from a directory of YAML files. This use case is important for
// proof-of-concept, but long term we expect to be able to abstract the storage
// of the Sous state away, so this might be deprecated at some point.
func ResolveFromDir(rc RectificationClient, dir string) error {
	return ResolveFromDirFiltered(rc, dir, nil)
}

// ResolveFromDirFiltered is similar to ResolveFromDir, but additionally filters
// the deployments to be resolved based on the predicate.
func ResolveFromDirFiltered(rc RectificationClient, dir string, predicate func(*Deployment) bool) error {
	config, err := LoadState(dir)
	if err != nil {
		return err
	}

	return ResolveFilteredDeployments(rc, config, predicate)
}
{'fraction_non_alphanumeric': 0.10120686004658057, 'fraction_numerical': 0.0016938386618674571, 'mean_word_length': 4.174151150054764, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2828228', 'n_tokens_mistral': 1576, 'n_tokens_neox': 1501, 'n_words': 541}