store

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 4, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package store provides abstractions for file storage operations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FileInfo

type FileInfo struct {
	Path    string
	IsDir   bool
	Size    int64
	ModTime time.Time
}

FileInfo represents file metadata.

type LocalStore

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

LocalStore implements Store using local filesystem and git.

func NewLocalStore

func NewLocalStore(path string, opts ...LocalStoreOption) (*LocalStore, error)

NewLocalStore creates a new local store at the given path.

func (*LocalStore) BeginTx

func (s *LocalStore) BeginTx(_ context.Context) (Transaction, error)

BeginTx starts a new transaction.

func (*LocalStore) Exists

func (s *LocalStore) Exists(ctx context.Context, path string) (bool, error)

Exists checks if a file exists.

func (*LocalStore) FS

func (s *LocalStore) FS() fs.FS

FS returns an fs.FS view of the store.

func (*LocalStore) IsRemoteEnabled

func (s *LocalStore) IsRemoteEnabled() bool

IsRemoteEnabled returns true if remote git operations are configured.

func (*LocalStore) List

func (s *LocalStore) List(ctx context.Context, dir string) ([]FileInfo, error)

List lists files in a directory.

func (*LocalStore) Lock

func (s *LocalStore) Lock()

Lock acquires the store's write lock for external coordination.

func (*LocalStore) Pull

func (s *LocalStore) Pull(ctx context.Context) error

Pull fetches and merges changes from the remote repository.

func (*LocalStore) Push

func (s *LocalStore) Push(ctx context.Context) error

Push pushes local commits to the remote repository. If a non-fast-forward error occurs, it will attempt to pull first and retry the push.

func (*LocalStore) Read

func (s *LocalStore) Read(ctx context.Context, path string) ([]byte, error)

Read reads a file from the store.

func (*LocalStore) RemoteConfig

func (s *LocalStore) RemoteConfig() *RemoteConfig

RemoteConfig returns the remote configuration.

func (*LocalStore) TestConnection

func (s *LocalStore) TestConnection(ctx context.Context) error

TestConnection tests the connection to the remote repository.

func (*LocalStore) Unlock

func (s *LocalStore) Unlock()

Unlock releases the store's write lock.

type LocalStoreOption

type LocalStoreOption func(*LocalStore)

LocalStoreOption configures LocalStore.

func WithLogger

func WithLogger(l *slog.Logger) LocalStoreOption

WithLogger sets a custom logger for the store.

func WithRemoteConfig

func WithRemoteConfig(cfg *RemoteConfig) LocalStoreOption

WithRemoteConfig sets the remote git configuration.

type ReadFSProvider

type ReadFSProvider interface {
	FS() fs.FS
}

ReadFSProvider returns an fs.FS view for read-only consumers.

type RemoteConfig

type RemoteConfig struct {
	Storage      StorageMode   // Storage mode: "local", "remote", or auto-detect (NTN_STORAGE)
	URL          string        // Remote git repository URL (NTN_GIT_URL)
	Password     string        // Password/token for HTTPS auth (NTN_GIT_PASS)
	Branch       string        // Target branch (NTN_GIT_BRANCH)
	User         string        // Commit author name (NTN_GIT_USER)
	Email        string        // Commit author email (NTN_GIT_EMAIL)
	Commit       bool          // Enable automatic git commit (NTN_COMMIT)
	CommitPeriod time.Duration // Periodic commit interval during sync (NTN_COMMIT_PERIOD)
	Push         *bool         // Push to remote after commits (NTN_PUSH), nil means auto-detect
}

RemoteConfig holds configuration for remote git operations.

func LoadRemoteConfigFromEnv

func LoadRemoteConfigFromEnv() *RemoteConfig

LoadRemoteConfigFromEnv loads remote configuration from environment variables.

func (*RemoteConfig) EffectiveStorageMode

func (c *RemoteConfig) EffectiveStorageMode() StorageMode

EffectiveStorageMode returns the effective storage mode after auto-detection. If Storage is set explicitly, it returns that value. Otherwise, it returns "remote" if URL is configured, or "local" if not.

func (*RemoteConfig) GetAuth

func (c *RemoteConfig) GetAuth() (transport.AuthMethod, error)

GetAuth returns the appropriate authentication method for the remote URL.

func (*RemoteConfig) GetCommitPeriod

func (c *RemoteConfig) GetCommitPeriod() time.Duration

GetCommitPeriod returns the periodic commit interval.

func (*RemoteConfig) IsCommitEnabled

func (c *RemoteConfig) IsCommitEnabled() bool

IsCommitEnabled returns true if automatic commits are enabled.

func (*RemoteConfig) IsEnabled

func (c *RemoteConfig) IsEnabled() bool

IsEnabled returns true if remote operations should be used. This checks both the storage mode and whether a URL is configured.

func (*RemoteConfig) IsPushEnabled

func (c *RemoteConfig) IsPushEnabled() bool

IsPushEnabled returns true if push to remote is enabled. When NTN_PUSH is not explicitly set, defaults to true if NTN_GIT_URL is set.

func (*RemoteConfig) IsSSH

func (c *RemoteConfig) IsSSH() bool

IsSSH returns true if the URL is an SSH URL.

func (*RemoteConfig) TestConnection

func (c *RemoteConfig) TestConnection(ctx context.Context) error

TestConnection tests the connection to the remote repository.

type StorageMode

type StorageMode string

StorageMode defines the storage mode for git operations.

const (
	// StorageModeAuto automatically detects the storage mode based on configuration.
	StorageModeAuto StorageMode = ""
	// StorageModeLocal uses local-only storage (no remote operations).
	StorageModeLocal StorageMode = "local"
	// StorageModeRemote uses remote storage (pull/push enabled).
	StorageModeRemote StorageMode = "remote"
)

type Store

type Store interface {
	// Read operations
	Read(ctx context.Context, path string) ([]byte, error)
	Exists(ctx context.Context, path string) (bool, error)
	List(ctx context.Context, dir string) ([]FileInfo, error)

	// Transaction management - all writes go through transactions
	BeginTx(ctx context.Context) (Transaction, error)

	// Remote operations
	Push(ctx context.Context) error

	// Concurrency control for external coordination (e.g., sync worker)
	Lock()
	Unlock()
}

Store abstracts file storage with transactional write operations.

type Transaction

type Transaction interface {
	// Write operations - applied immediately to filesystem
	Write(ctx context.Context, path string, content []byte) error
	WriteStream(ctx context.Context, path string, reader io.Reader) (int64, error)
	Delete(ctx context.Context, path string) error
	Mkdir(ctx context.Context, path string) error

	// Commit creates a git commit with all changes made in this transaction.
	// After commit, the transaction can continue to be used for more changes.
	Commit(ctx context.Context, message string) error

	// Rollback reverts all uncommitted changes and closes the transaction.
	Rollback(ctx context.Context) error
}

Transaction groups multiple write operations. All writes are applied immediately to the filesystem. Commit creates a git commit with all changes. Rollback reverts uncommitted changes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL