1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
package cli
import (
"flag"
"fmt"
"git.theodohertyfamily.com/tools/wg-wrap/internal/config"
"git.theodohertyfamily.com/tools/wg-wrap/internal/namespace"
)
type App struct {
Args []string
ConfigDir string // Optional override for profile storage location
}
func NewApp(args []string) *App {
return &App{Args: args}
}
func (a *App) Run() error {
// Handle the internal diagnostic commands first
if len(a.Args) > 1 {
switch a.Args[1] {
case "test-ns":
ok, msg := namespace.VerifyIsolation()
if !ok {
return fmt.Errorf("isolation check failed: %s", msg)
}
fmt.Println("Isolation Verified: OK")
return nil
case "test-args":
return namespace.VerifyArguments(a.Args)
}
}
// Handle subcommands first (profile list, import, configure, delete, stop)
if len(a.Args) > 1 && a.Args[1] == "profile" {
return a.handleProfileCmd()
}
// ...
cfg := &config.Config{}
fs := flag.NewFlagSet("wg-wrap", flag.ExitOnError)
fs.StringVar(&cfg.Profile, "profile", "", "WireGuard profile to use (filename without extension in ~/.config/wg-wrap/profiles/)")
fs.StringVar(&cfg.DNSServer, "dns-server", "", "Override DNS server to use")
args := a.Args[1:]
sepIdx := -1
for i, arg := range args {
if arg == "--" {
sepIdx = i
break
}
}
var flagsToParse []string
if sepIdx != -1 {
flagsToParse = args[:sepIdx]
cfg.Command = args[sepIdx+1:]
} else {
flagsToParse = args
}
err := fs.Parse(flagsToParse)
if err != nil {
return fmt.Errorf("error parsing flags: %w", err)
}
if sepIdx == -1 {
cfg.Command = fs.Args()
}
if len(cfg.Command) == 0 {
return fmt.Errorf("no command provided. use --help for usage")
}
if cfg.Profile == "" {
cfg.Profile = "default"
}
profilesDir := a.ConfigDir
if profilesDir == "" {
profilesDir = config.GetDefaultProfilesDir()
}
fmt.Printf("Profile: %s\n", cfg.Profile)
fmt.Printf("Profiles Directory: %s\n", profilesDir)
fmt.Printf("DNS Server: %s\n", cfg.DNSServer)
fmt.Printf("Command: %v\n", cfg.Command)
return nil
}
func (a *App) handleProfileCmd() error {
if len(a.Args) < 3 {
return fmt.Errorf("usage: wg-wrap profile <list|import|configure|delete|stop> [args]")
}
subCmd := a.Args[2]
switch subCmd {
case "list":
fmt.Println("Listing profiles...")
return fmt.Errorf("profile list not yet implemented")
case "import":
if len(a.Args) < 4 {
return fmt.Errorf("usage: wg-wrap profile import <path>")
}
fmt.Printf("Importing profile from %s...\n", a.Args[3])
return fmt.Errorf("profile import not yet implemented")
case "configure":
if len(a.Args) < 4 {
return fmt.Errorf("usage: wg-wrap profile configure <name>")
}
fmt.Printf("Configuring profile %s...\n", a.Args[3])
return fmt.Errorf("profile configure not yet implemented")
case "delete":
if len(a.Args) < 4 {
return fmt.Errorf("usage: wg-wrap profile delete <name>")
}
fmt.Printf("Deleting profile %s...\n", a.Args[3])
return fmt.Errorf("profile delete not yet implemented")
case "stop":
if len(a.Args) < 4 {
return fmt.Errorf("usage: wg-wrap profile stop <name>")
}
fmt.Printf("Stopping profile %s and unpinning namespace...\n", a.Args[3])
return fmt.Errorf("profile stop not yet implemented")
default:
return fmt.Errorf("unknown profile subcommand: %s", subCmd)
}
}
|