From 429732d076718ae6b0649d038f411cf625ee9e1e Mon Sep 17 00:00:00 2001 From: The Magician Date: Mon, 25 Nov 2024 20:47:56 +0000 Subject: [PATCH] Add CardtraderToken field to config file --- sevenkeys/config.example.json | 15 +++++++++------ sevenkeys/config/config.go | 29 +++++++++++++++++++++++++++++ sevenkeys/database/get_database.go | 21 +-------------------- sevenkeys/main.go | 4 +++- 4 files changed, 42 insertions(+), 27 deletions(-) create mode 100644 sevenkeys/config/config.go diff --git a/sevenkeys/config.example.json b/sevenkeys/config.example.json index a846ff6..a6f1874 100644 --- a/sevenkeys/config.example.json +++ b/sevenkeys/config.example.json @@ -1,8 +1,11 @@ { - "User": "", - "Passwd": "", - "Net": "tcp", - "Addr": "127.0.0.1:3306", - "DBName": "", - "AllowNativePasswords": true + "DatabaseConfig": { + "User": "", + "Passwd": "", + "Net": "tcp", + "Addr": "127.0.0.1:3306", + "DBName": "", + "AllowNativePasswords": true + }, + "CardtraderToken": "" } diff --git a/sevenkeys/config/config.go b/sevenkeys/config/config.go new file mode 100644 index 0000000..d792edc --- /dev/null +++ b/sevenkeys/config/config.go @@ -0,0 +1,29 @@ +package config + +import ( + "encoding/json" + "log" + "os" + + "github.com/go-sql-driver/mysql" +) + +type SevenkeysConfig struct { + DatabaseConfig mysql.Config `json:"DatabaseConfig"` + CardtraderToken string `json:"CardtraderToken"` +} + +func ReadConfigFromFile(filename string) SevenkeysConfig { + bytes, err := os.ReadFile(filename) + if err != nil { + log.Fatal(err) + } + + var config SevenkeysConfig + err = json.Unmarshal(bytes, &config) + if err != nil { + log.Fatal(err) + } + + return config +} diff --git a/sevenkeys/database/get_database.go b/sevenkeys/database/get_database.go index 6808ae9..06bc2c4 100644 --- a/sevenkeys/database/get_database.go +++ b/sevenkeys/database/get_database.go @@ -2,31 +2,12 @@ package database import ( "database/sql" - "encoding/json" "log" - "os" "github.com/go-sql-driver/mysql" ) -func readConfigFromFile(filename string) mysql.Config { - bytes, err := os.ReadFile(filename) - if err != nil { - log.Fatal(err) - } - - var config mysql.Config - err = json.Unmarshal(bytes, &config) - if err != nil { - log.Fatal(err) - } - - return config -} - -func GetDatabaseFromConfig(filename string) *sql.DB { - config := readConfigFromFile(filename) - +func GetDatabaseFromConfig(config mysql.Config) *sql.DB { db, err := sql.Open("mysql", config.FormatDSN()) if err != nil { diff --git a/sevenkeys/main.go b/sevenkeys/main.go index 27b22d3..d820ca7 100644 --- a/sevenkeys/main.go +++ b/sevenkeys/main.go @@ -8,6 +8,7 @@ import ( "io/ioutil" "log" "os" + "sevenkeys/config" "sevenkeys/database" "sevenkeys/delverlens" "sevenkeys/logic" @@ -37,7 +38,8 @@ func main() { flag.Parse() - db := database.GetDatabaseFromConfig("config." + profile + ".json") + config := config.ReadConfigFromFile("config." + profile + ".json") + db := database.GetDatabaseFromConfig(config.DatabaseConfig) if len(flag.Args()) == 0 { fmt.Fprintln(os.Stderr, "Please specify a subcommand.")