Define context for scoped operation across core types

It holds necessary state plus additional information relative to the
context which we are being run to (e.g. if we are in a terminal or not).
Besides in the future we can use it also as a contextual logger to
provide more smart logging capabilities.

This also replace the general global configuration instance that
previously was share between the core components.
This commit is contained in:
Ettore Di Giacinto
2021-10-21 00:13:02 +02:00
parent b9895c9e05
commit a1c669d3ae
89 changed files with 1774 additions and 1849 deletions

View File

@@ -4,9 +4,9 @@ import (
"sync"
"github.com/hashicorp/go-multierror"
"github.com/mudler/luet/pkg/api/core/types"
"github.com/mudler/luet/pkg/helpers"
fileHelper "github.com/mudler/luet/pkg/helpers/file"
. "github.com/mudler/luet/pkg/logger"
pkg "github.com/mudler/luet/pkg/package"
"github.com/mudler/luet/pkg/tree"
)
@@ -22,32 +22,30 @@ func (s *System) World() (pkg.Packages, error) {
return s.Database.World(), nil
}
type templatedata map[string]interface{}
func (s *System) ExecuteFinalizers(packs []pkg.Package) error {
func (s *System) ExecuteFinalizers(ctx *types.Context, packs []pkg.Package) error {
var errs error
executedFinalizer := map[string]bool{}
for _, p := range packs {
if fileHelper.Exists(p.Rel(tree.FinalizerFile)) {
out, err := helpers.RenderFiles(helpers.ChartFile(p.Rel(tree.FinalizerFile)), p.Rel(pkg.PackageDefinitionFile))
if err != nil {
Warning("Failed rendering finalizer for ", p.HumanReadableString(), err.Error())
ctx.Warning("Failed rendering finalizer for ", p.HumanReadableString(), err.Error())
errs = multierror.Append(errs, err)
continue
}
if _, exists := executedFinalizer[p.GetFingerPrint()]; !exists {
executedFinalizer[p.GetFingerPrint()] = true
Info("Executing finalizer for " + p.HumanReadableString())
ctx.Info("Executing finalizer for " + p.HumanReadableString())
finalizer, err := NewLuetFinalizerFromYaml([]byte(out))
if err != nil {
Warning("Failed reading finalizer for ", p.HumanReadableString(), err.Error())
ctx.Warning("Failed reading finalizer for ", p.HumanReadableString(), err.Error())
errs = multierror.Append(errs, err)
continue
}
err = finalizer.RunInstall(s)
err = finalizer.RunInstall(ctx, s)
if err != nil {
Warning("Failed running finalizer for ", p.HumanReadableString(), err.Error())
ctx.Warning("Failed running finalizer for ", p.HumanReadableString(), err.Error())
errs = multierror.Append(errs, err)
continue
}
@@ -79,15 +77,11 @@ func (s *System) Clean() {
}
func (s *System) ExistsPackageFile(file string) (bool, pkg.Package, error) {
Debug("Checking if file ", file, "belongs to any package")
s.buildFileIndex()
s.Lock()
defer s.Unlock()
if p, exists := s.fileIndex[file]; exists {
Debug(file, "belongs already to", p.HumanReadableString())
return exists, p, nil
}
Debug(file, "doesn't belong to any package")
return false, nil, nil
}