-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyggdrasil.go
50 lines (42 loc) · 967 Bytes
/
yggdrasil.go
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
/**
* This file is a part of the poni project and is licensed under the MIT license.
* See LICENSE.md for details.
*
* yggdrasil.go
* Contains the code that handles the command line arguments
*
* @created 2023-08-23
*/
package main
import (
"errors"
"fmt"
)
// Commands
// Contains all the commands.
var Commands []Command
// Command
// Represents a command and it's handler.
type Command struct {
Name string
Handler func([]string)
}
func registerCommand(name string, handler func([]string)) {
Commands = append(Commands, Command{Name: name, Handler: handler})
}
func findHandler(name string) (func([]string), error) {
for _, command := range Commands {
if command.Name == name {
return command.Handler, nil
}
}
return nil, errors.New(fmt.Sprintf("Command %s not found", name))
}
func handleCommand(name string, args []string) error {
handler, err := findHandler(name)
if err != nil {
return err
}
handler(args)
return nil
}