-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
108 lines (100 loc) · 3.04 KB
/
main.go
File metadata and controls
108 lines (100 loc) · 3.04 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
106
107
108
// Copyright Louis Royer and the NextMN contributors. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.
// SPDX-License-Identifier: MIT
package main
import (
"context"
"os"
"os/signal"
"runtime/debug"
"syscall"
"github.com/nextmn/cli-xdg"
"github.com/nextmn/json-api/healthcheck"
"github.com/nextmn/logrus-formatter/logger"
"github.com/nextmn/srv6-ctrl/internal/app"
"github.com/nextmn/srv6-ctrl/internal/config"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v3"
)
func main() {
logger.Init("Nextm-SRv6-ctrl")
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
defer cancel()
version := "Unknown version"
if info, ok := debug.ReadBuildInfo(); ok {
version = info.Main.Version
}
app := &cli.Command{
Name: "srv6-ctrl",
Usage: "NextMN-SRv6-ctrl - Controller for NextMN-SRv6",
EnableShellCompletion: true,
Authors: []any{
"Louis Royer",
},
Version: version,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
TakesFile: true,
Aliases: []string{"c"},
Usage: "load configuration from `FILE`",
// XXX: https://github.com/urfave/cli/issues/2244
// Required: true,
DefaultText: "${XDG_CONFIG_DIRS}/nextmn-srv6-ctrl/config.yaml",
Sources: cli.NewValueSourceChain(
cli.EnvVar("CONFIG_FILE"),
clixdg.ConfigFile("nextmn-srv6-ctrl/config.yaml"),
),
},
},
DefaultCommand: "run",
Commands: []*cli.Command{
{
Name: "run",
Usage: "Runs the SRv6 controller",
Action: func(ctx context.Context, cmd *cli.Command) error {
// XXX: https://github.com/urfave/cli/issues/2244
if cmd.String("config") == "" {
logrus.Fatal("Required flag \"config\" not set")
}
conf, err := config.ParseConf(cmd.String("config"))
if err != nil {
logrus.WithContext(ctx).WithError(err).Fatal("Error loading config, exiting…")
}
if conf.Logger != nil {
logrus.SetLevel(conf.Logger.Level)
}
if err := app.NewSetup(conf).Run(ctx); err != nil {
logrus.WithError(err).Fatal("Error while running, exiting…")
}
return nil
},
},
{
Name: "healthcheck",
Usage: "Checks status of the node",
Action: func(ctx context.Context, cmd *cli.Command) error {
// XXX: https://github.com/urfave/cli/issues/2244
if cmd.String("config") == "" {
logrus.Fatal("Required flag \"config\" not set")
}
conf, err := config.ParseConf(cmd.String("config"))
if err != nil {
logrus.WithContext(ctx).WithError(err).Fatal("Error loading config, exiting…")
}
if conf.Logger != nil {
logrus.SetLevel(conf.Logger.Level)
}
if err := healthcheck.NewHealthcheck(*conf.Control.Uri.JoinPath("status"), "go-github-nextmn-srv6-ctrl").Run(ctx); err != nil {
os.Exit(1)
}
return nil
},
},
},
}
if err := app.Run(ctx, os.Args); err != nil {
logrus.WithError(err).Fatal("Fatal error while running the application")
}
}