package kopia import ( "context" "fmt" "log" "os" "os/exec" "path/filepath" ) // LocalProvider implements the Provider interface for local storage type LocalProvider struct { BasePath string } // NewLocalProvider creates a new local provider func NewLocalProvider(basePath string) *LocalProvider { // If basePath is empty, use a default location if basePath == "" { basePath = filepath.Join(os.Getenv("HOME"), ".backea", "repos") } return &LocalProvider{ BasePath: basePath, } } // 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) log.Printf("Connecting to local repository at %s with config: %s", repoPath, configPath) // Ensure the directory exists if err := os.MkdirAll(repoPath, 0755); err != nil { return fmt.Errorf("failed to create repository directory: %w", err) } // Try to connect to existing repository with config file connectCmd := exec.CommandContext( ctx, "kopia", "--config-file", configPath, "repository", "connect", "filesystem", "--path", repoPath, "--password", password, ) err := connectCmd.Run() if err != nil { // Connection failed, create new repository log.Printf("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 fmt.Errorf("failed to create repository: %w\nOutput: %s", err, createOutput) } } 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" }