func (m *Manager) ManagePackages(imageTarget shared.ImageTarget) error {
	var validSets []shared.DefinitionPackagesSet
	for _, set := range m.def.Packages.Sets {
		if !shared.ApplyFilter(&set, m.def.Image.Release, m.def.Image.ArchitectureMapped, m.def.Image.Variant, m.def.Targets.Type, imageTarget) {
			continue
		}
		validSets = append(validSets, set)
	}
	if len(validSets) == 0 && !m.def.Packages.Update {
		return nil
	}
	err := m.mgr.refresh()
	if err != nil {
		return fmt.Errorf("Failed to refresh: %w", err)
	}
	if m.def.Packages.Update {
		err = m.mgr.update()
		if err != nil {
			return fmt.Errorf("Failed to update: %w", err)
		}
		for _, action := range m.def.GetRunnableActions("post-update", imageTarget) {
			err = shared.RunScript(action.Action)
			if err != nil {
				return fmt.Errorf("Failed to run post-update: %w", err)
			}
		}
	}
	for _, set := range optimizePackageSets(validSets) {
		if set.Action == "install" {
			err = m.mgr.install(set.Packages, set.Flags)
		} else if set.Action == "remove" {
			err = m.mgr.remove(set.Packages, set.Flags)
		}
		if err != nil {
			return fmt.Errorf("Failed to %s packages: %w", set.Action, err)
		}
	}
	if m.def.Packages.Cleanup {
		err = m.mgr.clean()
		if err != nil {
			return fmt.Errorf("Failed to clean up packages: %w", err)
		}
	}
	return nil
}
ManagePackages manages packages.
package managers
import (
	"errors"
	"fmt"
	"strings"
	"go.uber.org/zap"
	"github.com/lxc/distrobuilder/shared"
)
var ErrUnknownManager = errors.New("Unknown manager")
type managerFlags struct {
	global  []string
	install []string
	remove  []string
	clean   []string
	update  []string
	refresh []string
}
type managerHooks struct {
	clean      func() error
	preRefresh func() error
}
type managerCommands struct {
	clean   string
	install string
	refresh string
	remove  string
	update  string
}
type Manager struct {
	mgr manager
	def shared.Definition
}
type manager interface {
	init(logger *zap.SugaredLogger, definition shared.Definition)
	load() error
	manageRepository(repo shared.DefinitionPackagesRepository) error
	install(pkgs, flags []string) error
	remove(pkgs, flags []string) error
	clean() error
	refresh() error
	update() error
}
var managers = map[string]func() manager{
	"":           func() manager { return &custom{} },
	"apk":        func() manager { return &apk{} },
	"apt":        func() manager { return &apt{} },
	"dnf":        func() manager { return &dnf{} },
	"egoportage": func() manager { return &egoportage{} },
	"equo":       func() manager { return &equo{} },
	"luet":       func() manager { return &luet{} },
	"opkg":       func() manager { return &opkg{} },
	"pacman":     func() manager { return &pacman{} },
	"portage":    func() manager { return &portage{} },
	"xbps":       func() manager { return &xbps{} },
	"yum":        func() manager { return &yum{} },
	"zypper":     func() manager { return &zypper{} },
}
func Load(managerName string, logger *zap.SugaredLogger, definition shared.Definition) (*Manager, error) {
	df, ok := managers[managerName]
	if !ok {
		return nil, ErrUnknownManager
	}
	d := df()
	d.init(logger, definition)
	err := d.load()
	if err != nil {
		return nil, fmt.Errorf("Failed to load manager %q: %w", managerName, err)
	}
	return &Manager{def: definition, mgr: d}, nil
}