From 151b0d2dbb8d876c606f0b0d6358a705915fd128 Mon Sep 17 00:00:00 2001 From: Quentin Lemaire Date: Wed, 8 Dec 2021 23:18:53 +0100 Subject: [PATCH] fix(notification): Trigger notification if set in config file --- Taskfile.yml | 25 +++++++------- app.go | 12 +++++-- internal/config/config.go | 49 ++++++++++++++++++++++++++++ item.go | 10 ++++++ pkg/notifier/notification_windows.go | 4 +++ 5 files changed, 86 insertions(+), 14 deletions(-) create mode 100644 internal/config/config.go diff --git a/Taskfile.yml b/Taskfile.yml index 6ec0877..7ee2336 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -3,13 +3,21 @@ version: '3' vars: TEMP: sh: mktemp -d + VERSION: + sh: git describe --tags --exact-match 2>/dev/null || git describe --tags 2>/dev/null || echo "v0.0.0-$(git rev-parse --short HEAD)" tasks: default: + - task: clean - task: lint - task: tests - task: build + clean: + desc: Clean output folder + cmds: + - rm -rf out/* + build: cmds: - go mod tidy @@ -38,15 +46,11 @@ tasks: - rsrc_windows_amd64.syso - internal/icon/icon_unix.go - internal/icon/icon_windows.go - vars: - VERSION: - sh: git describe --tags --exact-match 2>/dev/null || git describe --tags 2>/dev/null || echo "v0.0.0-$(git rev-parse --short HEAD)" build:darwin: - deps: [ mod, generate ] + deps: [ generate ] cmds: - go build -ldflags="-s -w -X 'main.twitchClientID=${TWITCH_CLIENT_ID}' -X 'main.twitchClientSecret=${TWITCH_SECRET_ID}'" -tags production -o "{{.TEMP}}/twitch_clip_${GOOS}_${GOARCH}" . - - echo "Binary built at {{.TEMP}}/twitch_clip_${GOOS}_${GOARCH}" env: GOOS: darwin GOARCH: amd64 @@ -56,15 +60,15 @@ tasks: deps: [ build:darwin ] cmds: - go run hack/macapp.go -assets "{{.TEMP}}" -bin "twitch_clip_${GOOS}_${GOARCH}" -icon ./assets/icon1080.png -identifier com.skynewz.twitchclip -name "Twitch Clip" -o ./out/ + - zip -r ./out/twitch_clip_${GOOS}_${GOARCH}_{{ .VERSION }}.zip "./out/Twitch Clip.app" README.md LICENSE && rm -r "./out/Twitch Clip.app" env: GOOS: darwin GOARCH: amd64 build:windows: - deps: [ mod, generate ] + deps: [ generate ] cmds: - - go build -ldflags="-s -w -X 'main.twitchClientID=${TWITCH_CLIENT_ID}' -X 'main.twitchClientSecret=${TWITCH_SECRET_ID}' -H=windowsgui" -tags production -o "out/twitch_clip_${GOOS}_${GOARCH}.exe" . - - echo "Binary built at out/twitch_clip_${GOOS}_${GOARCH}.exe" + - go build -ldflags="-s -w -X 'main.twitchClientID=${TWITCH_CLIENT_ID}' -X 'main.twitchClientSecret=${TWITCH_SECRET_ID}' -H=windowsgui" -tags production -o "out/twitch_clip_${GOOS}_${GOARCH}_{{ .VERSION }}.exe" . env: GOOS: windows GOARCH: amd64 @@ -73,10 +77,9 @@ tasks: CXX: x86_64-w64-mingw32-g++ build:linux: - deps: [ mod, generate ] + deps: [ generate ] cmds: - - go build -ldflags="-s -w -X 'main.twitchClientID=${TWITCH_CLIENT_ID}' -X 'main.twitchClientSecret=${TWITCH_SECRET_ID}'" -tags production -o "out/twitch_clip_${GOOS}_${GOARCH}.exe" . - - echo "Binary built at out/twitch_clip_${GOOS}_${GOARCH}.exe" + - go build -ldflags="-s -w -X 'main.twitchClientID=${TWITCH_CLIENT_ID}' -X 'main.twitchClientSecret=${TWITCH_SECRET_ID}'" -tags production -o "out/twitch_clip_${GOOS}_${GOARCH}_{{ .VERSION }}.exe" . env: GOOS: linux GOARCH: amd64 diff --git a/app.go b/app.go index 8319581..6dad4e6 100644 --- a/app.go +++ b/app.go @@ -7,6 +7,7 @@ package main import ( "context" "fmt" + "github.com/SkYNewZ/twitch-clip/internal/config" "os" "sync" "time" @@ -59,6 +60,8 @@ type Application struct { // Each string in this chan will be send to system clipboard ClipboardListener chan string + + config *config.Config } // New creates a new Application @@ -95,6 +98,7 @@ func New() *Application { NotificationCallbackCh: notificationCh, State: make(map[string]*Item), ClipboardListener: make(chan string, 1), + config: config.Parse(), } } @@ -359,9 +363,11 @@ func (a *Application) NewItem(ctx context.Context, s *twitch.Stream) *Item { // Start routine click for this Item go item.Click(ctx) - // New item appear, so notify - if err := a.Notifier.Notify(username, item.Game, item.UserLogin); err != nil { - log.Errorf("fail to notify for [%s]: %s", item.UserLogin, err) + // New item appear, so notify if configured + if item.ShouldNotify() { + if err := a.Notifier.Notify(username, item.Game, item.UserLogin); err != nil { + log.Errorf("fail to notify for [%s]: %s", item.UserLogin, err) + } } return item diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..1c5da4b --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,49 @@ +package config + +import ( + log "github.com/sirupsen/logrus" + "gopkg.in/yaml.v3" + "os" + "path/filepath" +) + +const ( + configFileName = "config.yaml" + configDirectoryName = "Twitch Clip" +) + +type Config struct { + Notifications []string `json:"notifications,omitempty" yaml:"notifications,flow"` +} + +func defaultConfig() *Config { + return &Config{} +} + +// Parse load config stored on disk +func Parse() *Config { + var config = defaultConfig() + + dir, err := os.UserConfigDir() + if err != nil { + log.Errorf("cannot load config: %v", err) + return config + } + + var file = filepath.Join(dir, configDirectoryName, configFileName) + log.Printf("using config file: %s", file) + + data, err := os.ReadFile(file) + if err != nil { + log.Errorf("cannot load config: %v", err) + return config + } + + if err := yaml.Unmarshal(data, config); err != nil { + log.Errorf("cannot load config: %v", err) + return config + } + + log.Printf("%d notification(s) has been configured", len(config.Notifications)) + return config +} diff --git a/item.go b/item.go index 3964974..21bcb91 100644 --- a/item.go +++ b/item.go @@ -136,3 +136,13 @@ func (i *Item) SetIcon() { // set icon i.Item.SetIcon(img) } + +// ShouldNotify send true if current item is configured to send notifications +func (i *Item) ShouldNotify() bool { + for _, user := range i.Application.config.Notifications { + if i.UserLogin == strings.ToLower(user) { + return true + } + } + return false +} diff --git a/pkg/notifier/notification_windows.go b/pkg/notifier/notification_windows.go index 76a321e..e042396 100644 --- a/pkg/notifier/notification_windows.go +++ b/pkg/notifier/notification_windows.go @@ -1,7 +1,10 @@ package notifier import ( + "errors" "fmt" + "github.com/phayes/freeport" + "net/http" "net/url" log "github.com/sirupsen/logrus" @@ -47,6 +50,7 @@ func (s *service) makeNotificationURL(streamer string) string { func (s *service) startServer() { var port int once.Do(func() { + var err error port, err = freeport.GetFreePort() if err != nil { log.Errorf("notification service: cannot find free port, notifications click will not works: %s", err)