monobank-firefly3-bot/config.go

78 lines
1.8 KiB
Go
Raw Normal View History

2024-03-27 20:12:24 +02:00
package main
2024-03-27 21:19:30 +02:00
import (
"encoding/json"
2024-03-29 23:22:46 +02:00
"io"
2024-03-27 21:19:30 +02:00
"os"
)
type Config struct {
Accounts []ConfigAccount `json:"accounts"`
TransactionTypes []ConfigTransactionTypes `json:"transaction_types"`
}
2024-03-27 20:12:24 +02:00
2024-03-27 21:19:30 +02:00
type ConfigAccount struct {
Name string `json:"name"`
Firefly3Id string `json:"firefly3_id,omitempty"`
MonobankId string `json:"monobank_id,omitempty"`
}
2024-03-27 20:12:24 +02:00
2024-03-27 21:19:30 +02:00
type ConfigTransactionTypes struct {
2024-03-27 21:41:17 +02:00
Names []string `json:"names,omitempty"`
2024-03-27 21:19:30 +02:00
MccCodes []int `json:"mcc_codes,omitempty"`
2024-03-27 21:41:17 +02:00
Firefly3 ConfigTransactionTypeFirefly3 `json:"firefly3,omitempty"`
2024-03-29 22:05:27 +02:00
SumMax int `json:"sum_max,omitempty"`
2024-03-27 21:19:30 +02:00
}
2024-03-27 20:12:24 +02:00
2024-03-27 21:19:30 +02:00
type ConfigTransactionTypeFirefly3 struct {
Type string `json:"type,omitempty"`
Destination string `json:"destination,omitempty"`
Description string `json:"description,omitempty"`
Category string `json:"category,omitempty"`
IsUseDestinationAsSource bool `json:"is_use_destination_as_source,omitempty"`
2024-03-27 21:19:30 +02:00
}
2024-03-27 20:12:24 +02:00
2024-03-27 21:19:30 +02:00
func ReadConfig(path string) (Config, error) {
var config Config
2024-03-27 20:12:24 +02:00
2024-03-27 21:19:30 +02:00
// open file
file, err := os.Open(path)
if err != nil {
return config, err
}
defer file.Close()
2024-03-27 20:12:24 +02:00
2024-03-27 21:19:30 +02:00
// read file
2024-03-29 23:22:46 +02:00
bytes, err := io.ReadAll(file)
2024-03-27 21:19:30 +02:00
if err != nil {
return config, err
}
2024-03-27 20:12:24 +02:00
2024-03-27 21:19:30 +02:00
// read file ot config struct
if err := json.Unmarshal(bytes, &config); err != nil {
return config, err
}
2024-03-27 20:12:24 +02:00
2024-03-27 21:19:30 +02:00
return config, nil
}
2024-03-27 20:12:24 +02:00
2024-03-27 21:19:30 +02:00
func ConfigGetAccountByName(config Config, q string) ConfigAccount {
for _, row := range config.Accounts {
if row.Name == q {
return row
}
}
2024-03-27 20:12:24 +02:00
2024-03-27 21:19:30 +02:00
return ConfigAccount{}
}
2024-03-27 20:12:24 +02:00
2024-03-27 21:19:30 +02:00
func ConfigGetAccountByMonobankId(config Config, q string) ConfigAccount {
for _, row := range config.Accounts {
if row.MonobankId == q {
return row
}
}
2024-03-27 20:12:24 +02:00
2024-03-27 21:19:30 +02:00
return ConfigAccount{}
2024-03-27 20:12:24 +02:00
}