diff options
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/cli/cli.go | 18 | ||||
| -rw-r--r-- | internal/cli/profile_test.go | 18 |
2 files changed, 29 insertions, 7 deletions
diff --git a/internal/cli/cli.go b/internal/cli/cli.go index 11914b1..af408c5 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -228,9 +228,13 @@ func (a *App) handleProfileCmd() error { return a.handleProfileList() case "import": if len(a.Args) < 4 { - return fmt.Errorf("usage: wg-wrap profile import <path>") + return fmt.Errorf("usage: wg-wrap profile import <path> [name]") } - return a.handleProfileImport(a.Args[3]) + var name string + if len(a.Args) > 4 { + name = a.Args[4] + } + return a.handleProfileImport(a.Args[3], name) case "configure": if len(a.Args) < 4 { return fmt.Errorf("usage: wg-wrap profile configure <name>") @@ -307,7 +311,7 @@ func (a *App) handleProfileList() error { return nil } -func (a *App) handleProfileImport(srcPath string) error { +func (a *App) handleProfileImport(srcPath string, name string) error { profilesDir := a.getPathManager().ConfigDir() if err := os.MkdirAll(profilesDir, 0755); err != nil { return fmt.Errorf("failed to create profiles directory: %w", err) @@ -317,10 +321,12 @@ func (a *App) handleProfileImport(srcPath string) error { return fmt.Errorf("invalid WireGuard configuration at %s: %w", srcPath, err) } - baseName := filepath.Base(srcPath) - name := strings.TrimSuffix(baseName, filepath.Ext(baseName)) if name == "" { - return fmt.Errorf("invalid source filename") + baseName := filepath.Base(srcPath) + name = strings.TrimSuffix(baseName, filepath.Ext(baseName)) + if name == "" { + return fmt.Errorf("invalid source filename") + } } destPath := filepath.Join(profilesDir, name+".conf") diff --git a/internal/cli/profile_test.go b/internal/cli/profile_test.go index 17a5bc6..c9b1274 100644 --- a/internal/cli/profile_test.go +++ b/internal/cli/profile_test.go @@ -41,6 +41,7 @@ func TestProfileImport(t *testing.T) { t.Fatalf("failed to create source conf: %v", err) } + // 1. Test importing with derived name (no explicit name argument) app := NewApp([]string{"wg-wrap", "profile", "import", srcFile}) app.ConfigDir = profilesDir @@ -49,11 +50,26 @@ func TestProfileImport(t *testing.T) { t.Errorf("expected no error, got %v", err) } - // Verify the file was actually copied + // Verify the file was actually copied to derived name destFile := filepath.Join(profilesDir, "source.conf") if _, err := os.Stat(destFile); os.IsNotExist(err) { t.Errorf("expected profile to be imported to %s", destFile) } + + // 2. Test importing with explicit name argument + customName := "custom-vpn" + appCustom := NewApp([]string{"wg-wrap", "profile", "import", srcFile, customName}) + appCustom.ConfigDir = profilesDir + + err = appCustom.Route() + if err != nil { + t.Errorf("expected no error for custom name import, got %v", err) + } + + destCustomFile := filepath.Join(profilesDir, customName+".conf") + if _, err := os.Stat(destCustomFile); os.IsNotExist(err) { + t.Errorf("expected profile to be imported to %s", destCustomFile) + } } func TestProfileDelete(t *testing.T) { |
