feat(v0.4): add NewLease and NewSessionID constructors

This commit is contained in:
2026-04-21 17:02:48 -04:00
parent 305a9d7c23
commit c29985b663
2 changed files with 63 additions and 0 deletions
+24
View File
@@ -89,3 +89,27 @@ func currentTerminal() string {
}
return "unknown"
}
// NewSessionID returns a session identifier in the format
// "<hostname>-<pid>-<YYYYMMDDHHMMSS>" using the given time in UTC.
func NewSessionID(hostname string, pid int, t time.Time) string {
return fmt.Sprintf("%s-%d-%s", hostname, pid, t.UTC().Format("20060102150405"))
}
// NewLease constructs a fresh lease for the current process. startedAt is used
// for both StartedAt and LastHeartbeatAt.
func NewLease(startedAt time.Time, agent, mode string) *Lease {
hostname := currentHostname()
pid := os.Getpid()
return &Lease{
SessionID: NewSessionID(hostname, pid, startedAt),
PID: pid,
Hostname: hostname,
Username: currentUsername(),
Agent: agent,
Mode: mode,
StartedAt: startedAt.UTC().Truncate(time.Second),
LastHeartbeatAt: startedAt.UTC().Truncate(time.Second),
Terminal: currentTerminal(),
}
}