Skip to content

Commit

Permalink
chore: context improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
moshloop committed Jan 25, 2024
1 parent c4c6274 commit 986996b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
18 changes: 18 additions & 0 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ func (k Context) WithTimeout(timeout time.Duration) (Context, gocontext.CancelFu
}, cancelFunc
}

func (k Context) WithDeadline(deadline time.Time) (Context, gocontext.CancelFunc) {
ctx, cancelFunc := k.Context.WithDeadline(deadline)
return Context{
Context: ctx,
}, cancelFunc
}

// WithAnyValue is a wrapper around WithValue
func (k Context) WithAnyValue(key, val any) Context {
return Context{
Expand All @@ -89,6 +96,16 @@ func (k Context) WithUser(user *models.Person) Context {
}
}

func (k Context) WithoutName() Context {
k.Logger = k.Logger.WithoutName()
return k
}

func (k Context) WithName(name string) Context {
k.Logger = k.Logger.Named(name)
return k
}

func (k Context) User() *models.Person {
v := k.Value("user")
if v == nil {
Expand Down Expand Up @@ -243,6 +260,7 @@ func (k Context) GetNamespace() string {
}
return ""
}

func (k Context) GetName() string {
return k.GetObjectMeta().Name
}
Expand Down
12 changes: 11 additions & 1 deletion context/properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,18 @@ func (k Context) ClearCache() {
propertyCache = cache.New(time.Minute*15, time.Minute*15)
}

type Properties map[string]string

func (p Properties) On(key string) bool {
return p[key] == "true" || p[key] == "off"
}

func (p Properties) Off(key string) bool {
return p[key] == "false" || p[key] == "disabled"
}

// Properties returns a cached map of properties
func (k Context) Properties() map[string]string {
func (k Context) Properties() Properties {
// properties are currently global, but in future we might have context specific properties as well
if val, ok := propertyCache.Get("global"); ok {
return val.(map[string]string)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/asecurityteam/rolling v2.0.4+incompatible
github.com/exaring/otelpgx v0.5.2
github.com/fergusstrange/embedded-postgres v1.25.0
github.com/flanksource/commons v1.20.3
github.com/flanksource/commons v1.21.1
github.com/flanksource/gomplate/v3 v3.20.29
github.com/flanksource/kommons v0.31.4
github.com/flanksource/postq v0.1.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -762,8 +762,8 @@ github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/fergusstrange/embedded-postgres v1.25.0 h1:sa+k2Ycrtz40eCRPOzI7Ry7TtkWXXJ+YRsxpKMDhxK0=
github.com/fergusstrange/embedded-postgres v1.25.0/go.mod h1:t/MLs0h9ukYM6FSt99R7InCHs1nW0ordoVCcnzmpTYw=
github.com/flanksource/commons v1.20.3 h1:8HQvskTl6yghQIEMuLs4RWUtt9NS60MPD/iyG32VrYI=
github.com/flanksource/commons v1.20.3/go.mod h1:bs2nMwaTCpTQGZNtJv8KNqEVyfGjFZTGUDaAbPHB1cw=
github.com/flanksource/commons v1.21.1 h1:vZw21pM95hYd/YLXDdvtH/gb1PUZPMWLpRyk/N7eykw=
github.com/flanksource/commons v1.21.1/go.mod h1:GD5+yGvmYFPIW3WMNN+y1JkeDMJY74e05pQAsRbrvwY=
github.com/flanksource/gomplate/v3 v3.20.4/go.mod h1:27BNWhzzSjDed1z8YShO6W+z6G9oZXuxfNFGd/iGSdc=
github.com/flanksource/gomplate/v3 v3.20.29 h1:hDrNw1JaQk+gmhSCvqng+nebYAt3a5afhf/Vdmr4CTs=
github.com/flanksource/gomplate/v3 v3.20.29/go.mod h1:GKmptFMdr2LbOuqwQZrmo9a/UygyZ0pbXffks8MuYhE=
Expand Down

0 comments on commit 986996b

Please sign in to comment.