g3

backend

import "github.com/afreidah/g3/internal/backend"

Package backend is a generated GoMock package.

Index

Variables

Sentinel errors for common S3 error conditions.

var (
    ErrObjectNotFound = &S3Error{StatusCode: 404, Code: "NoSuchKey", Message: "The specified key does not exist"}
    ErrBucketNotFound = &S3Error{StatusCode: 404, Code: "NoSuchBucket", Message: "The specified bucket does not exist"}
    ErrBucketExists   = &S3Error{StatusCode: 409, Code: "BucketAlreadyOwnedByYou", Message: "Your previous request to create the named bucket succeeded"}
)

type BucketInfo

BucketInfo describes a bucket in a ListBuckets response.

type BucketInfo struct {
    Name         string
    CreationDate time.Time
}

type BucketRecord

BucketRecord represents a stored bucket’s label mapping.

type BucketRecord struct {
    Name      string
    LabelID   string
    CreatedAt time.Time
}

type GetObjectResult

GetObjectResult holds the response data from a GetObject operation.

type GetObjectResult struct {
    Body         io.ReadCloser
    Size         int64
    ContentType  string
    ETag         string
    LastModified time.Time
    Metadata     map[string]string
}

type GmailBackend

GmailBackend implements ObjectBackend using Gmail for metadata and Google Drive for object data storage.

type GmailBackend struct {
    // contains filtered or unexported fields
}

func NewGmailBackend

func NewGmailBackend(ctx context.Context, cfg *config.GmailConfig, store MetadataStore) (*GmailBackend, error)

NewGmailBackend creates a GmailBackend from the provided configuration. It builds an OAuth2 token source from the configured client credentials and refresh token, then initializes the Gmail and Drive API clients.

func (*GmailBackend) CreateBucket

func (g *GmailBackend) CreateBucket(ctx context.Context, bucket string) error

CreateBucket creates a new Gmail label for the given bucket name.

func (*GmailBackend) DeleteObject

func (g *GmailBackend) DeleteObject(ctx context.Context, bucket, key string) error

DeleteObject removes an object by deleting its Drive file, Gmail message, and metadata store record. Returns nil if the object does not exist (S3 idempotency).

func (*GmailBackend) GetObject

func (g *GmailBackend) GetObject(ctx context.Context, bucket, key string) (*GetObjectResult, error)

GetObject retrieves object data from Google Drive using the file ID stored in the local index or Gmail email metadata.

func (*GmailBackend) HeadObject

func (g *GmailBackend) HeadObject(ctx context.Context, bucket, key string) (*HeadObjectResult, error)

HeadObject retrieves only the metadata for an object. Checks the local SQLite index first (zero API calls). Falls back to Gmail API on cache miss.

func (*GmailBackend) ListBuckets

func (g *GmailBackend) ListBuckets(ctx context.Context) ([]BucketInfo, error)

ListBuckets returns all Gmail labels that match the configured label prefix, interpreted as S3 buckets.

func (*GmailBackend) ListObjects

func (g *GmailBackend) ListObjects(ctx context.Context, bucket, prefix, delimiter, startAfter string, maxKeys int) (*ListObjectsResult, error)

ListObjects searches Gmail for emails matching the given bucket and prefix, returning results in S3 ListObjectsV2 format with delimiter and pagination support.

func (*GmailBackend) PutObject

func (g *GmailBackend) PutObject(ctx context.Context, bucket, key string, body io.Reader, size int64, contentType string, metadata map[string]string) (string, error)

PutObject uploads object data to Google Drive and stores a metadata-only email in Gmail as a pointer. If an object with the same key exists, it is deleted first (last-write-wins semantics).

type HeadObjectResult

HeadObjectResult holds metadata from a HeadObject operation.

type HeadObjectResult struct {
    Size         int64
    ContentType  string
    ETag         string
    LastModified time.Time
    Metadata     map[string]string
}

type ListObjectsResult

ListObjectsResult holds the response from a ListObjectsV2 operation.

type ListObjectsResult struct {
    Contents       []ObjectInfo
    CommonPrefixes []string
    IsTruncated    bool
    NextStartAfter string
}

type MetadataStore

MetadataStore is the interface for the local metadata index. Decouples the backend from the concrete SQLite implementation for testability.

type MetadataStore interface {
    PutObject(ctx context.Context, rec *ObjectRecord) error
    GetObject(ctx context.Context, bucket, key string) (*ObjectRecord, error)
    DeleteObject(ctx context.Context, bucket, key string) error
    ListObjects(ctx context.Context, bucket, prefix, startAfter string, maxKeys int) ([]*ObjectRecord, error)
    PutBucket(ctx context.Context, rec *BucketRecord) error
    GetBucket(ctx context.Context, name string) (*BucketRecord, error)
    ListBuckets(ctx context.Context) ([]*BucketRecord, error)
}

type MockObjectBackend

MockObjectBackend is a mock of ObjectBackend interface.

type MockObjectBackend struct {
    // contains filtered or unexported fields
}

func NewMockObjectBackend

func NewMockObjectBackend(ctrl *gomock.Controller) *MockObjectBackend

NewMockObjectBackend creates a new mock instance.

func (*MockObjectBackend) CreateBucket

func (m *MockObjectBackend) CreateBucket(ctx context.Context, bucket string) error

CreateBucket mocks base method.

func (*MockObjectBackend) DeleteObject

func (m *MockObjectBackend) DeleteObject(ctx context.Context, bucket, key string) error

DeleteObject mocks base method.

func (*MockObjectBackend) EXPECT

func (m *MockObjectBackend) EXPECT() *MockObjectBackendMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockObjectBackend) GetObject

func (m *MockObjectBackend) GetObject(ctx context.Context, bucket, key string) (*GetObjectResult, error)

GetObject mocks base method.

func (*MockObjectBackend) HeadObject

func (m *MockObjectBackend) HeadObject(ctx context.Context, bucket, key string) (*HeadObjectResult, error)

HeadObject mocks base method.

func (*MockObjectBackend) ListBuckets

func (m *MockObjectBackend) ListBuckets(ctx context.Context) ([]BucketInfo, error)

ListBuckets mocks base method.

func (*MockObjectBackend) ListObjects

func (m *MockObjectBackend) ListObjects(ctx context.Context, bucket, prefix, delimiter, startAfter string, maxKeys int) (*ListObjectsResult, error)

ListObjects mocks base method.

func (*MockObjectBackend) PutObject

func (m *MockObjectBackend) PutObject(ctx context.Context, bucket, key string, body io.Reader, size int64, contentType string, metadata map[string]string) (string, error)

PutObject mocks base method.

type MockObjectBackendMockRecorder

MockObjectBackendMockRecorder is the mock recorder for MockObjectBackend.

type MockObjectBackendMockRecorder struct {
    // contains filtered or unexported fields
}

func (*MockObjectBackendMockRecorder) CreateBucket

func (mr *MockObjectBackendMockRecorder) CreateBucket(ctx, bucket any) *gomock.Call

CreateBucket indicates an expected call of CreateBucket.

func (*MockObjectBackendMockRecorder) DeleteObject

func (mr *MockObjectBackendMockRecorder) DeleteObject(ctx, bucket, key any) *gomock.Call

DeleteObject indicates an expected call of DeleteObject.

func (*MockObjectBackendMockRecorder) GetObject

func (mr *MockObjectBackendMockRecorder) GetObject(ctx, bucket, key any) *gomock.Call

GetObject indicates an expected call of GetObject.

func (*MockObjectBackendMockRecorder) HeadObject

func (mr *MockObjectBackendMockRecorder) HeadObject(ctx, bucket, key any) *gomock.Call

HeadObject indicates an expected call of HeadObject.

func (*MockObjectBackendMockRecorder) ListBuckets

func (mr *MockObjectBackendMockRecorder) ListBuckets(ctx any) *gomock.Call

ListBuckets indicates an expected call of ListBuckets.

func (*MockObjectBackendMockRecorder) ListObjects

func (mr *MockObjectBackendMockRecorder) ListObjects(ctx, bucket, prefix, delimiter, startAfter, maxKeys any) *gomock.Call

ListObjects indicates an expected call of ListObjects.

func (*MockObjectBackendMockRecorder) PutObject

func (mr *MockObjectBackendMockRecorder) PutObject(ctx, bucket, key, body, size, contentType, metadata any) *gomock.Call

PutObject indicates an expected call of PutObject.

type ObjectBackend

ObjectBackend defines the storage operations for the S3 gateway.

type ObjectBackend interface {
    PutObject(ctx context.Context, bucket, key string, body io.Reader, size int64, contentType string, metadata map[string]string) (etag string, err error)
    GetObject(ctx context.Context, bucket, key string) (*GetObjectResult, error)
    HeadObject(ctx context.Context, bucket, key string) (*HeadObjectResult, error)
    DeleteObject(ctx context.Context, bucket, key string) error
    ListObjects(ctx context.Context, bucket, prefix, delimiter, startAfter string, maxKeys int) (*ListObjectsResult, error)
    ListBuckets(ctx context.Context) ([]BucketInfo, error)
    CreateBucket(ctx context.Context, bucket string) error
}

type ObjectInfo

ObjectInfo describes a single object in a listing response.

type ObjectInfo struct {
    Key          string
    Size         int64
    LastModified time.Time
    ETag         string
}

type ObjectRecord

ObjectRecord represents a stored object’s metadata and Gmail reference.

type ObjectRecord struct {
    Bucket      string
    Key         string
    GmailMsgID  string
    DriveFileID string
    ETag        string
    Size        int64
    ContentType string
    CreatedAt   time.Time
    Metadata    map[string]string
}

type S3Error

S3Error represents a typed error that maps to an S3 HTTP error response.

type S3Error struct {
    StatusCode int
    Code       string
    Message    string
}

func (*S3Error) Error

func (e *S3Error) Error() string

Error implements the error interface.

type SyncMetadata

SyncMetadata holds parsed metadata fields needed by the sync command.

type SyncMetadata struct {
    ContentType string
    ETag        string
    Size        int64
    DriveFileID string
    CreatedAt   time.Time
    Metadata    map[string]string
}

func ParseMetadataForSync

func ParseMetadataForSync(bodyText string) (*SyncMetadata, error)

ParseMetadataForSync parses the JSON email body and returns metadata fields for populating the SQLite index during sync.

Generated by gomarkdoc