-
Notifications
You must be signed in to change notification settings - Fork 507
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
services/horizon: Add deprecation warning for using command-line flags (
#5051) * Add deprecated warning * Add deprecated warning tests * add test for env vars * Dynamically print deprecated message * Update flags.go * Update parameters_test.go * Update parameters_test.go * Update parameters_test.go * Update parameters_test.go * Shift environment.go to horizon package * Move environment.go to test dir * Update flags_test.go * Update flags_test.go * Update integration.go * Nit changes - 1 * Update CHANGELOG.md * Nit changes - 2 * Update flags_test.go
- Loading branch information
1 parent
006b2cf
commit e50ba18
Showing
9 changed files
with
243 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package test | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/stellar/go/support/errors" | ||
) | ||
|
||
type EnvironmentManager struct { | ||
oldEnvironment, newEnvironment map[string]string | ||
} | ||
|
||
func NewEnvironmentManager() *EnvironmentManager { | ||
env := &EnvironmentManager{} | ||
env.oldEnvironment = make(map[string]string) | ||
env.newEnvironment = make(map[string]string) | ||
return env | ||
} | ||
|
||
func (envManager *EnvironmentManager) InitializeEnvironmentVariables(environmentVars map[string]string) error { | ||
var env strings.Builder | ||
for key, value := range environmentVars { | ||
env.WriteString(fmt.Sprintf("%s=%s ", key, value)) | ||
} | ||
|
||
// prepare env | ||
for key, value := range environmentVars { | ||
innerErr := envManager.Add(key, value) | ||
if innerErr != nil { | ||
return errors.Wrap(innerErr, fmt.Sprintf( | ||
"failed to set envvar (%s=%s)", key, value)) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// Add sets a new environment variable, saving the original value (if any). | ||
func (envManager *EnvironmentManager) Add(key, value string) error { | ||
// If someone pushes an environmental variable more than once, we don't want | ||
// to lose the *original* value, so we're being careful here. | ||
if _, ok := envManager.newEnvironment[key]; !ok { | ||
if oldValue, ok := os.LookupEnv(key); ok { | ||
envManager.oldEnvironment[key] = oldValue | ||
} | ||
} | ||
|
||
envManager.newEnvironment[key] = value | ||
return os.Setenv(key, value) | ||
} | ||
|
||
// Restore restores the environment prior to any modifications. | ||
// | ||
// You should probably use this alongside `defer` to ensure the global | ||
// environment isn't modified for longer than you intend. | ||
func (envManager *EnvironmentManager) Restore() { | ||
for key := range envManager.newEnvironment { | ||
if oldValue, ok := envManager.oldEnvironment[key]; ok { | ||
os.Setenv(key, oldValue) | ||
} else { | ||
os.Unsetenv(key) | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.