mirror of
https://github.com/wailsapp/wails.git
synced 2026-03-14 14:45:49 +01:00
Add checkport tool for checking open ports
This commit is contained in:
parent
763c7708f4
commit
0b9fec3f87
2 changed files with 38 additions and 0 deletions
|
|
@ -44,6 +44,8 @@ func main() {
|
|||
//plugin.NewSubCommandFunction("list", "List plugins", commands.PluginList)
|
||||
plugin.NewSubCommandFunction("init", "Initialise a new plugin", commands.PluginInit)
|
||||
//plugin.NewSubCommandFunction("add", "Add a plugin", commands.PluginAdd)
|
||||
tool := app.NewSubCommand("tool", "Various tools")
|
||||
tool.NewSubCommandFunction("checkport", "Checks if a port is open. Useful for testing if vite is running.", commands.ToolCheckPort)
|
||||
|
||||
app.NewSubCommandFunction("version", "Print the version", commands.Version)
|
||||
|
||||
|
|
|
|||
36
v3/internal/commands/tool_checkport.go
Normal file
36
v3/internal/commands/tool_checkport.go
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ToolCheckPortOptions struct {
|
||||
Host string `name:"h" description:"Host to check" default:"localhost"`
|
||||
Port int `name:"p" description:"Port to check"`
|
||||
}
|
||||
|
||||
func isPortOpen(ip string, port int) bool {
|
||||
timeout := time.Second
|
||||
conn, err := net.DialTimeout("tcp", net.JoinHostPort(ip, fmt.Sprintf("%d", port)), timeout)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// If there's no error, close the connection and return true.
|
||||
if conn != nil {
|
||||
_ = conn.Close()
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func ToolCheckPort(options *ToolCheckPortOptions) error {
|
||||
if options.Port == 0 {
|
||||
return fmt.Errorf("please use the -p flag to specify a port")
|
||||
}
|
||||
if !isPortOpen(options.Host, options.Port) {
|
||||
return fmt.Errorf("port %d is not open on %s", options.Port, options.Host)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue