-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
89 lines (76 loc) · 1.81 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
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
package main
import (
"context"
"fmt"
"log"
"os"
"time"
"github.com/urfave/cli/v2"
"github.com/shemanaev/inpxer/internal/config"
"github.com/shemanaev/inpxer/internal/indexer"
"github.com/shemanaev/inpxer/internal/server"
)
var (
version = "dev"
date = "unknown"
)
type key int
const (
contextConfig key = iota
)
func main() {
app := &cli.App{
Version: version,
Commands: []*cli.Command{
{
Name: "serve",
Aliases: []string{"s"},
Usage: "start server",
Action: serveAction,
Before: loadConfig,
},
{
Name: "import",
Aliases: []string{"i"},
Usage: "import .inpx file",
Action: importAction,
Before: loadConfig,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "keep-deleted",
Usage: "Keep records marked as \"Deleted\" in inp",
},
&cli.BoolFlag{
Name: "partial",
Usage: "Only add new records, never delete",
},
},
},
},
}
if buildDate, err := time.Parse(time.RFC3339, date); err == nil {
server.BuildDate = buildDate
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func importAction(ctx *cli.Context) error {
cfg := ctx.Context.Value(contextConfig).(*config.MyConfig)
fmt.Println("Starting import from:", ctx.Args().First())
return indexer.Run(cfg, ctx.Args().First(), ctx.Bool("keep-deleted"), ctx.Bool("partial"))
}
func serveAction(ctx *cli.Context) error {
cfg := ctx.Context.Value(contextConfig).(*config.MyConfig)
isDevMode := version == "dev"
fmt.Printf("Starting web server on: http://%s\n", cfg.Listen)
return server.Run(cfg, isDevMode, version)
}
func loadConfig(ctx *cli.Context) error {
cfg, err := config.Load()
if err != nil {
return fmt.Errorf("error loading config %v", err)
}
ctx.Context = context.WithValue(ctx.Context, contextConfig, cfg)
return nil
}