Fix every finding surfaced by the canonical .golangci.yml with golangci-lint v2.12.2 (refs #61), behavior-preserving throughout: - err113: dynamic errors replaced with package-level sentinels and %w wrapping; direct comparisons converted to errors.Is - goprintffuncname: printf-style helpers renamed with an f suffix (ui.Writer message methods, cli.ReportErrorf, database.Fatalf, vaultik stdoutf) and all call sites updated - revive: stuttering type names renamed (blob.Handler, blob.WithReader, blob.ChunkPosition, storage.URL, storage.Info), doc comments added, unused parameters blanked, package comments added - contextcheck/noctx: ctx threaded through blob.Packer (AddChunk/Flush/FinalizeBlob/PackChunks) and scanner call sites; context-aware exec and sql variants used - funlen/cyclop/gocognit/nestif/dupl: oversized or duplicated functions split into focused helpers across production and test code - paralleltest/tparallel/thelper/usetesting/testpackage: tests parallelized where safe (global log.Initialize kept in the serial phase), helpers marked, t.TempDir adopted, external test packages where only exported API is used - gosec: integer conversions clamped or justified, header timeouts added, remaining findings suppressed with per-site justifications - mnd/goconst/lll/wsl_v5/nlreturn/noinlineerr/errcheck and other mechanical findings fixed directly Remove the deprecated log.LogOptions alias (callers migrated to log.Options). make check is green.
143 lines
3.2 KiB
Go
143 lines
3.2 KiB
Go
package storage
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/url"
|
|
"strings"
|
|
)
|
|
|
|
// Storage URL scheme names.
|
|
const (
|
|
schemeFile = "file"
|
|
schemeS3 = "s3"
|
|
schemeRclone = "rclone"
|
|
)
|
|
|
|
// Sentinel errors for storage URL parsing.
|
|
var (
|
|
ErrEmptyStorageURL = errors.New("storage URL is empty")
|
|
ErrEmptyFilePath = errors.New("file URL path is empty")
|
|
ErrMissingBucket = errors.New("s3 URL missing bucket name")
|
|
ErrMissingRemote = errors.New("rclone URL missing remote name")
|
|
ErrUnsupportedScheme = errors.New(
|
|
"unsupported URL scheme: must start with s3://, file://, or rclone://")
|
|
ErrUnsupportedStorage = errors.New("unsupported storage scheme")
|
|
)
|
|
|
|
// URL represents a parsed storage URL.
|
|
type URL struct {
|
|
Scheme string // "s3", "file", or "rclone"
|
|
Bucket string // S3 bucket name (empty for file/rclone)
|
|
Prefix string // Path within bucket or filesystem base path
|
|
Endpoint string // S3 endpoint (optional, default AWS)
|
|
Region string // S3 region (optional)
|
|
UseSSL bool // Use HTTPS for S3 (default true)
|
|
RcloneRemote string // rclone remote name (for rclone:// URLs)
|
|
}
|
|
|
|
// ParseStorageURL parses a storage URL string.
|
|
// Supported formats:
|
|
// - s3://bucket/prefix?endpoint=host®ion=us-east-1&ssl=true
|
|
// - file:///absolute/path/to/backup
|
|
// - rclone://remote/path/to/backups
|
|
func ParseStorageURL(rawURL string) (*URL, error) {
|
|
if rawURL == "" {
|
|
return nil, ErrEmptyStorageURL
|
|
}
|
|
|
|
// Handle file:// URLs
|
|
if after, ok := strings.CutPrefix(rawURL, "file://"); ok {
|
|
path := after
|
|
if path == "" {
|
|
return nil, ErrEmptyFilePath
|
|
}
|
|
|
|
return &URL{
|
|
Scheme: schemeFile,
|
|
Prefix: path,
|
|
}, nil
|
|
}
|
|
|
|
// Handle s3:// URLs
|
|
if strings.HasPrefix(rawURL, "s3://") {
|
|
u, err := url.Parse(rawURL)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid URL: %w", err)
|
|
}
|
|
|
|
bucket := u.Host
|
|
if bucket == "" {
|
|
return nil, ErrMissingBucket
|
|
}
|
|
|
|
prefix := strings.TrimPrefix(u.Path, "/")
|
|
|
|
query := u.Query()
|
|
|
|
useSSL := true
|
|
if query.Get("ssl") == "false" {
|
|
useSSL = false
|
|
}
|
|
|
|
return &URL{
|
|
Scheme: schemeS3,
|
|
Bucket: bucket,
|
|
Prefix: prefix,
|
|
Endpoint: query.Get("endpoint"),
|
|
Region: query.Get("region"),
|
|
UseSSL: useSSL,
|
|
}, nil
|
|
}
|
|
|
|
// Handle rclone:// URLs
|
|
if strings.HasPrefix(rawURL, "rclone://") {
|
|
u, err := url.Parse(rawURL)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid URL: %w", err)
|
|
}
|
|
|
|
remote := u.Host
|
|
if remote == "" {
|
|
return nil, ErrMissingRemote
|
|
}
|
|
|
|
path := strings.TrimPrefix(u.Path, "/")
|
|
|
|
return &URL{
|
|
Scheme: schemeRclone,
|
|
Prefix: path,
|
|
RcloneRemote: remote,
|
|
}, nil
|
|
}
|
|
|
|
return nil, ErrUnsupportedScheme
|
|
}
|
|
|
|
// String returns a human-readable representation of the storage URL.
|
|
func (u *URL) String() string {
|
|
switch u.Scheme {
|
|
case schemeFile:
|
|
return "file://" + u.Prefix
|
|
case schemeS3:
|
|
endpoint := u.Endpoint
|
|
if endpoint == "" {
|
|
endpoint = "s3.amazonaws.com"
|
|
}
|
|
|
|
if u.Prefix != "" {
|
|
return fmt.Sprintf("s3://%s/%s (endpoint: %s)", u.Bucket, u.Prefix, endpoint)
|
|
}
|
|
|
|
return fmt.Sprintf("s3://%s (endpoint: %s)", u.Bucket, endpoint)
|
|
case schemeRclone:
|
|
if u.Prefix != "" {
|
|
return fmt.Sprintf("rclone://%s/%s", u.RcloneRemote, u.Prefix)
|
|
}
|
|
|
|
return "rclone://" + u.RcloneRemote
|
|
default:
|
|
return u.Scheme + "://?"
|
|
}
|
|
}
|