156 lines
6.6 KiB
Go
156 lines
6.6 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
|
|
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
|
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
|
"go.maximumdirect.net/commandkit"
|
|
)
|
|
|
|
type lockCommandInput struct {
|
|
Selection configurationSelection
|
|
PreviousSession commandkit.Supplied[string]
|
|
Source commandkit.Supplied[string]
|
|
Reason commandkit.Supplied[string]
|
|
Force commandkit.Supplied[bool]
|
|
}
|
|
type lockCommandReport struct {
|
|
Campaign string `json:"campaign"`
|
|
SessionID string `json:"session_id"`
|
|
Operation string `json:"operation"`
|
|
Source string `json:"source,omitempty"`
|
|
Result string `json:"result"`
|
|
Complete bool `json:"complete"`
|
|
Locks []lockListEntry `json:"locks"`
|
|
}
|
|
type lockListEntry struct {
|
|
Source string `json:"source"`
|
|
Reason string `json:"reason,omitempty"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
ConfiguredForPublish bool `json:"configured_for_publish"`
|
|
}
|
|
|
|
func lockCommand(operation string, deps commandDependencies) commandkit.Command {
|
|
common := newConfigurationFlags(func(input **lockCommandInput) *configurationSelection { return &(*input).Selection })
|
|
flags := []commandkit.Flag[*lockCommandInput]{common.Config, common.Overlay, common.Campaign, common.CampaignFile, common.SessionFile, common.PartyFile, common.SecretsDir,
|
|
commandkit.ValueFlag(commandkit.FlagMeta{Name: "previous-session-id", Usage: "Assert the previous session identity."}, nil, stringValue, func(input **lockCommandInput, value commandkit.Supplied[string]) { (*input).PreviousSession = value }),
|
|
}
|
|
if operation == "add" {
|
|
flags = append(flags,
|
|
commandkit.ValueFlag(commandkit.FlagMeta{Name: "reason", Usage: "Reason for the publish lock."}, nil, stringValue, func(input **lockCommandInput, value commandkit.Supplied[string]) { (*input).Reason = value }),
|
|
commandkit.BoolFlag(commandkit.FlagMeta{Name: "force", Usage: "Replace an existing lock conditionally."}, false, func(input **lockCommandInput, value commandkit.Supplied[bool]) { (*input).Force = value }),
|
|
)
|
|
}
|
|
arguments := []commandkit.Argument[*lockCommandInput]{commandkit.ValueArgument(commandkit.ArgumentMeta{Name: "SESSION", Usage: "Session identity."}, true, stringValue, func(input **lockCommandInput, value commandkit.Supplied[string]) { (*input).Selection.Session = value })}
|
|
if operation != "list" {
|
|
arguments = append(arguments, commandkit.ValueArgument(commandkit.ArgumentMeta{Name: "SOURCE", Usage: "Canonical publish source identity."}, true, stringValue, func(input **lockCommandInput, value commandkit.Supplied[string]) { (*input).Source = value }))
|
|
}
|
|
return commandkit.NewCommand(commandkit.CommandSpec[*lockCommandInput]{Name: operation, Summary: "Manage independent remote publication locks.", NewInput: func() *lockCommandInput { return &lockCommandInput{} }, Flags: flags, Arguments: arguments,
|
|
CheckInput: func(input *lockCommandInput) error {
|
|
if err := checkConfigurationSelection(input.Selection); err != nil {
|
|
return err
|
|
}
|
|
if operation != "list" {
|
|
_, err := artifacts.NewRemoteLock(input.Source.Value, strings.TrimSpace(input.Reason.Value), nowUTC())
|
|
return err
|
|
}
|
|
return nil
|
|
},
|
|
Run: func(invocation commandkit.Invocation, input *lockCommandInput) commandkit.Outcome {
|
|
report, err := executeLockCommand(invocation.Context, input, operation, withCommandReporting(deps, invocation))
|
|
if report != nil {
|
|
err = errors.Join(err, invocation.Reporter().Report(commandkit.Report{Value: report}))
|
|
}
|
|
return operationOutcome(err)
|
|
},
|
|
})
|
|
}
|
|
|
|
func executeLockCommand(ctx context.Context, input *lockCommandInput, operation string, deps commandDependencies) (report *lockCommandReport, err error) {
|
|
deps = normalizeCommandDependencies(deps)
|
|
deps.secretsBootstrap = &secretsBootstrap{}
|
|
acquired := newConfigurationAcquisition(input.Selection, deps)
|
|
acquired.previousSessionID = input.PreviousSession.Value
|
|
defer func() { err = errors.Join(err, acquired.Close()) }()
|
|
report = &lockCommandReport{SessionID: input.Selection.Session.Value, Operation: operation, Source: input.Source.Value, Result: "not_applied"}
|
|
cfg, err := acquired.Domain(ctx)
|
|
if err != nil {
|
|
return report, err
|
|
}
|
|
report.Campaign = config.CampaignID(cfg.Campaign)
|
|
if err := config.Validate(cfg); err != nil {
|
|
return report, err
|
|
}
|
|
store, err := newCommandObjectStoreWithDependencies(ctx, cfg, nil, deps)
|
|
if err != nil {
|
|
return report, err
|
|
}
|
|
prefix, err := artifacts.ResolvePublishSessionPrefix(cfg, nil)
|
|
if err != nil {
|
|
return report, err
|
|
}
|
|
if operation == "list" {
|
|
locks, err := artifacts.ListRemoteLocks(ctx, store, prefix)
|
|
if err != nil {
|
|
return report, err
|
|
}
|
|
selected := map[string]bool{}
|
|
for _, rule := range cfg.PublicationOutputs() {
|
|
selected[rule.Source] = true
|
|
}
|
|
report.Locks = make([]lockListEntry, 0, len(locks))
|
|
for _, lock := range locks {
|
|
report.Locks = append(report.Locks, lockListEntry{Source: lock.Source, Reason: lock.Reason, CreatedAt: lock.CreatedAt, ConfiguredForPublish: selected[lock.Source]})
|
|
}
|
|
report.Complete = true
|
|
report.Result = "listed"
|
|
return report, nil
|
|
}
|
|
result, err := applyRemoteLockMutation(ctx, store, prefix, operation, input.Source.Value, input.Reason.Value, input.Force.Value)
|
|
report.Result = result
|
|
report.Complete = err == nil
|
|
return report, err
|
|
}
|
|
|
|
func applyRemoteLockMutation(ctx context.Context, store storage.ObjectStore, prefix, operation, source, reason string, force bool) (string, error) {
|
|
if operation == "remove" {
|
|
current, err := artifacts.LookupRemoteLock(ctx, store, prefix, source)
|
|
if err != nil {
|
|
return "not_applied", err
|
|
}
|
|
if err := artifacts.RemoveRemoteLock(ctx, store, prefix, source); err != nil {
|
|
return "not_applied", err
|
|
}
|
|
if !current.Present {
|
|
return "not_locked", nil
|
|
}
|
|
return "unlocked", nil
|
|
}
|
|
lock, err := artifacts.NewRemoteLock(source, strings.TrimSpace(reason), nowUTC())
|
|
if err != nil {
|
|
return "not_applied", err
|
|
}
|
|
if !force {
|
|
current, err := artifacts.LookupRemoteLock(ctx, store, prefix, source)
|
|
if err != nil {
|
|
return "not_applied", err
|
|
}
|
|
if current.Present {
|
|
return "already_locked", fmt.Errorf("remote lock for %q already exists; pass --force to update", source)
|
|
}
|
|
}
|
|
if err := artifacts.AddRemoteLock(ctx, store, prefix, lock, force); err != nil {
|
|
if errors.Is(err, storage.ErrConditionNotMet) {
|
|
return "not_applied", fmt.Errorf("remote lock for %q changed concurrently; retry: %w", source, err)
|
|
}
|
|
return "not_applied", err
|
|
}
|
|
return "locked", nil
|
|
}
|