package config import ( "path/filepath" "time" "gitea.maximumdirect.net/eric/distributor/internal/transform" ) const DefaultConfigPath = "/usr/local/etc/distributor/config.yml" const ( BackendLocal = "local" BackendSSH = "ssh" BackendS3 = "s3" BackendHTTPUpload = "http_upload" ) const ( ValidationActionFail = "fail" ) const ( TransferActionSkip = "skip" TransferActionReplace = "replace" TransferActionFail = "fail" ) const ( TransformModeSidecar = transform.MarkdownModeSidecar TransformModeIndex = transform.MarkdownModeIndex ) const ( PathMappingPreserveRelative = "preserve_relative" PathMappingFixed = "fixed" ) const ( LinkPrimaryAuto = "auto" LinkPrimaryHTML = "html" LinkPrimarySource = "source" ) const ( ReconciliationModeReplace = "replace" ReconciliationModeMerge = "merge" ) const ( StateModeSingleOwner = "single_owner" StateModeSharedRoot = "shared_root" ) const DefaultS3Region = "us-east-1" const ( DefaultHTTPBind = "127.0.0.1:8080" DefaultHTTPStagingRoot = "/var/spool/distributor" DefaultHTTPMaxUploadSize = ByteSize(20 * 1024 * 1024) DefaultHTTPQueueSize = 16 DefaultHTTPMaxConcurrency = 1 DefaultHTTPRetention = Duration(24 * time.Hour) ) func ApplyDefaults(cfg *Config) { applyHTTPServerDefaults(&cfg.Server.HTTP) for pipelineIndex := range cfg.Pipelines { pipeline := &cfg.Pipelines[pipelineIndex] applyBackendDefaults(&pipeline.Source) if pipeline.Source.Backend == BackendHTTPUpload { applyHTTPUploadDefaults(&pipeline.Source.Upload, pipeline.ID, cfg.Server.HTTP) } if pipeline.Validation.OnDigestMismatch == "" { pipeline.Validation.OnDigestMismatch = ValidationActionFail } for destinationIndex := range pipeline.Destinations { destination := &pipeline.Destinations[destinationIndex] applyDestinationDefaults(destination) if destination.Publish == nil { destination.Publish = &PublishPolicy{Source: true} } if destination.Transform.MarkdownToHTML != nil && destination.Transform.MarkdownToHTML.Mode == "" { destination.Transform.MarkdownToHTML.Mode = TransformModeSidecar } if destination.PathMap.Mode == "" { destination.PathMap.Mode = PathMappingPreserveRelative } if destination.Links != nil && destination.Links.Primary == "" { destination.Links.Primary = LinkPrimaryAuto } if destination.State.Mode == "" { destination.State.Mode = StateModeSingleOwner } if destination.Reconciliation.Mode == "" { destination.Reconciliation.Mode = ReconciliationModeReplace } if destination.Transfer.OnDestinationSame == "" { destination.Transfer.OnDestinationSame = TransferActionSkip } if destination.Transfer.OnDestinationOlder == "" { destination.Transfer.OnDestinationOlder = TransferActionReplace } if destination.Transfer.OnDestinationNewer == "" { destination.Transfer.OnDestinationNewer = TransferActionSkip } if destination.Transfer.OnConflict == "" { destination.Transfer.OnConflict = TransferActionFail } } } } func applyHTTPServerDefaults(server *HTTPServer) { if server.Bind == "" { server.Bind = DefaultHTTPBind } if server.StagingRoot == "" { server.StagingRoot = DefaultHTTPStagingRoot } if server.MaxUploadSize == nil { server.MaxUploadSize = byteSize(DefaultHTTPMaxUploadSize) } if server.QueueSize == 0 { server.QueueSize = DefaultHTTPQueueSize } if server.MaxConcurrency == 0 { server.MaxConcurrency = DefaultHTTPMaxConcurrency } if server.Retention == nil { server.Retention = duration(DefaultHTTPRetention) } } func applyHTTPUploadDefaults(upload *HTTPUpload, pipelineID string, server HTTPServer) { if upload.StagingPath == "" && pipelineID != "" { upload.StagingPath = filepath.Join(server.StagingRoot, pipelineID) } if upload.MaxUploadSize == nil && server.MaxUploadSize != nil { upload.MaxUploadSize = byteSize(*server.MaxUploadSize) } } func byteSize(value ByteSize) *ByteSize { return &value } func duration(value Duration) *Duration { return &value } func applyBackendDefaults(backend *Backend) { applyStorageBackendDefaults(backend.Backend, &backend.Port, &backend.SSH, &backend.Region, &backend.Prefix, &backend.ForcePath) } func applyDestinationDefaults(destination *Destination) { applyStorageBackendDefaults(destination.Backend, &destination.Port, &destination.SSH, &destination.Region, &destination.Prefix, &destination.ForcePath) } func applyStorageBackendDefaults(backend string, port *int, ssh *SSH, region, prefix *string, forcePath **bool) { if backend == BackendSSH { if *port == 0 { *port = 22 } if ssh.HostKeyPolicy == "" { ssh.HostKeyPolicy = HostKeyPolicyAcceptNew } } if backend == BackendS3 { applyS3Defaults(region, prefix, forcePath) } } func applyS3Defaults(region, prefix *string, forcePath **bool) { if *region == "" { *region = DefaultS3Region } *prefix = NormalizeS3Prefix(*prefix) if *forcePath == nil { defaultForcePath := true *forcePath = &defaultForcePath } }