Skip to content

Commit

Permalink
Eyal's review fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
gailazar300 committed Apr 2, 2024
1 parent f4a07a4 commit 38badc2
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2289,7 +2289,7 @@ version, err := xscManager.GetVersion()
```
#### Report XSC analytics metrics
#### Add analytics general event
##### Add analytics general event
Sent XSC a new event which contains analytics data, and get multi-scan id back from XSC.
```go
event := services.XscAnalyticsGeneralEvent{
Expand All @@ -2306,7 +2306,7 @@ event := services.XscAnalyticsGeneralEvent{
}}
msi, err := xscManager.AddAnalyticsGeneralEvent(event)
```
#### Update analytics general event
##### Update analytics general event
Sent XSC a finalized analytics metrics event with information matching an existing event's msi.
```go
finalizeEvent := services.XscAnalyticsGeneralEventFinalize{
Expand All @@ -2321,7 +2321,7 @@ finalizeEvent := services.XscAnalyticsGeneralEventFinalize{
err := xscManager.UpdateAnalyticsGeneralEvent(finalizeEvent)
```
#### Get analytics general event
##### Get analytics general event
Get a general event from XSC matching the provided msi.
```go
event, err := xscManager.GetAnalyticsGeneralEvent(msi)
Expand Down
20 changes: 12 additions & 8 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,25 +603,29 @@ func Pointer[K any](val K) *K {
return &val
}

func SetEnvWithResetCallback(key, value string) func() {
func SetEnvWithResetCallback(key, value string) (func() error, error) {
oldValue, exist := os.LookupEnv(key)
errMsg := "failed %s %s as environment variable. Cause: %s"

if err := os.Setenv(key, value); err != nil {
log.Debug(fmt.Sprintf(errMsg, "setting", key, err.Error()))
return func() {}
return func() error { return nil }, err
}

if exist {
return func() {
if err := os.Setenv(key, oldValue); err != nil {
return func() error {
err := os.Setenv(key, oldValue)
if err != nil {
log.Debug(fmt.Sprintf(errMsg, "setting", key, err.Error()))
}
}
return err
}, nil
}
return func() {
if err := os.Unsetenv(key); err != nil {
return func() error {
err := os.Unsetenv(key)
if err != nil {
log.Debug(fmt.Sprintf(errMsg, "unsetting", key, err.Error()))
}
}
return err
}, nil
}
5 changes: 3 additions & 2 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,10 @@ func TestSetEnvWithResetCallback(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.init()
resetCallback := SetEnvWithResetCallback(tt.args.key, tt.args.value)
resetCallback, err := SetEnvWithResetCallback(tt.args.key, tt.args.value)
assert.NoError(t, err)
assert.Equal(t, tt.args.value, os.Getenv(tt.args.key))
resetCallback()
assert.NoError(t, resetCallback())
tt.finish()
})
}
Expand Down
File renamed without changes.

0 comments on commit 38badc2

Please sign in to comment.