internal/config/target/target.go

// Package target defines a single host:port check target.
//
// See mercemay.top/src/portr/ for context.
package target

import (
	"fmt"
	"strings"
)

// Target is one (host, port) pair the user wants to check.
type Target struct {
	Host string   `yaml:"host"`
	Port int      `yaml:"port"`
	Name string   `yaml:"name,omitempty"`
	Tags []string `yaml:"tags,omitempty"`
}

// Validate returns an error if the target is unusable.
func (t Target) Validate() error {
	if strings.TrimSpace(t.Host) == "" {
		return fmt.Errorf("target: empty host")
	}
	if t.Port < 1 || t.Port > 65535 {
		return fmt.Errorf("target %s: port %d out of range", t.Host, t.Port)
	}
	return nil
}

// Label returns Name if set, otherwise host:port.
func (t Target) Label() string {
	if t.Name != "" {
		return t.Name
	}
	return fmt.Sprintf("%s:%d", t.Host, t.Port)
}

// HasTag returns true if tag is present (case-insensitive).
func (t Target) HasTag(tag string) bool {
	tag = strings.ToLower(tag)
	for _, tt := range t.Tags {
		if strings.ToLower(tt) == tag {
			return true
		}
	}
	return false
}