Add command to find a free port
This commit is contained in:
40
cmd/hoster_cli/commands/freePort.go
Normal file
40
cmd/hoster_cli/commands/freePort.go
Normal file
@ -0,0 +1,40 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
|
||||
"gitea.codeblob.work/pk/hoster/internals/conf"
|
||||
"gitea.codeblob.work/pk/hoster/internals/helpers"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func CmdCheckPort(c *cli.Context) error {
|
||||
sitesAvail := conf.App.MustString("nginx.sitesAvailable")
|
||||
existingProjects, err := helpers.FindHostConfigs(sitesAvail)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
usedPorts := []int{}
|
||||
for _, project := range existingProjects {
|
||||
usedPorts = append(usedPorts, project.InternalPort)
|
||||
}
|
||||
|
||||
port := conf.App.Int("nginx.portRangeStart")
|
||||
freePort := 0
|
||||
for port <= conf.App.Int("nginx.portRangeEnd") {
|
||||
if !slices.Contains(usedPorts, port) && helpers.IsFreePort(port) {
|
||||
freePort = port
|
||||
break
|
||||
}
|
||||
port++
|
||||
}
|
||||
|
||||
if freePort == 0 {
|
||||
return fmt.Errorf("no free port available")
|
||||
}
|
||||
|
||||
fmt.Printf("Free port: %d\n", freePort)
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user