Skip to content
This repository has been archived by the owner on Nov 23, 2024. It is now read-only.

Host can be chosen by IP #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"bufio"
"fmt"
"net"
"os"
"strconv"
"strings"
Expand All @@ -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)
}
Expand All @@ -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")
}