-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
223 lines (216 loc) · 5.81 KB
/
main.go
File metadata and controls
223 lines (216 loc) · 5.81 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package main
import (
"buzzer/DB_Worker"
"buzzer/WoL_Worker"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/urfave/cli/v2"
)
const (
version = "v1.2.0"
)
func getDBPath() (string, error) {
configDir, err := os.UserConfigDir()
if err != nil {
return "", fmt.Errorf("could not get user config directory: %w", err)
}
buzzerDir := filepath.Join(configDir, "buzzer")
if err := os.MkdirAll(buzzerDir, 0755); err != nil {
return "", fmt.Errorf("could not create config directory %q: %w", buzzerDir, err)
}
return filepath.Join(buzzerDir, ".machines"), nil
}
func main() {
dbPath, err := getDBPath()
if err != nil {
log.Fatalf("Could not get or create database path: %v", err)
}
db, err := DB_Worker.New(dbPath)
if err != nil {
log.Fatalf("Failed to initialize database: %v", err)
}
defer func(db *DB_Worker.DB) {
err := db.Close()
if err != nil {
log.Fatalf("Failed to close database: %v", err)
}
}(db)
app := &cli.App{
Name: "buzzer",
Usage: "A simple Wake-on-LAN (WoL) command-line tool",
Version: version,
Commands: []*cli.Command{
{
Name: "store",
Aliases: []string{"s"},
Usage: "Saves or stores a MAC address with a memorable alias",
ArgsUsage: "[ALIAS] [MAC_ADDRESS]",
Action: func(c *cli.Context) error {
if c.NArg() < 2 {
return cli.ShowSubcommandHelp(c)
}
alias := c.Args().Get(0)
mac := c.Args().Get(1)
if err := db.StoreMachine(alias, mac); err != nil {
return cli.Exit(fmt.Sprintf("Error storing machine: %v", err), 1)
}
fmt.Println("Machine stored successfully")
return nil
},
},
{
Name: "edit",
Aliases: []string{"e"},
Usage: "Edits an existing entry to assign a new MAC address to an alias",
ArgsUsage: "[ALIAS] [NEW_MAC_ADDRESS]",
Action: func(c *cli.Context) error {
if c.NArg() < 2 {
return cli.ShowSubcommandHelp(c)
}
alias := c.Args().Get(0)
newMAC := c.Args().Get(1)
if err := db.EditMachineDetails(alias, newMAC); err != nil {
return cli.Exit(fmt.Sprintf("Error editing machine: %v", err), 1)
}
fmt.Println("Machine edited successfully")
return nil
},
},
{
Name: "wake",
Aliases: []string{"w"},
Usage: "Wakes a machine using its stored alias",
ArgsUsage: "[ALIAS]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "via",
Usage: "Specify a custom broadcast address (e.g., 192.168.1.255:9)",
},
},
Action: func(c *cli.Context) error {
if c.NArg() < 1 {
return cli.ShowSubcommandHelp(c)
}
alias := c.Args().First()
// Pass the custom address if the --via flag is used
customAddr := c.String("via")
if err := db.WakeWithAlias(alias, customAddr); err != nil {
return cli.Exit(fmt.Sprintf("Error waking %q: %v", alias, err), 1)
}
fmt.Printf("Waking %s ...\n", alias)
return nil
},
},
{
Name: "get",
Aliases: []string{"g"},
Usage: "Gets and displays the MAC address associated with a stored alias",
ArgsUsage: "[ALIAS]",
Action: func(c *cli.Context) error {
if c.NArg() < 1 {
return cli.ShowSubcommandHelp(c)
}
alias := c.Args().First()
mac, err := db.GetStoredMac(alias)
if err != nil {
return cli.Exit(fmt.Sprintf("Error getting stored mac: %v", err), 1)
}
fmt.Printf("%s is tied to: %s\n", alias, mac)
return nil
},
},
{
Name: "broadcast",
Aliases: []string{"b"},
Usage: "Wakes a machine directly via its MAC address (Broadcast)",
ArgsUsage: "[MAC_ADDRESS]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "via",
Usage: "Specify a custom broadcast address (e.g., 192.168.1.255:9)",
},
},
Action: func(c *cli.Context) error {
if c.NArg() < 1 {
return cli.ShowSubcommandHelp(c)
}
mac := c.Args().First()
// Pass the custom address if the --via flag is used
customAddr := c.String("via")
if err := WoL_Worker.SendMagicPacket(mac, customAddr); err != nil {
return cli.Exit(fmt.Sprintf("Error sending magic packet: %v", err), 1)
}
fmt.Printf("Waking %s ...\n", mac)
return nil
},
},
{
Name: "list",
Aliases: []string{"l"},
Usage: "Lists all stored aliases and their corresponding MAC addresses",
Action: func(c *cli.Context) error {
return db.ListAllMachines()
},
},
{
Name: "remove",
Aliases: []string{"r"},
Usage: "Removes an alias and its MAC address from the database",
ArgsUsage: "[ALIAS]",
Action: func(c *cli.Context) error {
if c.NArg() < 1 {
return cli.ShowSubcommandHelp(c)
}
alias := c.Args().First()
return db.DeleteEntry(alias)
},
},
{
Name: "listen",
Usage: "Listens for incoming WoL packets to help with testing",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "port",
Value: 9,
Usage: "Port to listen on",
},
},
Action: func(c *cli.Context) error {
port := c.Int("port")
err := WoL_Worker.Listen(port)
if err != nil && port < 1024 && strings.Contains(err.Error(), "permission denied") {
return cli.Exit(fmt.Sprintf("Permission denied to bind to port %d. Try running with sudo. or changing the port using -port modifier", port), 1)
}
return err
},
},
// For Bash completions only
{
Name: "list-raw",
Hidden: true,
Action: func(c *cli.Context) error {
return db.ListAllMachineAliases()
},
},
// For GoReleaser only
{
Name: "generate-man-page",
Hidden: true,
Action: func(c *cli.Context) error {
man, err := c.App.ToMan()
if err != nil {
return err
}
fmt.Println(man)
return nil
},
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}