Eric Bower
·
17 Jun 24
util.go
1package util
2
3import (
4 "fmt"
5
6 "github.com/charmbracelet/ssh"
7 "github.com/picosh/pico/db"
8)
9
10type ctxUserKey struct{}
11type ctxFeatureFlagKey struct{}
12
13func GetUser(ctx ssh.Context) (*db.User, error) {
14 user, ok := ctx.Value(ctxUserKey{}).(*db.User)
15 if !ok {
16 return user, fmt.Errorf("user not set on `ssh.Context()` for connection")
17 }
18 return user, nil
19}
20
21func SetUser(ctx ssh.Context, user *db.User) {
22 ctx.SetValue(ctxUserKey{}, user)
23}
24
25func GetFeatureFlag(ctx ssh.Context) (*db.FeatureFlag, error) {
26 ff, ok := ctx.Value(ctxFeatureFlagKey{}).(*db.FeatureFlag)
27 if !ok || ff.Name == "" {
28 return ff, fmt.Errorf("feature flag not set on `ssh.Context()` for connection")
29 }
30 return ff, nil
31}
32
33func SetFeatureFlag(ctx ssh.Context, ff *db.FeatureFlag) {
34 ctx.SetValue(ctxFeatureFlagKey{}, ff)
35}