airshipctl/pkg/util/env.go
Ruslan Aliev 3c53dcabdf Add util functions to set/unset env variables
Proposed util functions can set variable amount of env vars at once,
covered with unit tests and handles possible errors in more convenient
way.

Change-Id: Ibd31f528fba1b10be49d1e8cc5e5f29c6b3d902a
Signed-off-by: Ruslan Aliev <raliev@mirantis.com>
2021-04-22 11:13:49 -05:00

49 lines
1.6 KiB
Go
Executable File

/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package util
import (
"os"
"opendev.org/airship/airshipctl/pkg/log"
)
// EnvVar is an object to store key:value pairs to set/unset environment variables
type EnvVar struct {
Key string
Value string
}
// Setenv is used to set variable number of env variables, if an error occurs it will be printed to logger
func Setenv(envVars ...EnvVar) {
for _, envVar := range envVars {
if err := os.Setenv(envVar.Key, envVar.Value); err != nil {
log.Printf("unable to set '%s' env variable, reason '%s'", envVar.Key, err.Error())
}
}
}
// Unsetenv is used to unset variable number of env variables, if an error occurs it will be printed to logger
func Unsetenv(envVars ...EnvVar) {
for _, envVar := range envVars {
if err := os.Unsetenv(envVar.Key); err != nil {
// Unsetenv on Unix never returns an error [1], so we can't emulate it in unit tests and
// this error message will never be printed
// [1] https://golang.org/src/syscall/env_unix.go
log.Printf("unable to unset '%s' env variable, reason '%s'", envVar.Key, err.Error())
}
}
}