-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.go
More file actions
39 lines (32 loc) · 721 Bytes
/
cli.go
File metadata and controls
39 lines (32 loc) · 721 Bytes
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
package main
import (
"errors"
"fmt"
"github.com/mikarwacki/gator/internal/config"
"github.com/mikarwacki/gator/internal/database"
)
type state struct {
db *database.Queries
cfg *config.Config
}
type command struct {
Name string
Args []string
}
type commands struct {
registeredCommands map[string]func(*state, command) error
}
func (c *commands) register(name string, f func(*state, command) error) {
c.registeredCommands[name] = f
}
func (c *commands) run(s *state, cmd command) error {
if f, ok := c.registeredCommands[cmd.Name]; ok {
err := f(s, cmd)
if err != nil {
fmt.Println("Error running the command")
return err
}
return nil
}
return errors.New("Command is not available")
}