From b1b68a4aa441d9ce39d05f85338e371a704dd601 Mon Sep 17 00:00:00 2001 From: James O'Doherty Date: Fri, 29 May 2026 19:30:26 -0400 Subject: feat(cli,parser): support custom profile names and overhaul WireGuard .conf parser for robustness - CLI: - Add optional `[name]` argument to `wg-wrap profile import [name]` to allow overriding the imported profile name. If not provided, it falls back to the derived filename. - Update `README.md` command documentation to reflect custom profile names and list the `wg-wrap profile stop ` subcommand. - Expand `internal/cli/profile_test.go` to cover derived vs custom-named profile imports. - WG Configuration Parser: - Overhaul `pkg/wgconf/wgconf.go` to support case-insensitivity on section headers (e.g. `[peer]`, `[interface]`) and key names (e.g. `privatekey`, `allowedips`). - Implement robust trailing comment stripping (both `#` and `;`) while preserving inline comment-like characters in cryptographic keys (e.g. `key-with-hash-inside#123`) using whitespace-padded match logic. - Clean up and normalize leading/trailing spaces/tabs on parsed keys, values, and list elements (e.g. `AllowedIPs` and `DNS` fields). - Gracefully ignore unrecognized keys (e.g. `MTU`, `ListenPort`, `PresharedKey`) without returning errors. - Add comprehensive tests in `pkg/wgconf/wgconf_test.go` covering inline/block comments, formatting variations, unrecognized keys, and case-insensitivity. --- internal/cli/cli.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'internal/cli/cli.go') 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 ") + return fmt.Errorf("usage: wg-wrap profile import [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 ") @@ -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") -- cgit v1.2.3