package utils

import (
	"net"
	"os"
	"strings"
)

func GetHostWithoutPort(hostIn string) string {
	host, _, err := net.SplitHostPort(hostIn)
	if err != nil {
		host = hostIn
	}
	return host
}

// GetRelativePath returns the relative path back to the project root directory.
// When running from a subdirectory (e.g. during tests), this walks up until
// it finds the "oos-service" directory marker.
func GetRelativePath() string {
	cwd, _ := os.Getwd()
	relativeDir := ""
	parts := strings.Split(cwd, "/")
	for i := len(parts) - 1; i >= 0; i-- {
		if parts[i] == "oos-service" {
			break
		}
		relativeDir = "../" + relativeDir
	}
	return relativeDir
}
