mirror of
https://github.com/yunginnanet/HellPot
synced 2024-06-29 17:20:50 +00:00
Tidy up: stop using globals
This commit is contained in:
parent
989927b975
commit
baa2e784e9
@ -15,8 +15,6 @@ import (
|
|||||||
|
|
||||||
// generic vars
|
// generic vars
|
||||||
var (
|
var (
|
||||||
f *os.File
|
|
||||||
err error
|
|
||||||
noColorForce = false
|
noColorForce = false
|
||||||
customconfig = false
|
customconfig = false
|
||||||
home string
|
home string
|
||||||
@ -39,34 +37,36 @@ func init() {
|
|||||||
snek = viper.New()
|
snek = viper.New()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func windowsConfig() {
|
||||||
|
newconfig := "hellpot-config"
|
||||||
|
snek.SetConfigName(newconfig)
|
||||||
|
if err := snek.MergeInConfig(); err == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := snek.SafeWriteConfigAs(newconfig + ".toml"); err != nil {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func writeConfig() {
|
func writeConfig() {
|
||||||
//goland:noinspection GoBoolExpressions
|
//goland:noinspection GoBoolExpressions
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
newconfig := "hellpot-config"
|
windowsConfig()
|
||||||
snek.SetConfigName(newconfig)
|
|
||||||
if err = snek.MergeInConfig(); err != nil {
|
|
||||||
if err = snek.SafeWriteConfigAs(newconfig + ".toml"); err != nil {
|
|
||||||
fmt.Println(err.Error())
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := os.Stat(prefConfigLocation); os.IsNotExist(err) {
|
if _, err := os.Stat(prefConfigLocation); os.IsNotExist(err) {
|
||||||
if err = os.MkdirAll(prefConfigLocation, 0o750); err != nil {
|
if err = os.MkdirAll(prefConfigLocation, 0o750); err != nil {
|
||||||
println("error writing new config: " + err.Error())
|
println("error writing new config: " + err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Filename = prefConfigLocation + "/" + "config.toml"
|
||||||
newconfig := prefConfigLocation + "/" + "config.toml"
|
if err := snek.SafeWriteConfigAs(Filename); err != nil {
|
||||||
if err = snek.SafeWriteConfigAs(newconfig); err != nil {
|
|
||||||
fmt.Println("Failed to write new configuration file: " + err.Error())
|
fmt.Println("Failed to write new configuration file: " + err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
Filename = newconfig
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init will initialize our toml configuration engine and define our default configuration values which can be written to a new configuration file if desired
|
// Init will initialize our toml configuration engine and define our default configuration values which can be written to a new configuration file if desired
|
||||||
@ -87,7 +87,7 @@ func Init() {
|
|||||||
snek.AddConfigPath(loc)
|
snek.AddConfigPath(loc)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = snek.MergeInConfig(); err != nil {
|
if err := snek.MergeInConfig(); err != nil {
|
||||||
println("Error reading configuration file: " + err.Error())
|
println("Error reading configuration file: " + err.Error())
|
||||||
println("Writing new configuration file...")
|
println("Writing new configuration file...")
|
||||||
writeConfig()
|
writeConfig()
|
||||||
@ -112,7 +112,8 @@ func getConfigPaths() (paths []string) {
|
|||||||
|
|
||||||
func loadCustomConfig(path string) {
|
func loadCustomConfig(path string) {
|
||||||
/* #nosec */
|
/* #nosec */
|
||||||
if f, err = os.Open(path); err != nil {
|
cf, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
println("Error opening specified config file: " + path)
|
println("Error opening specified config file: " + path)
|
||||||
println(err.Error())
|
println(err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
@ -125,13 +126,12 @@ func loadCustomConfig(path string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
defer func(f *os.File) {
|
defer func(f *os.File) {
|
||||||
fcerr := f.Close()
|
if fcerr := f.Close(); fcerr != nil {
|
||||||
if fcerr != nil {
|
|
||||||
fmt.Println("failed to close file handler for config file: ", fcerr.Error())
|
fmt.Println("failed to close file handler for config file: ", fcerr.Error())
|
||||||
}
|
}
|
||||||
}(f)
|
}(cf)
|
||||||
|
|
||||||
buf, err1 := io.ReadAll(f)
|
buf, err1 := io.ReadAll(cf)
|
||||||
err2 := snek.ReadConfig(bytes.NewBuffer(buf))
|
err2 := snek.ReadConfig(bytes.NewBuffer(buf))
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
var err error
|
||||||
if home, err = os.UserHomeDir(); err != nil {
|
if home, err = os.UserHomeDir(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@ -66,7 +67,7 @@ func setDefaults() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if GenConfig {
|
if GenConfig {
|
||||||
if err = snek.SafeWriteConfigAs("./config.toml"); err != nil {
|
if err := snek.SafeWriteConfigAs("./config.toml"); err != nil {
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user