Skip to content

Commit

Permalink
Follow XDG Base Directory Specification; close #67
Browse files Browse the repository at this point in the history
  • Loading branch information
louisroyer committed Sep 24, 2024
1 parent 459d91f commit f3b83e8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
)

require (
github.com/adrg/xdg v0.5.0 // indirect
github.com/bytedance/sonic v1.12.2 // indirect
github.com/bytedance/sonic/loader v0.2.0 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/adrg/xdg v0.5.0 h1:dDaZvhMXatArP1NPHhnfaQUqWBLBsmx1h1HXQdMoFCY=
github.com/adrg/xdg v0.5.0/go.mod h1:dDdY4M4DF9Rjy4kHPeNL+ilVF+p2lK8IdM9/rTSGcI4=
github.com/bytedance/sonic v1.12.2 h1:oaMFuRTpMHYLpCntGca65YWt5ny+wAceDERTkT2L9lg=
github.com/bytedance/sonic v1.12.2/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKzMzT9r/rk=
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
Expand Down
26 changes: 17 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (
"github.com/nextmn/srv6/internal/config"
"github.com/nextmn/srv6/internal/logger"

"github.com/adrg/xdg"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)

func main() {
logger.Init("NextMN-Srv6")
var config_file string
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
defer cancel()
app := &cli.App{
Expand All @@ -35,27 +35,35 @@ func main() {
Name: "config",
Aliases: []string{"c"},
Usage: "Load configuration from `FILE`",
Destination: &config_file,
Required: true,
DefaultText: "not set",
Required: false,
DefaultText: "$XDG_CONFIG_DIRS}/nextmn-srv6/config.yaml",
EnvVars: []string{"CONFIG_FILE"},
},
},
Action: func(c *cli.Context) error {
conf, err := config.ParseConf(config_file)
Action: func(ctx *cli.Context) error {
if ctx.Path("config") == "" {
if xdgPath, err := xdg.SearchConfigFile("nextmn-srv6/config.yaml"); err != nil {
cli.ShowAppHelp(ctx)
logrus.WithError(err).Fatal("No configuration file defined")
} else {
ctx.Set("config", xdgPath)
}
}
conf, err := config.ParseConf(ctx.Path("config"))
if err != nil {
logrus.WithContext(ctx).WithError(err).Fatal("Error loading config, exiting…")
logrus.WithContext(ctx.Context).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 {
if err := app.NewSetup(conf).Run(ctx.Context); err != nil {
logrus.WithError(err).Fatal("Error while running, exiting…")
}
return nil
},
}
if err := app.Run(os.Args); err != nil {
if err := app.RunContext(ctx, os.Args); err != nil {
logrus.Fatal(err)
}
}

0 comments on commit f3b83e8

Please sign in to comment.