37 lines
704 B
Go
37 lines
704 B
Go
package config
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
type Config struct {
|
|
Token string `json:"token"`
|
|
}
|
|
|
|
func LoadConfiguration(file string, config *Config) error {
|
|
filePath, err := filepath.Abs(file)
|
|
if err != nil {
|
|
fmt.Printf("File repo not found: +%v\n", err)
|
|
return err
|
|
}
|
|
configFile, err := os.Open(filePath)
|
|
// From https://stackoverflow/a/76287159
|
|
defer func() {
|
|
err = errors.Join(err, configFile.Close())
|
|
if err != nil {
|
|
fmt.Printf("File cannot be closed: +%v\n", err)
|
|
}
|
|
}()
|
|
if err != nil {
|
|
fmt.Printf("File cannot be opened: +%v\n", err)
|
|
return err
|
|
}
|
|
json.NewDecoder(configFile).Decode(config)
|
|
fmt.Println("Json loaded")
|
|
return nil
|
|
}
|