blob: d15acd31ce62e45e97540514c94b125113a543e0 [file] [log] [blame]
package discovery
import (
"net/http"
"io/ioutil"
"strings"
"errors"
)
func FindNamespace(host string) (string, error) {
//Attempt to access the host website
resp, err := http.Get(host)
if err == nil {
bytes, err := ioutil.ReadAll(resp.Body)
if err == nil {
//Successfully accessed the website!
body := string(bytes)
//Parse body looking for "v23.namespace.root="
//If found, return number after that
//Else keep looking
if strings.Contains(body, "v23.namespace.root=") {
//formatting return string
namespaces := strings.SplitAfter(body, "v23.namespace.root=")
for i := 1; i < len(namespaces); i += 2 {
namespaceWithJunk := namespaces[i]
namespace := strings.SplitAfter(namespaceWithJunk, "\n")[0]
cleanNamespace := strings.TrimSpace(namespace)
if cleanNamespace != "" {
return cleanNamespace, nil
}
}
}
}
}
//no instance of "v23.namespace.root=" is found
return "", errors.New("No namespace found")
}