@@ -9,86 +9,100 @@ import (
99 "context"
1010 "os"
1111 "os/signal"
12+ "runtime/debug"
1213 "syscall"
1314
15+ "github.com/nextmn/cli-xdg"
1416 "github.com/nextmn/json-api/healthcheck"
1517 "github.com/nextmn/logrus-formatter/logger"
1618
1719 "github.com/nextmn/srv6/internal/app"
1820 "github.com/nextmn/srv6/internal/config"
1921
20- "github.com/adrg/xdg"
2122 "github.com/sirupsen/logrus"
22- "github.com/urfave/cli/v2 "
23+ "github.com/urfave/cli/v3 "
2324)
2425
2526func main () {
26- logger .Init ("NextMN-Srv6 " )
27+ logger .Init ("NextMN-SRv6 " )
2728 ctx , cancel := signal .NotifyContext (context .Background (), syscall .SIGTERM , syscall .SIGINT )
2829 defer cancel ()
29- app := & cli.App {
30- Name : "NextMN-SRv6" ,
31- Usage : "Experimental implementation of SRv6 SIDs for MUP" ,
32- EnableBashCompletion : true ,
33- Authors : []* cli.Author {
34- {Name : "Louis Royer" },
30+ version := "Unknown version"
31+ if info , ok := debug .ReadBuildInfo (); ok {
32+ version = info .Main .Version
33+ }
34+ app := & cli.Command {
35+ Name : "srv6" ,
36+ Usage : "NextMN-SRv6 - Experimental implementation of SRv6 SIDs for MUP" ,
37+ EnableShellCompletion : true ,
38+ Authors : []any {
39+ "Louis Royer" ,
3540 },
36- HideHelpCommand : true ,
41+ Version : version ,
3742 Flags : []cli.Flag {
3843 & cli.StringFlag {
39- Name : "config" ,
40- Aliases : []string {"c" },
41- Usage : "Load configuration from `FILE`" ,
42- Required : false ,
44+ Name : "config" ,
45+ TakesFile : true ,
46+ Aliases : []string {"c" },
47+ Usage : "load configuration from `FILE`" ,
48+ // XXX: https://github.com/urfave/cli/issues/2244
49+ // Required: true,
4350 DefaultText : "${XDG_CONFIG_DIRS}/nextmn-srv6/config.yaml" ,
44- EnvVars : []string {"CONFIG_FILE" },
51+ Sources : cli .NewValueSourceChain (
52+ cli .EnvVar ("CONFIG_FILE" ),
53+ clixdg .ConfigFile ("nextmn-srv6/config.yaml" ),
54+ ),
4555 },
4656 },
47- Before : func (ctx * cli.Context ) error {
48- if ctx .Path ("config" ) == "" {
49- if xdgPath , err := xdg .SearchConfigFile ("nextmn-srv6/config.yaml" ); err != nil {
50- cli .ShowAppHelp (ctx )
51- logrus .WithError (err ).Fatal ("No configuration file defined" )
52- } else {
53- ctx .Set ("config" , xdgPath )
54- }
55- }
56- return nil
57- },
58- Action : func (ctx * cli.Context ) error {
59- conf , err := config .ParseConf (ctx .Path ("config" ))
60- if err != nil {
61- logrus .WithContext (ctx .Context ).WithError (err ).Fatal ("Error loading config, exiting…" )
62- }
63- if conf .Logger != nil {
64- logrus .SetLevel (conf .Logger .Level )
65- }
66- if err := app .NewSetup (conf ).Run (ctx .Context ); err != nil {
67- logrus .WithError (err ).Fatal ("Error while running, exiting…" )
68- }
69- return nil
70- },
57+ DefaultCommand : "run" ,
7158 Commands : []* cli.Command {
59+ {
60+ Name : "run" ,
61+ Usage : "Runs the SRv6 node" ,
62+ Action : func (ctx context.Context , cmd * cli.Command ) error {
63+ // XXX: https://github.com/urfave/cli/issues/2244
64+ if cmd .String ("config" ) == "" {
65+ logrus .Fatal ("Required flag \" config\" not set" )
66+ }
67+
68+ conf , err := config .ParseConf (cmd .String ("config" ))
69+ if err != nil {
70+ logrus .WithContext (ctx ).WithError (err ).Fatal ("Error loading config, exiting…" )
71+ }
72+ if conf .Logger != nil {
73+ logrus .SetLevel (conf .Logger .Level )
74+ }
75+ if err := app .NewSetup (conf ).Run (ctx ); err != nil {
76+ logrus .WithError (err ).Fatal ("Error while running, exiting…" )
77+ }
78+ return nil
79+ },
80+ },
7281 {
7382 Name : "healthcheck" ,
74- Usage : "check status of the node" ,
75- Action : func (ctx * cli.Context ) error {
76- conf , err := config .ParseConf (ctx .Path ("config" ))
83+ Usage : "Checks status of the node" ,
84+ Action : func (ctx context.Context , cmd * cli.Command ) error {
85+ // XXX: https://github.com/urfave/cli/issues/2244
86+ if cmd .String ("config" ) == "" {
87+ logrus .Fatal ("Required flag \" config\" not set" )
88+ }
89+
90+ conf , err := config .ParseConf (cmd .String ("config" ))
7791 if err != nil {
78- logrus .WithContext (ctx . Context ).WithError (err ).Fatal ("Error loading config, exiting…" )
92+ logrus .WithContext (ctx ).WithError (err ).Fatal ("Error loading config, exiting…" )
7993 }
8094 if conf .Logger != nil {
8195 logrus .SetLevel (conf .Logger .Level )
8296 }
83- if err := healthcheck .NewHealthcheck (* conf .Control .Uri .JoinPath ("status" ), "go-github-nextmn-srv6" ).Run (ctx . Context ); err != nil {
97+ if err := healthcheck .NewHealthcheck (* conf .Control .Uri .JoinPath ("status" ), "go-github-nextmn-srv6" ).Run (ctx ); err != nil {
8498 os .Exit (1 )
8599 }
86100 return nil
87101 },
88102 },
89103 },
90104 }
91- if err := app .RunContext (ctx , os .Args ); err != nil {
92- logrus .Fatal (err )
105+ if err := app .Run (ctx , os .Args ); err != nil {
106+ logrus .WithError (err ). Fatal ( "Fatal error while running the application" )
93107 }
94108}
0 commit comments