96 lines
1.8 KiB
Go
96 lines
1.8 KiB
Go
// utils.go
|
|
package extviper
|
|
|
|
import (
|
|
"os"
|
|
"path"
|
|
"strings"
|
|
|
|
"github.com/spf13/viper"
|
|
"git.portale-stac.it/go-pkg/utils"
|
|
)
|
|
|
|
const ENV_SSH_CLIENT = "SSH_CLIENT"
|
|
|
|
func GetSshClient() (clientHost string) {
|
|
// viper.BindEnv(ENV_SSH_CLIENT, ENV_SSH_CLIENT)
|
|
// clientSpec := viper.GetString(ENV_SSH_CLIENT)
|
|
|
|
clientSpec, exists := os.LookupEnv(ENV_SSH_CLIENT)
|
|
if exists {
|
|
if len(clientSpec) == 0 {
|
|
clientHost = "localhost"
|
|
} else {
|
|
clientHost = strings.SplitN(clientSpec, " ", 2)[0]
|
|
if clientHost == "127.0.0.1" {
|
|
clientHost = "localhost"
|
|
}
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func ExpandRelativeFilePath(value string, viperFlag string) (fixedPath string, err error) {
|
|
if len(value) != 0 {
|
|
|
|
value, err = utils.ExpandPath(value)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
if !strings.HasPrefix(value, "/") {
|
|
cwd, err := os.Getwd()
|
|
if err == nil {
|
|
absFilepath := path.Join(cwd, value)
|
|
fixedPath = absFilepath
|
|
}
|
|
}
|
|
|
|
if len(fixedPath) == 0 {
|
|
fixedPath = value
|
|
}
|
|
}
|
|
viper.Set(viperFlag, fixedPath)
|
|
return
|
|
}
|
|
|
|
func GetConfigString(param string, defaultValue string) string {
|
|
if viper.IsSet(param) {
|
|
return viper.GetString(param)
|
|
} else {
|
|
return defaultValue
|
|
}
|
|
}
|
|
|
|
func GetConfigInt(param string, defaultValue int) int {
|
|
if viper.IsSet(param) {
|
|
return viper.GetInt(param)
|
|
} else {
|
|
return defaultValue
|
|
}
|
|
}
|
|
|
|
func GetConfigInt64(param string, defaultValue int64) int64 {
|
|
if viper.IsSet(param) {
|
|
return viper.GetInt64(param)
|
|
} else {
|
|
return defaultValue
|
|
}
|
|
}
|
|
|
|
func GetConfigUint(param string, defaultValue uint) uint {
|
|
if viper.IsSet(param) {
|
|
return viper.GetUint(param)
|
|
} else {
|
|
return defaultValue
|
|
}
|
|
}
|
|
|
|
func GetConfigBool(param string, defaultValue bool) bool {
|
|
if viper.IsSet(param) {
|
|
return viper.GetBool(param)
|
|
} else {
|
|
return defaultValue
|
|
}
|
|
}
|