Add CardtraderToken field to config file

This commit is contained in:
The Magician 2024-11-25 20:47:56 +00:00
parent 93c2b55754
commit 429732d076
4 changed files with 42 additions and 27 deletions

View File

@ -1,8 +1,11 @@
{ {
"User": "<your username>", "DatabaseConfig": {
"Passwd": "<your password>", "User": "<your username>",
"Net": "tcp", "Passwd": "<your password>",
"Addr": "127.0.0.1:3306", "Net": "tcp",
"DBName": "<your database name>", "Addr": "127.0.0.1:3306",
"AllowNativePasswords": true "DBName": "<your database name>",
"AllowNativePasswords": true
},
"CardtraderToken": "<your cardtrader token>"
} }

View File

@ -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
}

View File

@ -2,31 +2,12 @@ package database
import ( import (
"database/sql" "database/sql"
"encoding/json"
"log" "log"
"os"
"github.com/go-sql-driver/mysql" "github.com/go-sql-driver/mysql"
) )
func readConfigFromFile(filename string) mysql.Config { func GetDatabaseFromConfig(config mysql.Config) *sql.DB {
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)
db, err := sql.Open("mysql", config.FormatDSN()) db, err := sql.Open("mysql", config.FormatDSN())
if err != nil { if err != nil {

View File

@ -8,6 +8,7 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"sevenkeys/config"
"sevenkeys/database" "sevenkeys/database"
"sevenkeys/delverlens" "sevenkeys/delverlens"
"sevenkeys/logic" "sevenkeys/logic"
@ -37,7 +38,8 @@ func main() {
flag.Parse() flag.Parse()
db := database.GetDatabaseFromConfig("config." + profile + ".json") config := config.ReadConfigFromFile("config." + profile + ".json")
db := database.GetDatabaseFromConfig(config.DatabaseConfig)
if len(flag.Args()) == 0 { if len(flag.Args()) == 0 {
fmt.Fprintln(os.Stderr, "Please specify a subcommand.") fmt.Fprintln(os.Stderr, "Please specify a subcommand.")