Skip to content

Commit b748bad

Browse files
authored
fix(parser): apply version to user-agent (#26)
* fix(parser): apply version to user-agent This moves the version var that we use LDL flags on builds to set to the current release version, and makes sure it is always applied by default. * addresses pr feedback
1 parent be59982 commit b748bad

5 files changed

Lines changed: 27 additions & 14 deletions

File tree

.goreleaser.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ builds:
3232
- goos: windows
3333
goarch: arm64
3434
ldflags:
35-
- -s -w -X main.version={{.Version}}
35+
- -s -w -X github.com/erraggy/oastools.version={{.Version}}
3636

3737
archives:
3838
- formats: [tar.gz]

build_details.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package oastools
2+
3+
import "fmt"
4+
5+
var (
6+
// version is set via ldflags during build by GoReleaser
7+
// For development builds, this will show "dev"
8+
version = "dev"
9+
)
10+
11+
// Version returns the compiled version or 'dev' if run from source
12+
func Version() string {
13+
return version
14+
}
15+
16+
// UserAgent returns the User-Agent string to use
17+
func UserAgent() string {
18+
return fmt.Sprintf("oastools/%s", version)
19+
}

cmd/oastools/main.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"path/filepath"
88
"time"
99

10+
"github.com/erraggy/oastools"
1011
"github.com/erraggy/oastools/converter"
1112
"github.com/erraggy/oastools/differ"
1213
"github.com/erraggy/oastools/joiner"
@@ -15,10 +16,6 @@ import (
1516
"gopkg.in/yaml.v3"
1617
)
1718

18-
// version is set via ldflags during build by GoReleaser
19-
// For development builds, this will show "dev"
20-
var version = "dev"
21-
2219
func main() {
2320
if len(os.Args) < 2 {
2421
printUsage()
@@ -29,7 +26,7 @@ func main() {
2926

3027
switch command {
3128
case "version", "-v", "--version":
32-
fmt.Printf("oastools v%s\n", version)
29+
fmt.Printf("oastools v%s\n", oastools.Version())
3330
case "help", "-h", "--help":
3431
printUsage()
3532
case "validate":
@@ -60,7 +57,6 @@ func handleParse(args []string) {
6057

6158
// Create parser with version in User-Agent
6259
p := parser.New()
63-
p.UserAgent = fmt.Sprintf("oastools/%s", version)
6460

6561
// Parse the file or URL
6662
result, err := p.Parse(specPath)
@@ -174,7 +170,6 @@ func handleValidate(args []string) {
174170
v := validator.New()
175171
v.StrictMode = strict
176172
v.IncludeWarnings = !noWarnings
177-
v.UserAgent = fmt.Sprintf("oastools/%s", version)
178173

179174
// Validate the file or URL with timing
180175
startTime := time.Now()
@@ -524,7 +519,6 @@ func handleConvert(args []string) {
524519
c := converter.New()
525520
c.StrictMode = strict
526521
c.IncludeInfo = !noWarnings
527-
c.UserAgent = fmt.Sprintf("oastools/%s", version)
528522

529523
// Convert the file or URL with timing
530524
startTime := time.Now()
@@ -671,7 +665,6 @@ func handleDiff(args []string) {
671665
d.Mode = differ.ModeSimple
672666
}
673667
d.IncludeInfo = !noInfo
674-
d.UserAgent = fmt.Sprintf("oastools/%s", version)
675668

676669
// Diff the files with timing
677670
startTime := time.Now()

parser/parser.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strings"
1212
"time"
1313

14+
"github.com/erraggy/oastools"
1415
semver "github.com/hashicorp/go-version"
1516
"gopkg.in/yaml.v3"
1617

@@ -33,7 +34,7 @@ func New() *Parser {
3334
return &Parser{
3435
ResolveRefs: false,
3536
ValidateStructure: true,
36-
UserAgent: "oastools",
37+
UserAgent: oastools.UserAgent(),
3738
}
3839
}
3940

@@ -154,7 +155,7 @@ func (p *Parser) fetchURL(urlStr string) ([]byte, string, error) {
154155
// Set user agent (use default if not set)
155156
userAgent := p.UserAgent
156157
if userAgent == "" {
157-
userAgent = "oastools"
158+
userAgent = oastools.UserAgent()
158159
}
159160
req.Header.Set("User-Agent", userAgent)
160161

parser/parser_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2422,12 +2422,12 @@ paths: {}`
24222422
{
24232423
name: "default user agent when not set",
24242424
userAgent: "",
2425-
expectedUserAgent: "oastools",
2425+
expectedUserAgent: "oastools/dev",
24262426
},
24272427
{
24282428
name: "default user agent from New()",
24292429
userAgent: "default",
2430-
expectedUserAgent: "oastools",
2430+
expectedUserAgent: "oastools/dev",
24312431
},
24322432
}
24332433

0 commit comments

Comments
 (0)