headplane/cmd/hp_agent/hp_agent.go

62 lines
1.1 KiB
Go
Raw Permalink Normal View History

package main
import (
2025-08-20 13:58:19 -04:00
"bufio"
"context"
"fmt"
2025-08-20 13:58:19 -04:00
"os"
"strings"
2025-06-16 11:38:13 -04:00
"github.com/tale/headplane/internal/config"
"github.com/tale/headplane/internal/tsnet"
"github.com/tale/headplane/internal/util"
)
type Register struct {
Type string
ID string
}
func main() {
2025-04-08 14:51:28 -04:00
log := util.GetLogger()
cfg, err := config.Load()
if err != nil {
2025-04-08 14:51:28 -04:00
log.Fatal("Failed to load config: %s", err)
}
2025-04-08 14:51:28 -04:00
log.SetDebug(cfg.Debug)
agent := tsnet.NewAgent(cfg)
defer agent.Shutdown()
2025-08-20 13:58:19 -04:00
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Bytes()
directive := strings.TrimSpace(string(line))
log.Debug("Received directive: %s", directive)
switch directive {
case "START":
agent.Connect()
fmt.Printf("READY %s\n", agent.ID)
case "SHUTDOWN":
agent.Shutdown()
os.Exit(0)
case "PING":
fmt.Printf("PONG %s\n", agent.ID)
case "REFRESH":
err := agent.DispatchHostInfo(context.Background())
if err != nil {
fmt.Printf("ERROR %s\n", err)
}
}
}
2025-08-20 13:58:19 -04:00
if err := scanner.Err(); err != nil {
fmt.Printf("ERROR %s\n", err)
os.Exit(1)
}
}