From 4c43ad47a47e2ac773125e242fb504f345af9bcf Mon Sep 17 00:00:00 2001 From: svenwiltink Date: Wed, 29 Jun 2022 23:03:53 +0200 Subject: [PATCH] Ci (#79) CI all the things. Thanks again to packagecloud --- .github/workflows/build.yml | 59 +++++++++++++++++++ .gitignore | 2 + .travis.yml | 44 -------------- Makefile | 50 ---------------- README.md | 4 +- deb-build.sh | 4 +- dist/config.json.example | 2 +- pkg/bot/bot.go | 12 ++-- pkg/bot/commands.go | 24 +++++++- pkg/bot/config.go | 3 +- .../messageprovider/mattermost/mattermost.go | 19 +++--- pkg/bot/messageprovider/slack/slack.go | 6 +- pkg/bot/messageprovider/terminal/terminal.go | 4 +- pkg/music/dataprovider/nts/nts.go | 3 +- pkg/music/dataprovider/youtube/youtube.go | 17 ++---- pkg/music/player/musicplayer.go | 12 ++-- pkg/music/provider/mpv/mpv.go | 3 +- pkg/music/queue_test.go | 4 +- 18 files changed, 127 insertions(+), 145 deletions(-) create mode 100644 .github/workflows/build.yml delete mode 100644 .travis.yml delete mode 100644 Makefile diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..ef63c22 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,59 @@ +--- +on: [push, pull_request] +name: Build +jobs: + build: + name: build + strategy: + matrix: + go-version: [1.18.3] + os: [ubuntu-latest] + runs-on: ${{ matrix.os }} + steps: + - uses: actions/setup-go@v3 + with: + go-version: ${{ matrix.go-version }} + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - run: go test ./... + - run: go build ./cmd/go-musicbot + + - name: setup fpm + run: sudo gem install fpm + + - name: git status + run: git status + + - name: make package + run: ./deb-build.sh + + - name: Upload Artifact + uses: actions/upload-artifact@v2 + with: + name: package + path: ./out/packages/* + retention-days: 5 + + - name: Setup package cloud + if: github.event_name == 'release' && github.event.action == 'created' + run: sudo gem install package_cloud + - name: Push to package_cloud + if: github.event_name == 'release' && github.event.action == 'created' + env: + PACKAGECLOUD_TOKEN: ${{ secrets.PACKAGECLOUD_TOKEN }} + run: package_cloud push svenwiltink/go-musicbot/ubuntu/bionic ./out/packages/* + golangci: + name: lint + strategy: + matrix: + go-version: [1.18.x] + runs-on: ubuntu-latest + steps: + - uses: actions/setup-go@v3 + with: + go-version: ${{ matrix.go-version }} + - uses: actions/checkout@v3 + - name: golangci-lint + uses: golangci/golangci-lint-action@v3 diff --git a/.gitignore b/.gitignore index 668446f..ce0cc74 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ .idea +.vscode vendor config.json whitelist.txt out pkg_root +go-musicbot diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 80bb17c..0000000 --- a/.travis.yml +++ /dev/null @@ -1,44 +0,0 @@ -language: go -go: -- 1.13.x -env: -- GO111MODULE=on - -install: true - -jobs: - include: - - stage: formatting - script: - - make fmt - - script: - - make deps - - make lint - - stage: build - script: - - make deps - - make build - - mkdir out/archives - - ls -1 out/binaries | xargs -L 1 -I{} tar -cf out/archives/{}.tar --transform='flags=r;s|.*\MusicBot.*\(\..*\)|MusicBot\1|' --transform='flags=r;s|dist/||' out/binaries/{} dist/config.json.example - - gem install --no-ri --no-rdoc fpm - - make deb - deploy: - - provider: releases - api_key: - secure: hmEPa5FCvu16GTt9kA1djudi2+gOrvpTarwc1yEruCDR76bn1dKa+KfaunyQL6L78zQPD2NTKI3J8OiEyoO68uOnsuAi+JKeX5a1UsnFvJaUWgmJ6l+NTgUPc++HUl86VeBqnzxuP7g2jUimhAA/LB5d7TCREuFW1Nm2ITqEpFhbRPOImniOZ2B0cxxorwdaMvwQ0QPCVVMTIuUnFzDPw1Xf0RNHa4s3U9JPHAh1N+NHMwXxDS8zaoplSjzyiiHxa9TBE4VdUoSkBMx0VtOK5X6ynItsPlBfG9TW7QuKBtUlW0sHhVkFZCYwU72Lnmlj6SgN2sOl2x1jAHwOT+tBHVgy6TtCwlLKP4H5EH4ptlTSkwBISZvrmEtmR2TW0Gt2y7jjmw2jeVCUS0kBFq+fNnFBzKKQJvwVztHnV3KxMRT1ubW02e6zyHd9wBAEk9VL/Gjgy7+N/o4wt8voB1Cp1qcYpneHDXOuf/MADUUmOyZXF1thtBmF5poZWJaSTXO9sdwdHZyqwfe58mJBK9adJ6Fu63O9opD4ZaG0IpMdlj6tJapUu57zRSa4BRelS8zbPtmWw3xEByF3n9KT4QV/tB8TxfAVt3OF/D/HFSsEzSjYRj7+Z7+lZUfIbRxBPBKwo54HjQj5wyR6FsuqJ7LH2OnMuPFtkWfCfJM2ZHvS8tY= - file_glob: true - file: - - out/archives/* - on: - tags: true - skip_cleanup: true - - provider: packagecloud - local-dir: out/packages - username: svenwiltink - repository: go-musicbot - dist: ubuntu/bionic - token: - secure: cSNkBddUy+CoJHhFDBeIt/3w0AeqPx0kJ2Z6xrnCcbRY7KjPloHCw7WxOl4EqWw4+zyCL1M2S4EYplKD2xL9SsIM7qQ8FgX2QiA50fVs0QuxzVGw7yML3bU6P/GmPjJ36hNcReGb7Ev388RWZD7141jcowD/AtwiBARynexn/JjHKhcS8G1T819n/jjISt8YLTexJuXNBxDVUAixvG6xZP0SgcmYAE6MPDXYoBJi5jklMVqP3+uhaB6RMJlu4/Fcl0X8ZJ1uhZuAFvKHjAuY5B7aaDaouyP1YU1IQ9QE4Tez0yU0u2oYVR7BR8AOM0sWdujkE/oGlTyvL/myFy93s86hdplBnY3RNEv54apaANmxttaetpYFSS0VluSOIm8FRfaYW1DLBwr5JQaOMTxoDOUG8RghuvvsTN2Dc/gmj32yZ1swSBbevlXFbHmzwcwIYcthRMlKkfZA1BpI07n7bt6NFW75USP6rYWAB+qU/SrAtR2JmuNPShUWiUYsatLwftmGZGZp3SkTupscPwLACzJlbl5aTxDBPqubySqk+TassaNf8pHDTBJnB/RBE9YprlcEBKtwfkrR5CG8VsODzRHZMcZdgwSd+iZMZ3+7JE7aqg6TVVPHHr2AOIzXq3Iif18fMDcAe9uMvdXCMVO+jhp6+4p11UWKwYgrZyKnWaE= - on: - tags: true - skip_cleanup: true diff --git a/Makefile b/Makefile deleted file mode 100644 index d3a848f..0000000 --- a/Makefile +++ /dev/null @@ -1,50 +0,0 @@ -.PHONY: default -default: verify build ; - -ME ?= github.com/svenwiltink/go-musicbot -PACKAGE_NAME ?= $(NAME) -NAME ?= MusicBot - -GIT_COMMIT_HASH=`git log | head -n1 | awk '{print $$2}'` -BUILD_DATE=`date +%Y-%m-%d_%H:%M:%S` -BUILD_HOST=`hostname` -GO_VERSION=`go version | awk '{print $$3}'` -GIT_VERSION_TAG=`git describe --tags --long` - -BUILD_PLATFORMS ?= -os '!netbsd' -os '!openbsd' -os '!freebsd' - -all: deps verify build test - -help: - -deps: - GO111MODULE=off go get -u golang.org/x/lint/golint - GO111MODULE=off go get -u github.com/mitchellh/gox - go mod vendor - -verify: fmt lint - -fmt: - @go fmt ./... | awk '{if (NF > 0) {if (NR == 1) print "Please run go fmt for:"; print "- "$$1}} END {if (NF > 0) {if (NR > 0) exit 1}}' - -lint: - @golint ./... | ( ! grep -v -e "^vendor/" -e "be unexported" -e "don't use an underscore in package name" -e "ALL_CAPS" ) - -build: - @mkdir -p out/binaries - gox $(BUILD_PLATFORMS) \ - -ldflags " \ - -X github.com/svenwiltink/go-musicbot/pkg/bot.Version=${GIT_VERSION_TAG} \ - -X github.com/svenwiltink/go-musicbot/pkg/bot.GoVersion=${GO_VERSION} \ - -X github.com/svenwiltink/go-musicbot/pkg/bot.BuildDate=${BUILD_DATE}" \ - -output="out/binaries/$(NAME)-{{.OS}}-{{.Arch}}" github.com/svenwiltink/go-musicbot/cmd/go-musicbot -test: - go test -v 'github.com/svenwiltink/go-musicbot/...' - -deb: - ./deb-build.sh - rm -rf pkg_root - -clean: - rm -rf out - rm -rf pkg_root diff --git a/README.md b/README.md index d9a74e7..f38d506 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ -[![.deb provided by packagecloud](https://img.shields.io/badge/deb-packagecloud.io-844fec.svg)](https://packagecloud.io/svenwiltink/go-musicbot)[![Build Status](https://travis-ci.org/svenwiltink/go-MusicBot.svg?branch=master)](https://travis-ci.org/svenwiltink/go-MusicBot) +[![.deb provided by packagecloud](https://img.shields.io/badge/deb-packagecloud.io-844fec.svg)](https://packagecloud.io/svenwiltink/go-musicbot)![build status](https://github.com/svenwiltink/go-musicbot/actions/workflows/build.yml/badge.svg) -A simple musicbot to run on IRC or Mattermost. Debian package hosted on [packagecloud](https://packagecloud.io/svenwiltink/go-musicbot) +A simple musicbot to run on IRC, mattermost or slack. Debian package hosted on [packagecloud](https://packagecloud.io/svenwiltink/go-musicbot) diff --git a/deb-build.sh b/deb-build.sh index 3fd67a7..e4aeb67 100755 --- a/deb-build.sh +++ b/deb-build.sh @@ -16,7 +16,7 @@ mkdir -p out/packages mkdir -p ${PKG_ROOT}/usr/local/bin mkdir -p ${PKG_ROOT}/usr/local/etc/go-Musicbot -cp out/binaries/MusicBot-linux-amd64 \ +cp go-musicbot \ ${PKG_ROOT}/usr/local/bin/go-Musicbot cp dist/config.json.example \ @@ -32,7 +32,7 @@ fpm \ --license MIT \ -m "Sven Wiltink" \ --url "https://github.com/svenwiltink/go-musicbot" \ - --description "A musicbot for rocketchat or irc" \ + --description "A musicbot for IRC, mattermost and slack" \ --config-files /usr/local/etc/go-Musicbot \ -p "out/packages/${VERSION}.deb" diff --git a/dist/config.json.example b/dist/config.json.example index f07663a..24d2a9b 100644 --- a/dist/config.json.example +++ b/dist/config.json.example @@ -19,7 +19,7 @@ "teamname": "svenwiltink", "channel": "muziek", "privateAccessToken": "", - "ssl": true + "ssl": true, "connectionTimeout": 30 }, "youtube": { diff --git a/pkg/bot/bot.go b/pkg/bot/bot.go index f3775d1..4fc8a68 100644 --- a/pkg/bot/bot.go +++ b/pkg/bot/bot.go @@ -17,10 +17,6 @@ import ( var ( errCommandNotFound = errors.New("command not found") errVariableNotFound = errors.New("command variable not found") - - Version = "placeholder string" - GoVersion = "placeholder string" - BuildDate = "placeholder string" ) type MusicBot struct { @@ -147,11 +143,15 @@ func (bot *MusicBot) getCommand(name string) (Command, error) { } func (bot *MusicBot) ReplyToMessage(message Message, reply string) { - bot.messageProvider.SendReplyToMessage(message, reply) + if err := bot.messageProvider.SendReplyToMessage(message, reply); err != nil { + log.Printf("Error replying to message: %s", err) + } } func (bot *MusicBot) BroadcastMessage(message string) { - bot.messageProvider.BroadcastMessage(message) + if err := bot.messageProvider.BroadcastMessage(message); err != nil { + log.Printf("Error broadcasting message: %s", err) + } } func (bot *MusicBot) Stop() { diff --git a/pkg/bot/commands.go b/pkg/bot/commands.go index 26352ea..b723d90 100644 --- a/pkg/bot/commands.go +++ b/pkg/bot/commands.go @@ -2,10 +2,12 @@ package bot import ( "fmt" - "github.com/svenwiltink/go-musicbot/pkg/music" + "runtime/debug" "strconv" "strings" "time" + + "github.com/svenwiltink/go-musicbot/pkg/music" ) type Command struct { @@ -402,7 +404,7 @@ var volCommand = Command{ } if volume >= 0 && volume <= 100 { - bot.musicPlayer.SetVolume(volume) + _ = bot.musicPlayer.SetVolume(volume) } else { bot.ReplyToMessage(message, fmt.Sprintf("%s is not a valid volume", volumeString)) return @@ -421,9 +423,25 @@ var volCommand = Command{ var aboutCommand = Command{ Name: "about", Function: func(bot *MusicBot, message Message) { + var GoVersion, Version, BuildDate string + + info, ok := debug.ReadBuildInfo() + if ok { + GoVersion = info.GoVersion + + for _, setting := range info.Settings { + switch setting.Key { + case "vcs.revision": + Version = setting.Value + case "vcs.time": + BuildDate = setting.Value + } + } + } + bot.ReplyToMessage(message, "go-MusicBot by Sven Wiltink: https://github.com/svenwiltink/go-MusicBot") - bot.ReplyToMessage(message, fmt.Sprintf("Version: %s", Version)) bot.ReplyToMessage(message, fmt.Sprintf("Go: %s", GoVersion)) + bot.ReplyToMessage(message, fmt.Sprintf("Version: %s", Version)) bot.ReplyToMessage(message, fmt.Sprintf("Build date: %s", BuildDate)) }, } diff --git a/pkg/bot/config.go b/pkg/bot/config.go index 3564be7..a2e8892 100644 --- a/pkg/bot/config.go +++ b/pkg/bot/config.go @@ -3,9 +3,10 @@ package bot import ( "encoding/json" "fmt" - "github.com/pkg/errors" "os" "time" + + "github.com/pkg/errors" ) const ( diff --git a/pkg/bot/messageprovider/mattermost/mattermost.go b/pkg/bot/messageprovider/mattermost/mattermost.go index 0ffd922..b7f0470 100644 --- a/pkg/bot/messageprovider/mattermost/mattermost.go +++ b/pkg/bot/messageprovider/mattermost/mattermost.go @@ -2,12 +2,13 @@ package mattermost import ( "fmt" - "github.com/gorilla/websocket" - mattermost "github.com/mattermost/mattermost-server/v5/model" - "github.com/svenwiltink/go-musicbot/pkg/bot" "log" "strings" "time" + + "github.com/gorilla/websocket" + mattermost "github.com/mattermost/mattermost-server/v5/model" + "github.com/svenwiltink/go-musicbot/pkg/bot" ) type MessageProvider struct { @@ -114,8 +115,8 @@ func (provider *MessageProvider) startReadLoop() { timeout := provider.Config.Mattermost.ConnectionTimeout for { - provider.websocketClient.Conn.SetWriteDeadline(time.Now().Add(timeout * time.Second)) - provider.websocketClient.Conn.SetReadDeadline(time.Now().Add(timeout * time.Second)) + _ = provider.websocketClient.Conn.SetWriteDeadline(time.Now().Add(timeout * time.Second)) + _ = provider.websocketClient.Conn.SetReadDeadline(time.Now().Add(timeout * time.Second)) for event := range provider.websocketClient.EventChannel { if event.Event == mattermost.WEBSOCKET_EVENT_POSTED { @@ -193,13 +194,13 @@ func (provider *MessageProvider) pingLoop() { timeout := provider.Config.Mattermost.ConnectionTimeout log.Printf("Starting ping loop with timeout of %d seconds", timeout) - provider.websocketClient.Conn.SetWriteDeadline(time.Now().Add(timeout * time.Second)) - provider.websocketClient.Conn.SetReadDeadline(time.Now().Add(timeout * time.Second)) + _ = provider.websocketClient.Conn.SetWriteDeadline(time.Now().Add(timeout * time.Second)) + _ = provider.websocketClient.Conn.SetReadDeadline(time.Now().Add(timeout * time.Second)) // push back the timeout by 30 seconds every time we get a pong provider.websocketClient.Conn.SetPongHandler(func(appData string) error { - provider.websocketClient.Conn.SetWriteDeadline(time.Now().Add(timeout * time.Second)) - provider.websocketClient.Conn.SetReadDeadline(time.Now().Add(timeout * time.Second)) + _ = provider.websocketClient.Conn.SetWriteDeadline(time.Now().Add(timeout * time.Second)) + _ = provider.websocketClient.Conn.SetReadDeadline(time.Now().Add(timeout * time.Second)) return nil }) diff --git a/pkg/bot/messageprovider/slack/slack.go b/pkg/bot/messageprovider/slack/slack.go index c13c417..5b97766 100644 --- a/pkg/bot/messageprovider/slack/slack.go +++ b/pkg/bot/messageprovider/slack/slack.go @@ -2,12 +2,13 @@ package slack import ( "fmt" + "log" + "os" + "github.com/slack-go/slack" "github.com/slack-go/slack/slackevents" "github.com/slack-go/slack/socketmode" "github.com/svenwiltink/go-musicbot/pkg/bot" - "log" - "os" ) type MessageProvider struct { @@ -24,7 +25,6 @@ func (provider *MessageProvider) Start() error { provider.rtm = socketmode.New(provider.api) go func() { err := provider.rtm.Run() - provider.rtm.Run() if err != nil { panic(err) } diff --git a/pkg/bot/messageprovider/terminal/terminal.go b/pkg/bot/messageprovider/terminal/terminal.go index 50d3f59..61f3e70 100644 --- a/pkg/bot/messageprovider/terminal/terminal.go +++ b/pkg/bot/messageprovider/terminal/terminal.go @@ -17,12 +17,12 @@ func (messageProvider *MessageProvider) GetMessageChannel() chan bot.Message { } func (messageProvider *MessageProvider) SendReplyToMessage(message bot.Message, reply string) error { - log.Printf(reply) + log.Printf("%s", reply) return nil } func (messageProvider *MessageProvider) BroadcastMessage(message string) error { - log.Printf(message) + log.Printf("%s", message) return nil } diff --git a/pkg/music/dataprovider/nts/nts.go b/pkg/music/dataprovider/nts/nts.go index 93eb9a6..438fa7e 100644 --- a/pkg/music/dataprovider/nts/nts.go +++ b/pkg/music/dataprovider/nts/nts.go @@ -2,6 +2,7 @@ package nts import ( "fmt" + "github.com/svenwiltink/go-musicbot/pkg/music" ) @@ -42,7 +43,7 @@ func (DataProvider) Search(name string) ([]music.Song, error) { fmt.Println("trying to search NTS ", name) songs := make([]music.Song, 0, len(streams)) - for stream, _ := range streams { + for stream := range streams { songs = append(songs, music.Song{ Name: stream, Artist: "nts", diff --git a/pkg/music/dataprovider/youtube/youtube.go b/pkg/music/dataprovider/youtube/youtube.go index 030af5f..a7a248e 100644 --- a/pkg/music/dataprovider/youtube/youtube.go +++ b/pkg/music/dataprovider/youtube/youtube.go @@ -1,23 +1,22 @@ package youtube import ( + "context" "errors" "fmt" - isoduration "github.com/channelmeter/iso8601duration" - "net/http" "net/url" "regexp" "strings" + isoduration "github.com/channelmeter/iso8601duration" + "github.com/svenwiltink/go-musicbot/pkg/music" - "google.golang.org/api/googleapi/transport" + "google.golang.org/api/option" "google.golang.org/api/youtube/v3" ) const ( - youTubeVideoURL = "https://www.youtube.com/watch?v=%s" - youTubePlaylistURL = "https://www.youtube.com/watch?v=%s&list=%s" - + youTubeVideoURL = "https://www.youtube.com/watch?v=%s" MaxYoutubeItems = 500 ) @@ -43,11 +42,7 @@ func NewDataProvider(apiKey string) (*DataProvider, error) { } func (provider *DataProvider) initAPIClient() error { - client := &http.Client{ - Transport: &transport.APIKey{Key: provider.apiKey}, - } - - service, err := youtube.New(client) + service, err := youtube.NewService(context.Background(), option.WithAPIKey(provider.apiKey)) if err != nil { return fmt.Errorf("YoutubeAPI.init: Error creating client: %v", err) } diff --git a/pkg/music/player/musicplayer.go b/pkg/music/player/musicplayer.go index 61a4f84..fd1ffda 100644 --- a/pkg/music/player/musicplayer.go +++ b/pkg/music/player/musicplayer.go @@ -2,12 +2,14 @@ package player import ( "fmt" - "github.com/svenwiltink/go-musicbot/pkg/music" "log" + "github.com/svenwiltink/go-musicbot/pkg/music" + "errors" - "github.com/vansante/go-event-emitter" "time" + + eventemitter "github.com/vansante/go-event-emitter" ) // MusicPlayer is responsible for playing music @@ -62,7 +64,7 @@ func (player *MusicPlayer) GetStatus() music.PlayerStatus { func (player *MusicPlayer) GetCurrentSong() (*music.Song, time.Duration) { if player.currentSong != nil { - return player.currentSong, player.currentSongEnds.Sub(time.Now()).Round(time.Second) + return player.currentSong, time.Until(player.currentSongEnds).Round(time.Second) } return nil, time.Duration(0) @@ -116,10 +118,6 @@ func (player *MusicPlayer) GetVolume() (int, error) { return 0, errors.New("nothing is playing") } -func (player *MusicPlayer) addMusicProvider(provider music.Provider) { - player.musicProviders = append(player.musicProviders, provider) -} - func (player *MusicPlayer) Search(searchString string) ([]music.Song, error) { songs := make([]music.Song, 0) diff --git a/pkg/music/provider/mpv/mpv.go b/pkg/music/provider/mpv/mpv.go index 1804d3e..75c5e18 100644 --- a/pkg/music/provider/mpv/mpv.go +++ b/pkg/music/provider/mpv/mpv.go @@ -10,6 +10,7 @@ import ( "time" "context" + "github.com/DexterLB/mpvipc" "github.com/svenwiltink/go-musicbot/pkg/music" eventemitter "github.com/vansante/go-event-emitter" @@ -264,7 +265,7 @@ func (player *Player) Pause() error { func (player *Player) Stop() { if player.isRunning { - player.process.Process.Kill() + _ = player.process.Process.Kill() } os.Remove(player.socketPath) diff --git a/pkg/music/queue_test.go b/pkg/music/queue_test.go index 09c9f47..3234801 100644 --- a/pkg/music/queue_test.go +++ b/pkg/music/queue_test.go @@ -1,10 +1,11 @@ package music import ( - "github.com/stretchr/testify/assert" "math/rand" "testing" "time" + + "github.com/stretchr/testify/assert" ) func TestQueue_GetLength(t *testing.T) { @@ -127,7 +128,6 @@ func TestQueue_Flush(t *testing.T) { func TestQueue_Append_Multiple(t *testing.T) { t.Parallel() - queue := NewQueue() queue, song1, song2 := getTestQueue()