Refactor CLI to use flags instead of positional arguments

- Change all commands to use flags (--bucket, --prefix, etc.)
- Add --config flag to backup command
- Support VAULTIK_CONFIG environment variable for config path
- Use /etc/vaultik/config.yml as default config location
- Add test/config.yaml for testing
- Update tests to use environment variable for config path
- Add .gitignore for build artifacts and local configs
- Update documentation to reflect new CLI syntax
This commit is contained in:
2025-07-20 09:45:24 +02:00
parent 3e8b98dec6
commit bcbc186286
11 changed files with 207 additions and 59 deletions

View File

@@ -6,24 +6,23 @@ import (
"testing"
)
func TestMain(m *testing.M) {
// Set up test environment
testConfigPath := filepath.Join("..", "..", "test", "config.yaml")
if absPath, err := filepath.Abs(testConfigPath); err == nil {
_ = os.Setenv("VAULTIK_CONFIG", absPath)
}
code := m.Run()
os.Exit(code)
}
// TestConfigLoad ensures the config package can be imported and basic functionality works
func TestConfigLoad(t *testing.T) {
// Create a temporary config file
tmpDir := t.TempDir()
configPath := filepath.Join(tmpDir, "test-config.yaml")
configContent := `age_recipient: age1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
source_dirs:
- /tmp/test
s3:
endpoint: https://s3.example.com
bucket: test-bucket
access_key_id: test-key
secret_access_key: test-secret
`
if err := os.WriteFile(configPath, []byte(configContent), 0644); err != nil {
t.Fatalf("Failed to write test config: %v", err)
// Use the test config file
configPath := os.Getenv("VAULTIK_CONFIG")
if configPath == "" {
t.Fatal("VAULTIK_CONFIG environment variable not set")
}
// Test loading the config
@@ -37,11 +36,32 @@ s3:
t.Errorf("Expected age recipient to be set, got '%s'", cfg.AgeRecipient)
}
if len(cfg.SourceDirs) != 1 || cfg.SourceDirs[0] != "/tmp/test" {
t.Errorf("Expected source dirs to be ['/tmp/test'], got %v", cfg.SourceDirs)
if len(cfg.SourceDirs) != 2 {
t.Errorf("Expected 2 source dirs, got %d", len(cfg.SourceDirs))
}
if cfg.S3.Bucket != "test-bucket" {
t.Errorf("Expected S3 bucket to be 'test-bucket', got '%s'", cfg.S3.Bucket)
if cfg.SourceDirs[0] != "/tmp/vaultik-test-source" {
t.Errorf("Expected first source dir to be '/tmp/vaultik-test-source', got '%s'", cfg.SourceDirs[0])
}
if cfg.S3.Bucket != "vaultik-test-bucket" {
t.Errorf("Expected S3 bucket to be 'vaultik-test-bucket', got '%s'", cfg.S3.Bucket)
}
if cfg.Hostname != "test-host" {
t.Errorf("Expected hostname to be 'test-host', got '%s'", cfg.Hostname)
}
}
// TestConfigFromEnv tests loading config path from environment variable
func TestConfigFromEnv(t *testing.T) {
configPath := os.Getenv("VAULTIK_CONFIG")
if configPath == "" {
t.Skip("VAULTIK_CONFIG not set")
}
// Verify the file exists
if _, err := os.Stat(configPath); os.IsNotExist(err) {
t.Errorf("Config file does not exist at path from VAULTIK_CONFIG: %s", configPath)
}
}