package publish import ( "context" "encoding/json" "fmt" "time" "gitea.maximumdirect.net/eric/distributor/internal/state" "gitea.maximumdirect.net/eric/distributor/internal/storage" ) func Execute(ctx context.Context, req Request, plan Plan) error { switch plan.Action { case ActionSkipSame, ActionSkipDestinationNewer: return nil case ActionPublishNew, ActionReplaceOlder: default: return fmt.Errorf("cannot execute action %s: %s", plan.Action, plan.Reason) } if plan.Action == ActionReplaceOlder { if plan.ExistingState == nil { return fmt.Errorf("replace requires existing destination state") } if err := req.DestinationBackend.DeleteManagedBundle(ctx, req.DestinationBundlePath, existingManagedOutputPaths(*plan.ExistingState), storage.DeleteOptions{IgnoreMissing: true, PruneEmptyDirs: true}); err != nil { return err } if err := ensureDestinationEmpty(ctx, req.DestinationBackend, req.DestinationBundlePath); err != nil { return err } } writtenOutputs := make([]Output, 0, len(plan.Outputs)) cleanup := func() { _ = req.DestinationBackend.DeleteManagedBundle(ctx, req.DestinationBundlePath, managedOutputPaths(writtenOutputs), storage.DeleteOptions{IgnoreMissing: true, PruneEmptyDirs: true}) } for _, output := range plan.Outputs { destinationPath, err := storage.Join(req.DestinationBundlePath, output.DestinationPath) if err != nil { cleanup() return err } data := output.Data if output.Kind == state.OutputKindSource { sourcePath, err := storage.Join(req.SourceBundle.RootRelativePath, output.SourcePath) if err != nil { cleanup() return err } data, err = req.SourceBackend.ReadFile(ctx, sourcePath) if err != nil { cleanup() return err } } if _, err := req.DestinationBackend.WriteFile(ctx, destinationPath, data, storage.WriteOptions{Overwrite: false, PreferAtomic: true}); err != nil { cleanup() return err } writtenOutputs = append(writtenOutputs, output) } destinationState := state.DistributorState{ SchemaVersion: state.SchemaVersion, DistributorVersion: req.DistributorVersion, PipelineID: req.PipelineID, DestinationID: req.DestinationID, PublishedAt: time.Now().UTC(), Source: state.SourceState{Manifest: req.SourceBundle.Manifest}, Outputs: stateOutputs(plan.Outputs), } if err := state.Validate(destinationState); err != nil { cleanup() return err } data, err := json.MarshalIndent(destinationState, "", " ") if err != nil { cleanup() return err } data = append(data, '\n') statePath, err := storage.StatePath(req.DestinationBundlePath) if err != nil { cleanup() return err } if _, err := req.DestinationBackend.WriteFile(ctx, statePath, data, storage.WriteOptions{Overwrite: false, PreferAtomic: true}); err != nil { cleanup() return err } return nil } func existingManagedOutputPaths(destinationState state.DistributorState) []string { paths := make([]string, 0, len(destinationState.Outputs)) for _, output := range destinationState.Outputs { paths = append(paths, output.Path) } return paths }