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" "encoding/json"
"fmt" "fmt"
"os" "os"
"path/filepath"
"strings" "strings"
"twitch-hooks/discordhooks" "twitch-hooks/discordhooks"
"twitch-hooks/twitchhooks" "twitch-hooks/twitchhooks"
"twitch-hooks/vkhooks" "twitch-hooks/vkhooks"
) )
const configFilename string = "config.cfg" const DefaultConfigFilename string = "config.cfg"
type keys struct { type keys struct {
Twitch twitchhooks.Keys Twitch twitchhooks.Keys
@ -30,22 +31,23 @@ type Config struct {
Messages messages Messages messages
} }
// Checks if config file exists in the same directory // Checks if config file exists
func ConfigExists() bool { func ConfigExists(configPath string) bool {
_, err := os.Stat(configFilename) _, err := os.Stat(configPath)
if err != nil { if err != nil {
return false return false
} }
return true return true
} }
// Creates a new config file in current directory. // Creates a new config file in specified directory
func CreateConfig() error { func CreateConfig(dir string) error {
// create a config file in the same directory // create a config file in the same directory
configF, err := os.Create(configFilename) configF, err := os.Create(filepath.Join(dir, DefaultConfigFilename))
if err != nil { if err != nil {
return fmt.Errorf("could not create a config file: %s", err) return fmt.Errorf("could not create a config file: %s", err)
} }
defer configF.Close()
// write default config fields // write default config fields
defaults, err := json.MarshalIndent(&Config{}, "", " ") defaults, err := json.MarshalIndent(&Config{}, "", " ")
@ -63,9 +65,9 @@ func CreateConfig() error {
// Opens and reads config file, returns `Config` struct. // Opens and reads config file, returns `Config` struct.
// If ReadConfig cannot unmarshal config file - it creates a new one with // If ReadConfig cannot unmarshal config file - it creates a new one with
// all default fields // all default fields
func ReadConfig() (*Config, error) { func ReadConfig(pathToConfig string) (*Config, error) {
// get config`s contents // get config`s contents
configContents, err := os.ReadFile(configFilename) configContents, err := os.ReadFile(pathToConfig)
if err != nil { if err != nil {
return nil, fmt.Errorf("could not read config: %s", err) return nil, fmt.Errorf("could not read config: %s", err)
} }
@ -73,7 +75,7 @@ func ReadConfig() (*Config, error) {
var config Config var config Config
err = json.Unmarshal(configContents, &config) err = json.Unmarshal(configContents, &config)
if err != nil { if err != nil {
_ = CreateConfig() _ = CreateConfig(filepath.Dir(pathToConfig))
return nil, fmt.Errorf("could not unmarshal config: %s\nCreatead a new one", err) 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 package main
import ( import (
"flag"
"fmt" "fmt"
"os" "os"
"path/filepath"
"time" "time"
"twitch-hooks/config" "twitch-hooks/config"
"twitch-hooks/discordhooks" "twitch-hooks/discordhooks"
@ -10,21 +12,31 @@ import (
"twitch-hooks/vkhooks" "twitch-hooks/vkhooks"
) )
var logo string = ` _______ _ _ _ _ _ var (
logo string = ` _______ _ _ _ _ _
|__ __| (_) | | | | | | | |__ __| (_) | | | | | | |
| |_ ___| |_ ___| |__ ______| |__ ___ ___ | | _____ | |_ ___| |_ ___| |__ ______| |__ ___ ___ | | _____
| \ \ /\ / / | __/ __| '_ \______| '_ \ / _ \ / _ \| |/ / __| | \ \ /\ / / | __/ __| '_ \______| '_ \ / _ \ / _ \| |/ / __|
| |\ V V /| | || (__| | | | | | | | (_) | (_) | <\__ \ | |\ V V /| | || (__| | | | | | | | (_) | (_) | <\__ \
|_| \_/\_/ |_|\__\___|_| |_| |_| |_|\___/ \___/|_|\_\___/ by Unbewohnte` |_| \_/\_/ |_|\__\___|_| |_| |_| |_|\___/ \___/|_|\_\___/ 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() { func init() {
// process the config file // 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; // there is no existing config file;
// create a new one and exit // create a new one and exit
err := config.CreateConfig() err := config.CreateConfig(filepath.Dir(*pathToConfig))
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -33,7 +45,7 @@ func init() {
os.Exit(0) os.Exit(0)
} }
configContents, err := config.ReadConfig() configContents, err := config.ReadConfig(*pathToConfig)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -55,9 +67,6 @@ func main() {
} }
} }
var delay = time.Second * 300
fmt.Printf("Delay: %s\n", delay)
// mainloop
for { for {
// retrieve access token // retrieve access token
tokenResp, err := twitchhooks.GetToken(&Config.Keys.Twitch) tokenResp, err := twitchhooks.GetToken(&Config.Keys.Twitch)
@ -76,8 +85,6 @@ func main() {
if is_live || Config.ForceSend { if is_live || Config.ForceSend {
// live or forced to send -> send alerts // live or forced to send -> send alerts
fmt.Println("Live !")
if Config.Keys.Discord.WebhookUrl != "" { if Config.Keys.Discord.WebhookUrl != "" {
err := discordhooks.Post(Config.Keys.Discord.WebhookUrl, Config.Messages.DiscordMessage) err := discordhooks.Post(Config.Keys.Discord.WebhookUrl, Config.Messages.DiscordMessage)
if err != nil { if err != nil {
@ -92,13 +99,12 @@ func main() {
panic(err) panic(err)
} }
} }
// alerted. Now exiting // alerted. Now exiting
fmt.Println("Alerts has been sent ! My work is done here...") fmt.Println("Alerts has been sent ! My work is done here...")
os.Exit(0) os.Exit(0)
} }
duration, _ := time.ParseDuration(fmt.Sprintf("%ds", *delay))
// sleeping time.Sleep(duration)
time.Sleep(delay)
} }
} }

Loading…
Cancel
Save