196 lines
6.9 KiB
Go
196 lines
6.9 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
|
)
|
|
|
|
type inspectionFlags struct {
|
|
pipelinePath string
|
|
campaignPath string
|
|
campaignFilePath string
|
|
overlay singletonStringFlag
|
|
secretsDir singletonStringFlag
|
|
}
|
|
|
|
type inspectionConfig struct {
|
|
PipelinePath string
|
|
Pipeline *config.PipelineConfig
|
|
CampaignPath string
|
|
Campaign *config.CampaignConfig
|
|
Party config.ResolvedParty
|
|
ArtifactCatalog *config.ArtifactCatalog
|
|
}
|
|
|
|
// ConfigArtifacts validates and reports the effective concrete artifact catalog
|
|
// without loading session state or runtime collaborators.
|
|
func ConfigArtifacts(ctx context.Context, args []string, out io.Writer) error {
|
|
return configArtifactsWithDependencies(ctx, args, out, commandDependencies{})
|
|
}
|
|
|
|
func configArtifactsWithDependencies(_ context.Context, args []string, out io.Writer, deps commandDependencies) error {
|
|
flags, err := parseInspectionFlags("config artifacts", args, out)
|
|
if err != nil {
|
|
if errors.Is(err, flag.ErrHelp) {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
resolved, err := resolveInspectionConfigWithDependencies(flags, deps)
|
|
if err != nil {
|
|
return fmt.Errorf("config artifacts: %w", err)
|
|
}
|
|
records := resolved.ArtifactCatalog.Records
|
|
if _, err := fmt.Fprintln(out, "key\tsource\tdefinitions\tenabled\toutput\tdependencies\tfamily\tcharacter\tfamily_source\tparty_source"); err != nil {
|
|
return err
|
|
}
|
|
for _, record := range records {
|
|
fields := []string{
|
|
record.Key,
|
|
record.SourceID,
|
|
strings.Join(normalizedCatalogPaths(record.DefinitionSources), ","),
|
|
strconv.FormatBool(record.Enabled),
|
|
record.OutputPath,
|
|
strings.Join(record.Dependencies, ","),
|
|
record.FamilyKey,
|
|
record.CharacterID,
|
|
strings.Join(normalizedCatalogPaths(record.FamilySources), ","),
|
|
config.NormalizedConfigurationPath(record.PartySource),
|
|
}
|
|
for index := range fields {
|
|
fields[index] = escapeCatalogField(fields[index])
|
|
}
|
|
if _, err := fmt.Fprintln(out, strings.Join(fields, "\t")); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func normalizedCatalogPaths(paths []string) []string {
|
|
result := make([]string, len(paths))
|
|
for index, path := range paths {
|
|
result[index] = config.NormalizedConfigurationPath(path)
|
|
}
|
|
sort.Strings(result)
|
|
return result
|
|
}
|
|
|
|
func escapeCatalogField(value string) string {
|
|
value = strings.ReplaceAll(value, "\\", "\\\\")
|
|
value = strings.ReplaceAll(value, "\t", "\\t")
|
|
value = strings.ReplaceAll(value, "\r", "\\r")
|
|
return strings.ReplaceAll(value, "\n", "\\n")
|
|
}
|
|
|
|
func parseInspectionFlags(command string, args []string, out io.Writer) (inspectionFlags, error) {
|
|
fs := flag.NewFlagSet(command, flag.ContinueOnError)
|
|
fs.SetOutput(out)
|
|
var flags inspectionFlags
|
|
fs.StringVar(&flags.pipelinePath, "config", "", "path to pipeline.yml (optional; defaults searched)")
|
|
fs.StringVar(&flags.campaignPath, "campaign", "", "campaign ID")
|
|
fs.StringVar(&flags.campaignFilePath, "campaign-file", "", "path to campaign.yml")
|
|
flags.overlay.name = "overlay"
|
|
fs.Var(&flags.overlay, "overlay", "named pipeline overlay")
|
|
flags.secretsDir.name = "secrets-dir"
|
|
fs.Var(&flags.secretsDir, "secrets-dir", "secrets directory (optional; defaults to /usr/local/etc/narratio/secrets)")
|
|
fs.Usage = func() {
|
|
fmt.Fprintf(out, "Usage: narratio %s [--config <pipeline.yml>] [--campaign <id> | --campaign-file <campaign.yml>] [--overlay <name>] [--secrets-dir <path>]\n\n", command)
|
|
fmt.Fprintln(out, "Resolves configuration without a session, workspace, manifest, adapters, or external services.")
|
|
fmt.Fprintln(out)
|
|
fmt.Fprintln(out, "Flags:")
|
|
fs.PrintDefaults()
|
|
}
|
|
if err := fs.Parse(args); err != nil {
|
|
return inspectionFlags{}, fmt.Errorf("%s: invalid flags: %w", command, err)
|
|
}
|
|
if fs.NArg() != 0 {
|
|
return inspectionFlags{}, fmt.Errorf("%s: unexpected positional arguments", command)
|
|
}
|
|
if strings.TrimSpace(flags.campaignPath) != "" && strings.TrimSpace(flags.campaignFilePath) != "" {
|
|
return inspectionFlags{}, fmt.Errorf("%s: --campaign and --campaign-file are mutually exclusive", command)
|
|
}
|
|
return flags, nil
|
|
}
|
|
|
|
func resolveInspectionConfigWithDependencies(flags inspectionFlags, deps commandDependencies) (*inspectionConfig, error) {
|
|
pipelinePath, pipeline, err := loadPipelineConfigWithDependencies(flags.pipelinePath, config.PipelineLoadOptions{Overlay: flags.overlay.pointer()}, flags.secretsDir.pointer(), deps)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
needsCampaign := pipelineHasArtifactFamilies(pipeline)
|
|
hasCampaignSelection := strings.TrimSpace(flags.campaignPath) != "" || strings.TrimSpace(flags.campaignFilePath) != ""
|
|
if !needsCampaign && !hasCampaignSelection {
|
|
if err := validateInspectionPipeline(pipelinePath, pipeline); err != nil {
|
|
return nil, err
|
|
}
|
|
catalog, err := config.DeriveArtifactCatalog(pipeline, config.ResolvedParty{})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &inspectionConfig{PipelinePath: pipelinePath, Pipeline: pipeline, ArtifactCatalog: catalog}, nil
|
|
}
|
|
|
|
campaignPath, err := resolveCampaignConfigPath(pipeline, flags.campaignPath, flags.campaignFilePath)
|
|
if err != nil {
|
|
if needsCampaign {
|
|
return nil, fmt.Errorf("pipeline.scriptorium.artifact_families requires a campaign: %w", err)
|
|
}
|
|
return nil, err
|
|
}
|
|
campaign, err := config.LoadCampaign(campaignPath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if selectedID := strings.TrimSpace(flags.campaignPath); selectedID != "" && strings.TrimSpace(flags.campaignFilePath) == "" {
|
|
if got := config.CampaignID(campaign); got != selectedID {
|
|
return nil, fmt.Errorf("campaign config %q invalid: campaign_id %q does not match selected campaign %q", campaignPath, got, selectedID)
|
|
}
|
|
}
|
|
loaded, err := config.LoadPipelineCampaign(pipelinePath, pipeline, campaignPath, campaign)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := validateInspectionPipeline(loaded.PipelinePath, loaded.Pipeline); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := validateInspectionCampaign(loaded.CampaignPath, loaded.Campaign); err != nil {
|
|
return nil, err
|
|
}
|
|
return &inspectionConfig{
|
|
PipelinePath: loaded.PipelinePath,
|
|
Pipeline: loaded.Pipeline,
|
|
CampaignPath: loaded.CampaignPath,
|
|
Campaign: loaded.Campaign,
|
|
Party: loaded.Party,
|
|
ArtifactCatalog: loaded.ArtifactCatalog,
|
|
}, nil
|
|
}
|
|
|
|
func pipelineHasArtifactFamilies(pipeline *config.PipelineConfig) bool {
|
|
return pipeline != nil && pipeline.Scriptorium != nil && len(pipeline.Scriptorium.ArtifactFamilies) != 0
|
|
}
|
|
|
|
func validateInspectionPipeline(path string, pipeline *config.PipelineConfig) error {
|
|
if err := config.ValidatePipelineConfig(pipeline); err != nil {
|
|
return fmt.Errorf("pipeline config %q invalid: %w", path, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func validateInspectionCampaign(path string, campaign *config.CampaignConfig) error {
|
|
if err := config.ValidateCampaignConfig(campaign); err != nil {
|
|
return fmt.Errorf("campaign config %q invalid: %w", path, err)
|
|
}
|
|
return nil
|
|
}
|