Moved config module to utils package
This commit is contained in:
parent
2d6d9c3959
commit
5888a7821b
4
main.go
4
main.go
|
@ -9,10 +9,12 @@ import (
|
|||
|
||||
"github.com/oschwald/geoip2-golang"
|
||||
"github.com/thoj/go-ircevent"
|
||||
"irc_bot/utils"
|
||||
)
|
||||
|
||||
func main() {
|
||||
config, err := LoadConfig("config.json")
|
||||
// Load configuration
|
||||
config, err := utils.LoadConfig("config.json")
|
||||
if err != nil {
|
||||
log.Fatalf("Error loading config: %v", err)
|
||||
}
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
package main
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
|
@ -15,18 +16,18 @@ type Config struct {
|
|||
GeoIPASN string `json:"geoip_asn"`
|
||||
}
|
||||
|
||||
func LoadConfig(filePath string) (*Config, error) {
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
func LoadConfig(file string) (Config, error) {
|
||||
var config Config
|
||||
|
||||
config := &Config{}
|
||||
decoder := json.NewDecoder(file)
|
||||
err = decoder.Decode(config)
|
||||
configFile, err := os.Open(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return config, fmt.Errorf("error opening config file: %v", err)
|
||||
}
|
||||
defer configFile.Close()
|
||||
|
||||
err = json.NewDecoder(configFile).Decode(&config)
|
||||
if err != nil {
|
||||
return config, fmt.Errorf("error decoding config file: %v", err)
|
||||
}
|
||||
|
||||
return config, nil
|
Loading…
Reference in New Issue