From 1cd23cecbe7c47310accc345cb9f33f81de52e8f Mon Sep 17 00:00:00 2001 From: Pawel Bereza Date: Tue, 17 May 2022 13:03:43 +0200 Subject: [PATCH] Host can be chosen by IP --- utils/utils.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/utils/utils.go b/utils/utils.go index 757e5d3..b4b58a3 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -3,6 +3,7 @@ package utils import ( "bufio" "fmt" + "net" "os" "strconv" "strings" @@ -23,7 +24,13 @@ func ChooseFromList(resourcesList []resources.Resource) (resources.Resource, err reader := bufio.NewReader(os.Stdin) fmt.Print("Choose your ship: ") choose, _ := reader.ReadString('\n') - idx, err := strconv.Atoi(strings.TrimSuffix(choose, "\n")) + choose = strings.TrimSuffix(choose, "\n") + + if choosenIP(choose) { + return findResourceByIP(choose, resourcesList) + } + + idx, err := strconv.Atoi(choose) if len(resourcesList)+1 <= idx || idx < 1 || err != nil { return nil, fmt.Errorf("unknown choose %s", choose) } @@ -32,3 +39,17 @@ func ChooseFromList(resourcesList []resources.Resource) (resources.Resource, err return nil, fmt.Errorf("no possible elements to display") } } + +func choosenIP(id string) bool { + ip := net.ParseIP(id) + return ip != nil +} + +func findResourceByIP(ip string, resourcesList []resources.Resource) (resources.Resource, error) { + for _, v := range resourcesList { + if v.ConnectIdentifier(true, false) == ip { + return v, nil + } + } + return nil, fmt.Errorf("could not find resource with specified IP") +}