From 46e6a667c8037e01846fef4ddad7caf5d023ddb0 Mon Sep 17 00:00:00 2001 From: Philipp Wolfer Date: Fri, 24 Nov 2023 08:58:31 +0100 Subject: [PATCH] Added constants for app name and version, use custom user-agent --- cmd/root.go | 9 +++--- internal/backends/deezer/client.go | 2 ++ internal/backends/funkwhale/client.go | 2 ++ internal/backends/listenbrainz/client.go | 2 ++ .../backends/listenbrainz/listenbrainz.go | 4 ++- internal/backends/maloja/client.go | 2 ++ internal/backends/spotify/client.go | 2 ++ internal/backends/subsonic/subsonic.go | 3 +- internal/version/version.go | 25 ++++++++++++++++ internal/version/version_test.go | 30 +++++++++++++++++++ 10 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 internal/version/version.go create mode 100644 internal/version/version_test.go diff --git a/cmd/root.go b/cmd/root.go index f1cc298..10dc528 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -23,17 +23,18 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" + "go.uploadedlobster.com/scotty/internal/version" ) var cfgFile string // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ - Use: "scotty", + Use: version.AppName, Short: "Beam data between music listening services", Long: `Scotty transfers your listens/scrobbles between ListenBrainz and various other listening and streaming services.`, - Version: "0.1.0", + Version: version.AppVersion, // Uncomment the following line if your bare application // has an action associated with it: // Run: func(cmd *cobra.Command, args []string) { }, @@ -67,7 +68,7 @@ func init() { func defaultConfigDir() string { configDir, err := os.UserConfigDir() cobra.CheckErr(err) - return path.Join(configDir, "scotty") + return path.Join(configDir, version.AppName) } // initConfig reads in config file and ENV variables if set. @@ -78,7 +79,7 @@ func initConfig() { } else { viper.AddConfigPath(defaultConfigDir()) viper.SetConfigType("toml") - viper.SetConfigName("scotty") + viper.SetConfigName(version.AppName) } viper.AutomaticEnv() // read in environment variables that match diff --git a/internal/backends/deezer/client.go b/internal/backends/deezer/client.go index ef48a4c..0d9cbb0 100644 --- a/internal/backends/deezer/client.go +++ b/internal/backends/deezer/client.go @@ -27,6 +27,7 @@ import ( "strconv" "github.com/go-resty/resty/v2" + "go.uploadedlobster.com/scotty/internal/version" "golang.org/x/oauth2" ) @@ -43,6 +44,7 @@ func NewClient(token oauth2.TokenSource) Client { client := resty.New() client.SetBaseURL(baseURL) client.SetHeader("Accept", "application/json") + client.SetHeader("User-Agent", version.UserAgent()) client.SetRetryCount(5) return Client{ HttpClient: client, diff --git a/internal/backends/funkwhale/client.go b/internal/backends/funkwhale/client.go index ebd049c..b757d6f 100644 --- a/internal/backends/funkwhale/client.go +++ b/internal/backends/funkwhale/client.go @@ -27,6 +27,7 @@ import ( "github.com/go-resty/resty/v2" "go.uploadedlobster.com/scotty/internal/ratelimit" + "go.uploadedlobster.com/scotty/internal/version" ) const MaxItemsPerGet = 50 @@ -42,6 +43,7 @@ func NewClient(serverUrl string, token string) Client { client.SetAuthScheme("Bearer") client.SetAuthToken(token) client.SetHeader("Accept", "application/json") + client.SetHeader("User-Agent", version.UserAgent()) // Handle rate limiting (see https://docs.funkwhale.audio/developer/api/rate-limit.html) ratelimit.EnableHTTPHeaderRateLimit(client, "Retry-After") diff --git a/internal/backends/listenbrainz/client.go b/internal/backends/listenbrainz/client.go index da59144..aa30b78 100644 --- a/internal/backends/listenbrainz/client.go +++ b/internal/backends/listenbrainz/client.go @@ -28,6 +28,7 @@ import ( "github.com/go-resty/resty/v2" "go.uploadedlobster.com/scotty/internal/ratelimit" + "go.uploadedlobster.com/scotty/internal/version" ) const ( @@ -48,6 +49,7 @@ func NewClient(token string) Client { client.SetAuthScheme("Token") client.SetAuthToken(token) client.SetHeader("Accept", "application/json") + client.SetHeader("User-Agent", version.UserAgent()) // Handle rate limiting (see https://listenbrainz.readthedocs.io/en/latest/users/api/index.html#rate-limiting) ratelimit.EnableHTTPHeaderRateLimit(client, "X-RateLimit-Reset-In") diff --git a/internal/backends/listenbrainz/listenbrainz.go b/internal/backends/listenbrainz/listenbrainz.go index 35cd37a..e972eca 100644 --- a/internal/backends/listenbrainz/listenbrainz.go +++ b/internal/backends/listenbrainz/listenbrainz.go @@ -23,6 +23,7 @@ import ( "github.com/spf13/viper" "go.uploadedlobster.com/scotty/internal/models" + "go.uploadedlobster.com/scotty/internal/version" ) type ListenBrainzApiBackend struct { @@ -119,7 +120,8 @@ func (b *ListenBrainzApiBackend) ImportListens(export models.ListensResult, impo AdditionalInfo: l.AdditionalInfo, }, } - listen.TrackMetadata.AdditionalInfo["submission_client"] = "Scotty" + listen.TrackMetadata.AdditionalInfo["submission_client"] = version.AppName + listen.TrackMetadata.AdditionalInfo["submission_client_version"] = version.AppVersion submission.Payload = append(submission.Payload, listen) } diff --git a/internal/backends/maloja/client.go b/internal/backends/maloja/client.go index 5e3ea31..3b79110 100644 --- a/internal/backends/maloja/client.go +++ b/internal/backends/maloja/client.go @@ -26,6 +26,7 @@ import ( "strconv" "github.com/go-resty/resty/v2" + "go.uploadedlobster.com/scotty/internal/version" ) const MaxItemsPerGet = 1000 @@ -39,6 +40,7 @@ func NewClient(serverUrl string, token string) Client { client := resty.New() client.SetBaseURL(serverUrl) client.SetHeader("Accept", "application/json") + client.SetHeader("User-Agent", version.UserAgent()) client.SetRetryCount(5) return Client{ HttpClient: client, diff --git a/internal/backends/spotify/client.go b/internal/backends/spotify/client.go index 2ec12b0..08c00f3 100644 --- a/internal/backends/spotify/client.go +++ b/internal/backends/spotify/client.go @@ -30,6 +30,7 @@ import ( "github.com/go-resty/resty/v2" "go.uploadedlobster.com/scotty/internal/ratelimit" + "go.uploadedlobster.com/scotty/internal/version" "golang.org/x/oauth2" ) @@ -48,6 +49,7 @@ func NewClient(token oauth2.TokenSource) Client { client := resty.NewWithClient(httpClient) client.SetBaseURL(baseURL) client.SetHeader("Accept", "application/json") + client.SetHeader("User-Agent", version.UserAgent()) // Handle rate limiting (see https://developer.spotify.com/documentation/web-api/concepts/rate-limits) ratelimit.EnableHTTPHeaderRateLimit(client, "Retry-After") diff --git a/internal/backends/subsonic/subsonic.go b/internal/backends/subsonic/subsonic.go index 348a249..732b9ab 100644 --- a/internal/backends/subsonic/subsonic.go +++ b/internal/backends/subsonic/subsonic.go @@ -24,6 +24,7 @@ import ( "github.com/delucks/go-subsonic" "github.com/spf13/viper" "go.uploadedlobster.com/scotty/internal/models" + "go.uploadedlobster.com/scotty/internal/version" ) type SubsonicApiBackend struct { @@ -38,7 +39,7 @@ func (b *SubsonicApiBackend) FromConfig(config *viper.Viper) models.Backend { Client: &http.Client{}, BaseUrl: config.GetString("server-url"), User: config.GetString("username"), - ClientName: "Scotty", + ClientName: version.AppName, } b.password = config.GetString("token") return b diff --git a/internal/version/version.go b/internal/version/version.go new file mode 100644 index 0000000..43a6205 --- /dev/null +++ b/internal/version/version.go @@ -0,0 +1,25 @@ +/* +Copyright © 2023 Philipp Wolfer + +Scotty is free software: you can redistribute it and/or modify it under the +terms of the GNU General Public License as published by the Free Software +Foundation, either version 3 of the License, or (at your option) any later version. + +Scotty is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along with +Scotty. If not, see . +*/ + +package version + +const ( + AppName = "scotty" + AppVersion = "0.1.0" +) + +func UserAgent() string { + return AppName + "/" + AppVersion +} diff --git a/internal/version/version_test.go b/internal/version/version_test.go new file mode 100644 index 0000000..4472b91 --- /dev/null +++ b/internal/version/version_test.go @@ -0,0 +1,30 @@ +/* +Copyright © 2023 Philipp Wolfer + +Scotty is free software: you can redistribute it and/or modify it under the +terms of the GNU General Public License as published by the Free Software +Foundation, either version 3 of the License, or (at your option) any later version. + +Scotty is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along with +Scotty. If not, see . +*/ + +package version_test + +import ( + "testing" + + "go.uploadedlobster.com/scotty/internal/version" +) + +func TestUserAgent(t *testing.T) { + expected := "scotty/" + version.AppVersion + ua := version.UserAgent() + if ua != expected { + t.Errorf("Expected UserAgent to be '%v', got '%v'", expected, ua) + } +}