-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
63 lines (49 loc) · 1.5 KB
/
main.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
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
// "database/sql"
"fmt"
"io"
"os"
"github.com/bbland1/goDo/cmd"
"github.com/bbland1/goDo/task"
)
const dbFile = "goDo.db"
func main() {
var exitCode int
db, err := task.InitDatabase(dbFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to initialize database: %v\n", err)
os.Exit(1)
}
defer db.Close()
// register the commands to be used manually
// todo: maybe move to the init once make sure new command logic works for all
cmd.RegisterCommand(cmd.NewHelpCommand(os.Stdout, os.Stderr, &exitCode))
cmd.RegisterCommand(cmd.NewVersionCommand(os.Stdout, os.Stderr, &exitCode))
cmd.RegisterCommand(cmd.NewAddCommand(os.Stdout, os.Stderr, db, &exitCode))
cmd.RegisterCommand(cmd.NewStatusCommand(os.Stdout, os.Stderr, db, &exitCode))
cmd.RegisterCommand(cmd.NewDeleteCommand(os.Stdout, os.Stderr, db, &exitCode))
cmd.RegisterCommand(cmd.NewEditCommand(os.Stdout, os.Stderr, db, &exitCode))
exitCode = runAppLogic(os.Stdout, os.Stderr, os.Args)
os.Exit(exitCode)
}
func runAppLogic(stdout, stderr io.Writer, args []string) int {
if len(args) < 2 {
cmd.DisplayGreeting(stdout)
return 0
}
passedCommand := args[1]
passedArgs := args[2:]
command, exists := cmd.GetCommand(passedCommand)
if !exists {
fmt.Fprintf(stderr, "Unknown command: %s\n", passedCommand)
// cmd.DisplayUserManual(os.Stderr)
return 1
}
if err := command.Init(passedArgs); err != nil {
fmt.Fprintf(stderr, "Error initializing command: %v\n", err)
return 1
}
command.Run()
return 0
}