Files
narratio/internal/app/artifact_catalog_command.go

85 lines
4.3 KiB
Go

package app
import (
"context"
"errors"
"sort"
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
"gitea.maximumdirect.net/eric/narratio/internal/config"
"go.maximumdirect.net/commandkit"
)
type artifactCatalogReport struct {
Artifacts []artifactDefinitionReport `json:"artifacts"`
}
type artifactDefinitionReport struct {
Key string `json:"key,omitempty"`
Source string `json:"source"`
Producer string `json:"producer"`
Output string `json:"output,omitempty"`
Enabled *bool `json:"enabled,omitempty"`
Dependencies []string `json:"dependencies"`
Family string `json:"family,omitempty"`
CharacterID string `json:"character_id,omitempty"`
DefinitionSources []string `json:"definition_sources,omitempty"`
FamilySources []string `json:"family_sources,omitempty"`
PartySource string `json:"party_source,omitempty"`
}
func artifactCatalogCommand(deps commandDependencies) commandkit.Command {
common := newConfigurationFlags(func(input *configurationSelection) *configurationSelection { return input })
return commandkit.NewCommand(commandkit.CommandSpec[configurationSelection]{
Name: "list", Summary: "List artifact definitions for the selected pipeline and party.",
Flags: []commandkit.Flag[configurationSelection]{common.Config, common.Overlay, common.Campaign, common.CampaignFile, common.PartyFile, common.SecretsDir},
CheckInput: checkConfigurationSelection,
Run: func(invocation commandkit.Invocation, input configurationSelection) commandkit.Outcome {
report, err := loadArtifactCatalogReport(invocation.Context, input, withCommandReporting(deps, invocation))
if report != nil {
err = errors.Join(err, invocation.Reporter().Report(commandkit.Report{Value: report}))
}
return reportOperationOutcome(invocation, err)
},
})
}
func loadArtifactCatalogReport(ctx context.Context, input configurationSelection, deps commandDependencies) (report *artifactCatalogReport, err error) {
deps = normalizeCommandDependencies(deps)
deps.secretsBootstrap = &secretsBootstrap{}
acquired := newConfigurationAcquisition(input, deps)
defer func() { err = errors.Join(err, acquired.Close()) }()
catalog, err := acquired.Catalog(ctx)
if err != nil {
return nil, err
}
return describeArtifactDefinitions(acquired.pipeline, catalog)
}
func describeArtifactDefinitions(pipeline *config.PipelineConfig, catalog *config.ArtifactCatalog) (*artifactCatalogReport, error) {
configured := artifacts.ConfiguredArtifactDefinitions(catalog.Artifacts)
effective, err := artifacts.ResolveEffectiveArtifactSet(configured, nil)
if err != nil {
return nil, err
}
runtime, err := artifacts.BootstrapRuntimeCatalog(configured, effective, artifacts.ExtractionDefinitionsFromConfig(pipeline.Notarius))
if err != nil {
return nil, err
}
report := &artifactCatalogReport{Artifacts: []artifactDefinitionReport{}}
for _, spec := range artifacts.RuntimeTranscriptArtifacts() {
report.Artifacts = append(report.Artifacts, artifactDefinitionReport{Source: spec.SourceID, Producer: spec.ProducerStage, Output: spec.CanonicalRelPath, Dependencies: []string{}})
}
if bounds, ok := runtime.Lookup(artifacts.ArtifactBoundsSession); ok {
report.Artifacts = append(report.Artifacts, artifactDefinitionReport{Source: bounds.SourceID, Producer: bounds.ProducerStage, Output: bounds.CanonicalRelPath, Dependencies: []string{}})
}
for _, record := range catalog.Records {
report.Artifacts = append(report.Artifacts, artifactDefinitionReport{Key: record.Key, Source: record.SourceID, Producer: "analyze", Output: record.OutputPath, Enabled: &record.Enabled, Dependencies: append([]string{}, record.Dependencies...), Family: record.FamilyKey, CharacterID: record.CharacterID, DefinitionSources: record.DefinitionSources, FamilySources: record.FamilySources, PartySource: record.PartySource})
}
for _, entry := range runtime.ListExtraction() {
enabled := pipeline.Notarius != nil && pipeline.Notarius.Enabled
report.Artifacts = append(report.Artifacts, artifactDefinitionReport{Key: entry.ExtractionKey, Source: entry.SourceID, Producer: entry.ProducerStage, Enabled: &enabled, Dependencies: []string{}})
}
sort.Slice(report.Artifacts, func(i, j int) bool { return report.Artifacts[i].Source < report.Artifacts[j].Source })
return report, nil
}