feat(v0.4): add NewLease and NewSessionID constructors
This commit is contained in:
@@ -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(),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user