diff --git a/.goreleaser.yml b/.goreleaser.yml index 3fa968a..5ce855a 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -19,9 +19,9 @@ builds: - 7 ldflags: - -s -w - - -X "main.Version={{ .Version }}" - - -X "main.GitCommit={{ .ShortCommit }}" - - -X "main.Timestamp={{ .Timestamp }}" + - -X "github.com/l3uddz/nabarr/build.Version={{ .Version }}" + - -X "github.com/l3uddz/nabarr/build.GitCommit={{ .ShortCommit }}" + - -X "github.com/l3uddz/nabarr/build.Timestamp={{ .Timestamp }}" flags: - -trimpath diff --git a/Makefile b/Makefile index 40eaea6..ddbf772 100644 --- a/Makefile +++ b/Makefile @@ -38,7 +38,7 @@ ${BUILD_PATH}/${CMD}: ${GO_FILES} go.sum CGO_ENABLED=${CGO} go build \ -mod vendor \ -trimpath \ - -ldflags "-s -w -X main.Version=${VERSION} -X main.GitCommit=${GIT_COMMIT} -X main.Timestamp=${TIMESTAMP}" \ + -ldflags "-s -w -X github.com/l3uddz/nabarr/build.Version=${VERSION} -X github.com/l3uddz/nabarr/build.GitCommit=${GIT_COMMIT} -X github.com/l3uddz/nabarr/build.Timestamp=${TIMESTAMP}" \ -o ${BUILD_PATH}/${CMD} \ ./cmd/nabarr diff --git a/build/build.go b/build/build.go new file mode 100644 index 0000000..bf8c3a2 --- /dev/null +++ b/build/build.go @@ -0,0 +1,7 @@ +package build + +var ( + Version string + Timestamp string + GitCommit string +) diff --git a/cmd/nabarr/main.go b/cmd/nabarr/main.go index 0aaa5a4..15f3fab 100644 --- a/cmd/nabarr/main.go +++ b/cmd/nabarr/main.go @@ -6,6 +6,7 @@ import ( "github.com/alecthomas/kong" "github.com/goccy/go-yaml" "github.com/l3uddz/nabarr" + "github.com/l3uddz/nabarr/build" "github.com/l3uddz/nabarr/cache" "github.com/l3uddz/nabarr/cmd/nabarr/pvr" "github.com/l3uddz/nabarr/media" @@ -28,10 +29,6 @@ type config struct { } var ( - Version string - Timestamp string - GitCommit string - // CLI cli struct { globals @@ -67,7 +64,7 @@ func main() { Compact: true, }), kong.Vars{ - "version": fmt.Sprintf("%s (%s@%s)", Version, GitCommit, Timestamp), + "version": fmt.Sprintf("%s (%s@%s)", build.Version, build.GitCommit, build.Timestamp), "config_file": filepath.Join(defaultConfigPath(), "config.yml"), "cache_file": filepath.Join(defaultConfigPath(), "cache"), "log_file": filepath.Join(defaultConfigPath(), "activity.log"), diff --git a/cmd/nabarr/update.go b/cmd/nabarr/update.go index 445f2cb..8435fd3 100644 --- a/cmd/nabarr/update.go +++ b/cmd/nabarr/update.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/alecthomas/kong" "github.com/blang/semver" + "github.com/l3uddz/nabarr/build" "github.com/rhysd/go-github-selfupdate/selfupdate" "os" ) @@ -15,7 +16,7 @@ func (u updateFlag) Decode(ctx *kong.DecodeContext) error { return nil } func (u updateFlag) IsBool() bool { return true } func (u updateFlag) BeforeApply(app *kong.Kong, vars kong.Vars) error { // parse current version - v, err := semver.Parse(Version) + v, err := semver.Parse(build.Version) if err != nil { fmt.Printf("Failed parsing current build version: %v\n", err) app.Exit(1) @@ -33,7 +34,7 @@ func (u updateFlag) BeforeApply(app *kong.Kong, vars kong.Vars) error { // check version if !found || latest.Version.LTE(v) { - fmt.Printf("Already using the latest version: %v\n", Version) + fmt.Printf("Already using the latest version: %v\n", build.Version) app.Exit(0) return nil } diff --git a/util/http.go b/util/http.go index 44dd2c3..4cfcafb 100644 --- a/util/http.go +++ b/util/http.go @@ -2,6 +2,7 @@ package util import ( "github.com/hashicorp/go-retryablehttp" + "github.com/l3uddz/nabarr/build" "github.com/rs/zerolog" "go.uber.org/ratelimit" "net/http" @@ -14,6 +15,9 @@ func NewRetryableHttpClient(timeout time.Duration, rl ratelimit.Limiter, log *ze retryClient.RetryWaitMin = 1 * time.Second retryClient.RetryWaitMax = 10 * time.Second retryClient.RequestLogHook = func(l retryablehttp.Logger, request *http.Request, i int) { + // set user-agent + request.Header.Set("User-Agent", "nabarr/"+build.Version) + // rate limit if rl != nil { rl.Take()