go: make sure flags are properly checked
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
leo 2023-05-04 21:28:30 +02:00
parent 44025ccd36
commit 746eb82f67
Signed by: wanderer
SSH Key Fingerprint: SHA256:Dp8+iwKHSlrMEHzE3bJnPng70I7LEsa3IJXRH/U+idQ
3 changed files with 32 additions and 10 deletions

16
app/settings/helper.go Normal file

@ -0,0 +1,16 @@
package settings
import "flag"
// as per https://stackoverflow.com/a/54747682.
func isFlagPassed(name string) bool {
found := false
flag.Visit(func(f *flag.Flag) {
if f.Name == name {
found = true
}
})
return found
}

@ -45,20 +45,26 @@ func (s *Settings) Consolidate(conf *config.Config, host *string, port *int, dev
overrideMsg := "overriding '%s' based on a flag: %+v"
if isFlagPassed("host") {
if h := *host; h != "unset" && h != conf.Host {
log.Debugf(overrideMsg, "host", h)
s.SetHost(h)
}
}
if p := *port; p != 0 && p > 0 && p < 65536 && p != conf.Port {
if isFlagPassed("port") {
if p := *port; p > 0 && p < 65536 && p != conf.Port {
log.Debugf(overrideMsg, "port", p)
s.SetPort(p)
}
}
if isFlagPassed("devel") {
if d := *devel; d != conf.DevelMode {
log.Debugf(overrideMsg, "develMode", d)
s.SetIsDevel(d)
}
}
s.SetVersion(version)
}

2
run.go

@ -52,7 +52,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.`
var (
host = flag.String("host", "unset", "host address to listen on")
port = flag.Int("port", 0, "TCP port to listen on")
port = flag.Int("port", -1, "TCP port to listen on")
configFlag = flag.String("config", "config.dhall", "Default path of the config file")
devel = flag.Bool("devel", false, "Run the application in dev mode, connect to a local browser-sync instance for hot-reloading")
license = flag.Bool("license", false, "Print licensing information and exit")