Archived
1
0
Fork 0
This repository has been archived on 2024-12-18. You can view files and clone it, but cannot push or open issues or pull requests.
restockbot/config.go
Julien Riou 4342f65211
Use array of key/value for Twitter hashtags (#1)
With Go, maps are not ordered the way they are declared. Keys can be read at any
order. When a pattern is too large ("rtx 3060"), when placed first, it can match
a name of another product ("rtx 3060 ti"). When placed second, the good hashtag
is chosen. This commit uses an array of maps because arrays are ordered.

Signed-off-by: Julien Riou <julien@riou.xyz>
2021-02-27 18:34:13 +01:00

53 lines
1.3 KiB
Go

package main
import (
"encoding/json"
"io/ioutil"
"path/filepath"
)
// Config to store JSON configuration
type Config struct {
TwitterConfig `json:"twitter"`
URLs []string `json:"urls"`
IncludeRegex string `json:"include_regex"`
ExcludeRegex string `json:"exclude_regex"`
}
// TwitterConfig to store Twitter API secrets
type TwitterConfig struct {
ConsumerKey string `json:"consumer_key"`
ConsumerSecret string `json:"consumer_secret"`
AccessToken string `json:"access_token"`
AccessTokenSecret string `json:"access_token_secret"`
Hashtags []map[string]string `json:"hashtags"`
}
// NewConfig creates a Config struct
func NewConfig() *Config {
return &Config{}
}
// Read Config from configuration file
func (c *Config) Read(file string) error {
file, err := filepath.Abs(file)
if err != nil {
return err
}
jsonFile, err := ioutil.ReadFile(file)
if err != nil {
return err
}
err = json.Unmarshal(jsonFile, &c)
if err != nil {
return err
}
return nil
}
// HasTwitter returns true when Twitter has been configured
func (c *Config) HasTwitter() bool {
return (c.TwitterConfig.AccessToken != "" && c.TwitterConfig.AccessTokenSecret != "" && c.TwitterConfig.ConsumerKey != "" && c.TwitterConfig.ConsumerSecret != "")
}