Skip to content

Commit a681cdc

Browse files
authored
Merge pull request #62 from hslatman/automatic-user-agent-version-2
Add automatic module version detection
2 parents 982ac18 + c136e52 commit a681cdc

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

internal/bouncer/bouncer.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,26 @@ import (
2727

2828
"github.com/crowdsecurity/crowdsec/pkg/models"
2929
csbouncer "github.com/crowdsecurity/go-cs-bouncer"
30+
"github.com/hslatman/caddy-crowdsec-bouncer/internal/version"
3031

3132
"go.uber.org/zap"
3233
)
3334

3435
const (
35-
userAgentName = "caddy-cs-bouncer"
36-
userAgentVersion = "v0.8.0"
37-
userAgent = userAgentName + "/" + userAgentVersion
38-
36+
userAgentName = "caddy-cs-bouncer"
3937
maxNumberOfDecisionsToLog = 10
4038
)
4139

40+
var (
41+
userAgent string
42+
userAgentVersion string
43+
)
44+
45+
func init() {
46+
userAgentVersion = version.Current()
47+
userAgent = userAgentName + "/" + userAgentVersion
48+
}
49+
4250
// Bouncer is a wrapper for a CrowdSec bouncer. It supports both the
4351
// streaming and live bouncer implementations. The streaming bouncer is
4452
// backed by an immutable radix tree storing known bad IPs and IP ranges.

internal/version/version.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package version
2+
3+
import (
4+
"runtime/debug"
5+
)
6+
7+
const (
8+
modulePath = "github.com/hslatman/caddy-crowdsec-bouncer"
9+
fallback = "v0.8.0"
10+
)
11+
12+
func Current() string {
13+
info, ok := debug.ReadBuildInfo()
14+
if !ok {
15+
return fallback
16+
}
17+
18+
for _, d := range info.Deps {
19+
if d.Path == modulePath {
20+
return d.Version
21+
}
22+
}
23+
24+
return fallback
25+
}

internal/version/version_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package version
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestCurrent(t *testing.T) {
10+
v := Current()
11+
12+
assert.Equal(t, "v0.8.0", v) // fallback
13+
}

0 commit comments

Comments
 (0)