2025-03-28 19:57:44 +01:00

86 lines
2.2 KiB
Go

package kopia
import (
"backea/internal/logging"
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
)
// LocalProvider implements the Provider interface for local storage
type LocalProvider struct {
BasePath string
logger *logging.Logger
}
// NewLocalProvider creates a new local provider
func NewLocalProvider(basePath string) *LocalProvider {
if basePath == "" {
basePath = filepath.Join(os.Getenv("HOME"), ".backea", "repos")
}
return &LocalProvider{
BasePath: basePath,
logger: logging.GetLogger(),
}
}
// Connect connects to a local repository
func (p *LocalProvider) Connect(ctx context.Context, serviceName string, password string, configPath string) error {
repoPath := filepath.Join(p.BasePath, serviceName)
p.logger.Info("Connecting to local repository at %s with config: %s", repoPath, configPath)
if err := os.MkdirAll(repoPath, 0755); err != nil {
return NewKopiaError("failed to create repository directory", err)
}
connectCmd := exec.CommandContext(
ctx,
"kopia",
"--config-file", configPath,
"repository", "connect", "filesystem",
"--path", repoPath,
"--password", password,
)
err := connectCmd.Run()
if err != nil {
p.logger.Info("Creating new local repository for %s at destination: %s", serviceName, repoPath)
createCmd := exec.CommandContext(
ctx,
"kopia",
"--config-file", configPath,
"repository", "create", "filesystem",
"--path", repoPath,
"--password", password,
)
createOutput, err := createCmd.CombinedOutput()
if err != nil {
return NewKopiaError(fmt.Sprintf("failed to create repository: %s", string(createOutput)), err)
}
}
return nil
}
// GetRepositoryParams returns parameters for local filesystem operations
func (p *LocalProvider) GetRepositoryParams(serviceName string) ([]string, error) {
repoPath := filepath.Join(p.BasePath, serviceName)
return []string{
"filesystem",
"--path", repoPath,
}, nil
}
// GetBucketName returns the path for a service
func (p *LocalProvider) GetBucketName(serviceName string) string {
return filepath.Join(p.BasePath, serviceName)
}
// GetProviderType returns the provider type identifier
func (p *LocalProvider) GetProviderType() string {
return "local"
}