-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
105 lines (93 loc) · 2.93 KB
/
Copy pathmain.go
File metadata and controls
105 lines (93 loc) · 2.93 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
// This file is part of Aguaxaca.
// Copyright (C) 2025 Arnaud Berthomier.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or (at
// your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"context"
"flag"
"fmt"
"os"
"git.cypr.io/oz/aguaxaca/app"
"git.cypr.io/oz/aguaxaca/web"
"git.cypr.io/oz/aguaxaca/workers"
"github.com/peterbourgon/ff/v3/ffcli"
)
func main() {
ctx := context.Background()
app := app.NewApp(ctx)
// CLI command: aguaxaca collect
collectCmd := &ffcli.Command{
Name: "collect",
ShortHelp: "Fetch latest water schedules",
Exec: func(context.Context, []string) error {
if err := app.DefaultCollector().Collect(); err != nil {
fmt.Printf("Error collecting schedules: %v\n", err)
os.Exit(2)
}
return nil
},
}
// CLI command: aguaxaca analyze
analyzeCmd := &ffcli.Command{
Name: "analyze",
ShortHelp: "Analyze and extract data from collected images",
Exec: func(context.Context, []string) error {
analyzer := app.NewAnalyzer()
count, err := analyzer.ProcessPendingImports()
if err != nil {
fmt.Printf("Error analyzing images: %v", err)
}
fmt.Printf("Image analysis complete (%d).\n", count)
return nil
},
}
// CLI command: aguaxaca server
serverCmd := &ffcli.Command{
Name: "server",
ShortHelp: "Start web server + async workers",
Exec: func(context.Context, []string) error {
serv := web.NewServer(app)
sched := workers.NewScheduler(app)
return app.Start(serv, sched)
},
}
// root command
rootFlagSet := flag.NewFlagSet("aguaxaca", flag.ExitOnError)
debug := rootFlagSet.Bool("debug", false, "log debug information")
listenAddr := rootFlagSet.String("listen", "localhost:8080", "listen address")
root := &ffcli.Command{
Name: "aguaxaca",
ShortUsage: "aguaxaca [OPTIONS] SUBCOMMAND ...",
FlagSet: rootFlagSet,
Subcommands: []*ffcli.Command{collectCmd, analyzeCmd, serverCmd},
Exec: func(context.Context, []string) error {
// The root command by itself has no use. Show usage help.
return flag.ErrHelp
},
}
if err := root.Parse(os.Args[1:]); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
// Configure App after flags parsing.
if err := app.Init(*debug, *listenAddr); err != nil {
fmt.Fprintf(os.Stderr, "App init error: %v\n", err)
os.Exit(2)
}
if err := root.Run(ctx); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}