Add local source publication

This commit is contained in:
2026-05-31 02:21:36 +00:00
parent aeee91940d
commit e296361042
17 changed files with 1019 additions and 84 deletions

View File

@@ -5,7 +5,10 @@ import (
"fmt"
"io"
"gitea.maximumdirect.net/eric/distributor/internal/adapters/local"
"gitea.maximumdirect.net/eric/distributor/internal/bundle"
"gitea.maximumdirect.net/eric/distributor/internal/config"
"gitea.maximumdirect.net/eric/distributor/internal/publish"
)
type RunOptions struct {
@@ -18,9 +21,6 @@ func Run(ctx context.Context, options RunOptions) error {
if err := ctx.Err(); err != nil {
return err
}
if !options.DryRun {
return fmt.Errorf("run command: %w", ErrNotImplemented)
}
configPath := options.ConfigPath
if configPath == "" {
@@ -30,25 +30,77 @@ func Run(ctx context.Context, options RunOptions) error {
if err != nil {
return err
}
return writeRunSummary(options.Stdout, cfg)
return runConfig(ctx, cfg, options)
}
func writeRunSummary(w io.Writer, cfg config.Config) error {
if w == nil {
return nil
}
if _, err := fmt.Fprintf(w, "Configured pipelines: %d\n", len(cfg.Pipelines)); err != nil {
return err
}
for _, pipeline := range cfg.Pipelines {
if _, err := fmt.Fprintf(w, "- %s: source=%s destinations=%d\n", pipeline.ID, pipeline.Source.Backend, len(pipeline.Destinations)); err != nil {
func runConfig(ctx context.Context, cfg config.Config, options RunOptions) error {
if options.Stdout != nil {
if _, err := fmt.Fprintf(options.Stdout, "Configured pipelines: %d\n", len(cfg.Pipelines)); err != nil {
return err
}
for _, destination := range pipeline.Destinations {
if _, err := fmt.Fprintf(w, " - %s: backend=%s publish_source=%t publish_html=%t\n", destination.ID, destination.Backend, destination.Publish.Source, destination.Publish.HTML); err != nil {
}
for _, pipeline := range cfg.Pipelines {
if pipeline.Source.Backend != config.BackendLocal {
return fmt.Errorf("pipeline %s source backend %s is not implemented for execution", pipeline.ID, pipeline.Source.Backend)
}
sourceBackend, err := local.New(pipeline.Source.Path)
if err != nil {
return err
}
bundles, err := bundle.Discover(ctx, sourceBackend, "")
if err != nil {
return fmt.Errorf("pipeline %s discover source bundles: %w", pipeline.ID, err)
}
if options.Stdout != nil {
if _, err := fmt.Fprintf(options.Stdout, "- %s: source=local bundles=%d destinations=%d\n", pipeline.ID, len(bundles), len(pipeline.Destinations)); err != nil {
return err
}
}
for _, sourceBundle := range bundles {
for _, destination := range pipeline.Destinations {
if destination.Backend != config.BackendLocal {
return fmt.Errorf("pipeline %s destination %s backend %s is not implemented for execution", pipeline.ID, destination.ID, destination.Backend)
}
destinationBackend, err := local.New(destination.Path)
if err != nil {
return err
}
req := publish.Request{
PipelineID: pipeline.ID,
DestinationID: destination.ID,
SourceBundle: sourceBundle,
SourceBackend: sourceBackend,
DestinationBackend: destinationBackend,
DestinationBundlePath: sourceBundle.RootRelativePath,
Publish: *destination.Publish,
Transfer: destination.Transfer,
DistributorVersion: Version,
}
plan, err := publish.Build(ctx, req)
if options.Stdout != nil {
writePlanLine(options.Stdout, plan, err)
}
if err != nil {
return fmt.Errorf("pipeline %s destination %s bundle %s: %w", pipeline.ID, destination.ID, displayBundlePath(sourceBundle.RootRelativePath), err)
}
if !options.DryRun {
if err := publish.Execute(ctx, req, plan); err != nil {
return fmt.Errorf("pipeline %s destination %s bundle %s: %w", pipeline.ID, destination.ID, displayBundlePath(sourceBundle.RootRelativePath), err)
}
}
}
}
}
return nil
}
func writePlanLine(w io.Writer, plan publish.Plan, planErr error) {
if w == nil {
return
}
if planErr != nil {
fmt.Fprintf(w, " - bundle=%s destination=%s action=error reason=%q\n", displayBundlePath(plan.BundlePath), plan.DestinationID, planErr.Error())
return
}
fmt.Fprintf(w, " - bundle=%s destination=%s action=%s outputs=%d reason=%q\n", displayBundlePath(plan.BundlePath), plan.DestinationID, plan.Action, len(plan.Outputs), plan.Reason)
}