feat: handle logging from the agent

This commit is contained in:
Aarnav Tale
2025-08-20 13:58:19 -04:00
parent bcd87453bd
commit 8fc657f86a
10 changed files with 63 additions and 235 deletions
+33 -10
View File
@@ -1,11 +1,13 @@
package main
import (
"bufio"
"context"
"fmt"
"os"
"strings"
_ "github.com/joho/godotenv/autoload"
"github.com/tale/headplane/internal/config"
"github.com/tale/headplane/internal/hpagent"
"github.com/tale/headplane/internal/tsnet"
"github.com/tale/headplane/internal/util"
)
@@ -24,15 +26,36 @@ func main() {
log.SetDebug(cfg.Debug)
agent := tsnet.NewAgent(cfg)
agent.Connect()
defer agent.Shutdown()
log.Msg(&Register{
Type: "register",
ID: agent.ID,
})
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Bytes()
directive := strings.TrimSpace(string(line))
log.Debug("Received directive: %s", directive)
fmt.Println("READY")
hpagent.FollowMaster(agent)
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)
}
}
}
if err := scanner.Err(); err != nil {
fmt.Printf("ERROR %s\n", err)
os.Exit(1)
}
}