Skip to content

Commit

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

require (
github.com/adrg/xdg v0.5.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/rogpeppe/go-internal v1.10.0 // 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/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.5 h1:ZtcqGrnekaHpVLArFSe4HK5DoKx1T0rq2DwVB0alcyc=
Expand Down
28 changes: 18 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (
"github.com/nextmn/upf/internal/config"
"github.com/nextmn/upf/internal/logger"

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

func main() {
logger.Init("NextMN-UPF")
var config_file string
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
defer cancel()
app := &cli.App{
Expand All @@ -31,31 +31,39 @@ func main() {
{Name: "Louis Royer"},
},
Flags: []cli.Flag{
&cli.StringFlag{
&cli.PathFlag{
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-upf/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-upf/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.String("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 85253fc

Please sign in to comment.