Browse Source

Added some useful flags

main
Unbewohnte 3 years ago
parent
commit
04136ef5c7
  1. 2
      twitch-hooks/.gitignore
  2. 22
      twitch-hooks/config/config.go
  3. 34
      twitch-hooks/main.go

2
twitch-hooks/.gitignore vendored

@ -0,0 +1,2 @@
config.cfg
twitch-hooks

22
twitch-hooks/config/config.go

@ -4,13 +4,14 @@ import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"twitch-hooks/discordhooks"
"twitch-hooks/twitchhooks"
"twitch-hooks/vkhooks"
)
const configFilename string = "config.cfg"
const DefaultConfigFilename string = "config.cfg"
type keys struct {
Twitch twitchhooks.Keys
@ -30,22 +31,23 @@ type Config struct {
Messages messages
}
// Checks if config file exists in the same directory
func ConfigExists() bool {
_, err := os.Stat(configFilename)
// Checks if config file exists
func ConfigExists(configPath string) bool {
_, err := os.Stat(configPath)
if err != nil {
return false
}
return true
}
// Creates a new config file in current directory.
func CreateConfig() error {
// Creates a new config file in specified directory
func CreateConfig(dir string) error {
// create a config file in the same directory
configF, err := os.Create(configFilename)
configF, err := os.Create(filepath.Join(dir, DefaultConfigFilename))
if err != nil {
return fmt.Errorf("could not create a config file: %s", err)
}
defer configF.Close()
// write default config fields
defaults, err := json.MarshalIndent(&Config{}, "", " ")
@ -63,9 +65,9 @@ func CreateConfig() error {
// Opens and reads config file, returns `Config` struct.
// If ReadConfig cannot unmarshal config file - it creates a new one with
// all default fields
func ReadConfig() (*Config, error) {
func ReadConfig(pathToConfig string) (*Config, error) {
// get config`s contents
configContents, err := os.ReadFile(configFilename)
configContents, err := os.ReadFile(pathToConfig)
if err != nil {
return nil, fmt.Errorf("could not read config: %s", err)
}
@ -73,7 +75,7 @@ func ReadConfig() (*Config, error) {
var config Config
err = json.Unmarshal(configContents, &config)
if err != nil {
_ = CreateConfig()
_ = CreateConfig(filepath.Dir(pathToConfig))
return nil, fmt.Errorf("could not unmarshal config: %s\nCreatead a new one", err)
}

34
twitch-hooks/main.go

@ -1,8 +1,10 @@
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"time"
"twitch-hooks/config"
"twitch-hooks/discordhooks"
@ -10,21 +12,31 @@ import (
"twitch-hooks/vkhooks"
)
var logo string = ` _______ _ _ _ _ _
var (
logo string = ` _______ _ _ _ _ _
|__ __| (_) | | | | | | |
| |_ ___| |_ ___| |__ ______| |__ ___ ___ | | _____
| \ \ /\ / / | __/ __| '_ \______| '_ \ / _ \ / _ \| |/ / __|
| |\ V V /| | || (__| | | | | | | | (_) | (_) | <\__ \
|_| \_/\_/ |_|\__\___|_| |_| |_| |_|\___/ \___/|_|\_\___/ by Unbewohnte`
var Config config.Config
Config config.Config
pathToConfig *string = flag.String("config", config.DefaultConfigFilename, "Specifies path to a config in another directory")
delay *uint = flag.Uint("delay", 5000, "Delay in seconds for each check cycle")
)
func init() {
// process the config file
flag.Parse()
if *pathToConfig != config.DefaultConfigFilename {
// config in another place
config.ReadConfig(*pathToConfig)
}
if !config.ConfigExists() {
if !config.ConfigExists(*pathToConfig) {
// there is no existing config file;
// create a new one and exit
err := config.CreateConfig()
err := config.CreateConfig(filepath.Dir(*pathToConfig))
if err != nil {
panic(err)
}
@ -33,7 +45,7 @@ func init() {
os.Exit(0)
}
configContents, err := config.ReadConfig()
configContents, err := config.ReadConfig(*pathToConfig)
if err != nil {
panic(err)
}
@ -55,9 +67,6 @@ func main() {
}
}
var delay = time.Second * 300
fmt.Printf("Delay: %s\n", delay)
// mainloop
for {
// retrieve access token
tokenResp, err := twitchhooks.GetToken(&Config.Keys.Twitch)
@ -76,8 +85,6 @@ func main() {
if is_live || Config.ForceSend {
// live or forced to send -> send alerts
fmt.Println("Live !")
if Config.Keys.Discord.WebhookUrl != "" {
err := discordhooks.Post(Config.Keys.Discord.WebhookUrl, Config.Messages.DiscordMessage)
if err != nil {
@ -92,13 +99,12 @@ func main() {
panic(err)
}
}
// alerted. Now exiting
fmt.Println("Alerts has been sent ! My work is done here...")
os.Exit(0)
}
// sleeping
time.Sleep(delay)
duration, _ := time.ParseDuration(fmt.Sprintf("%ds", *delay))
time.Sleep(duration)
}
}

Loading…
Cancel
Save