Created
April 25, 2022 01:39
-
-
Save binwiederhier/b925ee20822257f55ec04271e40a1437 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module bla | |
go 1.18 | |
require github.com/urfave/cli/v2 v2.4.8 | |
require ( | |
github.com/BurntSushi/toml v1.1.0 // indirect | |
github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect | |
github.com/russross/blackfriday/v2 v2.1.0 // indirect | |
gopkg.in/yaml.v2 v2.4.0 // indirect | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
github.com/BurntSushi/toml v1.1.0 h1:ksErzDEI1khOiGPgpwuI7x2ebx/uXQNw7xJpn9Eq1+I= | |
github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= | |
github.com/cpuguy83/go-md2man/v2 v2.0.1 h1:r/myEWzV9lfsM1tFLgDyu0atFtJ1fXn261LKYj/3DxU= | |
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= | |
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= | |
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= | |
github.com/urfave/cli/v2 v2.4.8 h1:9HuvvddU3oEJr1tJlwUVVsk3snVWMuKSpyAO+SzTNuI= | |
github.com/urfave/cli/v2 v2.4.8/go.mod h1:oDzoM7pVwz6wHn5ogWgFUU1s4VJayeQS+aEZDqXIEJs= | |
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | |
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | |
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= | |
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/urfave/cli/v2" | |
"github.com/urfave/cli/v2/altsrc" | |
"log" | |
"os" | |
"time" | |
) | |
func main() { | |
c := &cli.App{ | |
Name: "ntfy", | |
Usage: "Simple pub-sub notification service", | |
UsageText: "ntfy [OPTION..]", | |
HideVersion: true, | |
UseShortOptionHandling: true, | |
Commands: []*cli.Command{ | |
cmdServe, | |
}, | |
} | |
log.Fatalln(c.Run(os.Args)) | |
} | |
var flagsServe = []cli.Flag{ | |
&cli.StringFlag{Name: "config", Aliases: []string{"c"}, EnvVars: []string{"NTFY_CONFIG_FILE"}, Value: "/etc/ntfy/server.yml", DefaultText: "/etc/ntfy/server.yml", Usage: "config file"}, | |
altsrc.NewDurationFlag(&cli.DurationFlag{Name: "keepalive-interval", Aliases: []string{"k"}, EnvVars: []string{"NTFY_KEEPALIVE_INTERVAL"}, Value: 45 * time.Second, Usage: "interval of keepalive messages"}), | |
} | |
var cmdServe = &cli.Command{ | |
Name: "serve", | |
Usage: "Run the ntfy server", | |
UsageText: "ntfy serve [OPTIONS..]", | |
Action: execServe, | |
Flags: flagsServe, | |
Before: initConfigFileInputSource("config", flagsServe), | |
} | |
func execServe(c *cli.Context) error { | |
keepaliveInterval := c.Duration("keepalive-interval") | |
log.Printf("keepalive %#v", keepaliveInterval) | |
return nil | |
} | |
// initConfigFileInputSource is like altsrc.InitInputSourceWithContext and altsrc.NewYamlSourceFromFlagFunc, but checks | |
// if the config flag is exists and only loads it if it does. If the flag is set and the file exists, it fails. | |
func initConfigFileInputSource(configFlag string, flags []cli.Flag) cli.BeforeFunc { | |
return func(context *cli.Context) error { | |
configFile := context.String(configFlag) | |
if context.IsSet(configFlag) && !FileExists(configFile) { | |
return fmt.Errorf("config file %s does not exist", configFile) | |
} else if !context.IsSet(configFlag) && !FileExists(configFile) { | |
return nil | |
} | |
inputSource, err := altsrc.NewYamlSourceFromFile(configFile) | |
if err != nil { | |
return err | |
} | |
return altsrc.ApplyInputSourceValues(context, inputSource, flags) | |
} | |
} | |
// FileExists checks if a file exists, and returns true if it does | |
func FileExists(filename string) bool { | |
stat, _ := os.Stat(filename) | |
return stat != nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
``` | |
$ go run main.go serve --config=server.yml | |
2022/04/24 21:38:54 keepalive 0 | |
2022/04/24 21:38:54 <nil> | |
exit status 1 | |
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# empty file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment