go: fix port default,flag handling
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
leo 2023-05-23 16:37:33 +02:00
parent 11b28e3d39
commit ff68a7dbe3
Signed by: wanderer
SSH Key Fingerprint: SHA256:Dp8+iwKHSlrMEHzE3bJnPng70I7LEsa3IJXRH/U+idQ
2 changed files with 13 additions and 5 deletions

@ -70,8 +70,12 @@ func (s *Settings) Consolidate(conf *config.Config, host *string, port *int, dev
log.Debug("starting to consolidate settings") log.Debug("starting to consolidate settings")
log.Debug("parsing config values") log.Debug("parsing config values")
if p := conf.Port; p > 0 && p < 65536 {
log.Warnf("port '%d', outside of bounds, setting a default of %d/tcp", p, 3000)
s.SetPort(3000)
}
s.SetHost(conf.Host) s.SetHost(conf.Host)
s.SetPort(conf.Port)
s.SetAppPath(conf.AppPath) s.SetAppPath(conf.AppPath)
s.SetIsLive(conf.LiveMode) s.SetIsLive(conf.LiveMode)
s.SetIsDevel(conf.DevelMode) s.SetIsDevel(conf.DevelMode)
@ -120,9 +124,13 @@ func (s *Settings) Consolidate(conf *config.Config, host *string, port *int, dev
} }
if isFlagPassed("port") { if isFlagPassed("port") {
if p := *port; p > 0 && p < 65536 && p != conf.Port { if p := *port; p > 0 && p < 65536 {
log.Debugf(overrideMsg, "port", p) if p != conf.Port {
s.SetPort(p) log.Debugf(overrideMsg, "port", p)
s.SetPort(p)
}
} else {
log.Warnf("flag-supplied port '%d' outside of bounds, ignoring", p)
} }
} }

2
run.go

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