-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
101 lines (87 loc) · 2.48 KB
/
main.go
File metadata and controls
101 lines (87 loc) · 2.48 KB
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
package main
import (
"flag"
"fmt"
"os"
"github.com/ituoga/wgmeshbuilder/pkg/crypto"
"github.com/ituoga/wgmeshbuilder/pkg/mesh"
)
func main() {
var (
stateFile = flag.String("state", "mesh-state.json", "Path to mesh state file")
addNode = flag.String("add", "", "Add node (format: hostname:ip:ssh_host[:ssh_port])")
removeNode = flag.String("remove", "", "Remove node by hostname")
list = flag.Bool("list", false, "List all nodes")
deploy = flag.Bool("deploy", false, "Deploy configuration to all nodes")
init = flag.Bool("init", false, "Initialize new mesh")
encrypt = flag.Bool("encrypt", false, "Encrypt state file with password (asks for password)")
)
flag.Parse()
// Handle encryption flag
if *encrypt {
var password string
var err error
if *init {
// For init, ask for password twice
password, err = crypto.ReadPasswordTwice("Enter encryption password: ")
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read password: %v\n", err)
os.Exit(1)
}
} else {
// For other operations, ask once
password, err = crypto.ReadPassword("Enter encryption password: ")
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read password: %v\n", err)
os.Exit(1)
}
}
mesh.SetEncryptionPassword(password)
}
if *init {
if err := mesh.Initialize(*stateFile); err != nil {
fmt.Fprintf(os.Stderr, "Failed to initialize mesh: %v\n", err)
os.Exit(1)
}
fmt.Println("Mesh initialized successfully")
return
}
m, err := mesh.Load(*stateFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to load mesh state: %v\n", err)
os.Exit(1)
}
switch {
case *addNode != "":
if err := m.AddNode(*addNode); err != nil {
fmt.Fprintf(os.Stderr, "Failed to add node: %v\n", err)
os.Exit(1)
}
if err := m.Save(*stateFile); err != nil {
fmt.Fprintf(os.Stderr, "Failed to save state: %v\n", err)
os.Exit(1)
}
fmt.Printf("Node added successfully\n")
case *removeNode != "":
if err := m.RemoveNode(*removeNode); err != nil {
fmt.Fprintf(os.Stderr, "Failed to remove node: %v\n", err)
os.Exit(1)
}
if err := m.Save(*stateFile); err != nil {
fmt.Fprintf(os.Stderr, "Failed to save state: %v\n", err)
os.Exit(1)
}
fmt.Printf("Node removed successfully\n")
case *list:
m.List()
case *deploy:
if err := m.Deploy(); err != nil {
fmt.Fprintf(os.Stderr, "Failed to deploy: %v\n", err)
os.Exit(1)
}
fmt.Println("Deployment completed successfully")
default:
flag.Usage()
os.Exit(1)
}
}