-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
37 lines (32 loc) · 844 Bytes
/
utils.go
File metadata and controls
37 lines (32 loc) · 844 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package main
import (
"crypto/rand"
"encoding/hex"
"fmt"
"os"
"path/filepath"
"time"
)
func defaultSessionPath() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("getting home directory: %w", err)
}
dir := filepath.Join(home, ".ajent", "sessions")
if err := os.MkdirAll(dir, 0755); err != nil {
return "", fmt.Errorf("creating sessions directory: %w", err)
}
cwd, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("getting working directory: %w", err)
}
base := filepath.Base(cwd)
var b [4]byte
if _, err := rand.Read(b[:]); err != nil {
return "", fmt.Errorf("generating random bytes: %w", err)
}
hash := hex.EncodeToString(b[:])
ts := time.Now().Format("20060102-150405")
name := fmt.Sprintf("%s-%s-%s.hjl", base, ts, hash)
return filepath.Join(dir, name), nil
}