diff options
| -rw-r--r-- | cmd/mbf/main.go | 41 | ||||
| -rw-r--r-- | internal/rosa/state.go | 50 |
2 files changed, 90 insertions, 1 deletions
diff --git a/cmd/mbf/main.go b/cmd/mbf/main.go index eb250ec8..7ec1188a 100644 --- a/cmd/mbf/main.go +++ b/cmd/mbf/main.go @@ -515,6 +515,47 @@ func main() { }) c.NewCommand( + "tofu", + "Determine source checksum of named artifact", + func(args []string) error { + if len(args) != 1 { + return errors.New("tofu requires 1 argument") + } + handle := rosa.ArtifactH(unique.Make(args[0])) + + meta, _ := rosa.Load(handle) + if meta == nil { + return fmt.Errorf("unknown artifact %q", args[0]) + } + + v, err := meta.GetVersions(ctx) + if err != nil { + return err + } + + version := meta.GetLatest(v) + if version == meta.Version { + return nil + } + + err = cm.Do(func(cache *pkg.Cache) error { + _, _, err = cache.Cure(builtin.Std().LoadTOFU(handle, version)) + return err + }) + if err == nil { + return errors.New("TOFU attempt did not return an error") + } + + if e, ok := errors.AsType[*pkg.ChecksumMismatchError](err); !ok { + return err + } else { + fmt.Printf("%s\t%s\n", version, pkg.Encode(e.Got)) + return nil + } + }, + ) + + c.NewCommand( "daemon", "Service artifact IR with Rosa OS extensions", func([]string) error { diff --git a/internal/rosa/state.go b/internal/rosa/state.go index 7bb4ebfe..d6b2e65a 100644 --- a/internal/rosa/state.go +++ b/internal/rosa/state.go @@ -213,6 +213,13 @@ type S struct { // Must only be accessed after a call to getFrame. spool sync.Pool + // Whether to perform TOFU attempt. + tofu bool + // Target of TOFU attempt. + tofuName azalea.Ident + // TOFU version substitute. + tofuVersion string + // Options for [pkg.Artifact] created against [S]. opts int @@ -409,6 +416,16 @@ func (t Toolchain) Load(handle ArtifactH) (*Metadata, pkg.Artifact) { return r.meta, r.a } +// LoadTOFU is like Load, but for a TOFU attempt. +func (t Toolchain) LoadTOFU(handle ArtifactH, version string) pkg.Artifact { + s := t.Clone() + s.tofu = true + s.tofuName = azalea.Ident(handle.String()) + s.tofuVersion = version + _, a := s.New(t.stage).Load(handle) + return a +} + // MustLoad is like Load, but panics if the named [Artifact] is not registered. func (t Toolchain) MustLoad(handle ArtifactH) (*Metadata, pkg.Artifact) { meta, a := t.Load(handle) @@ -1333,12 +1350,43 @@ func (s *S) RegisterAzalea(r io.Reader, name string, b fs.FS) error { } } + var identVersion = unique.Make(azalea.Ident("version")) for _, f := range pending { if !s.Register(string(f.Ident), func(t Toolchain) (*Metadata, pkg.Artifact) { + var sub azalea.Substitutions + if t.tofu { + sub = azalea.Substitutions{ + t.tofuName: func( + name unique.Handle[azalea.Ident], + prev any, + ) (v any, ok bool) { + if name != identVersion { + return + } + + got, _ok := prev.(string) + if !_ok { + panic(azalea.TypeError{ + Concrete: reflect.TypeOf(prev), + Asserted: reflect.TypeFor[string](), + }) + } + + if t.tofuVersion == got { + panic(fmt.Errorf( + "superfluous TOFU attempt for %s-%s", + t.tofuName, t.tofuVersion, + )) + } + return t.tofuVersion, true + }, + } + } + v, set, err := azalea.Evaluate[cachedArtifact]( (&evalContext{b, f, t}).pf, t.getStack(), - nil, + sub, *f, ) if err != nil { |
