Skip to content

Commit bf6605a

Browse files
authored
feat: add user-agent (#2)
1 parent 47e16f0 commit bf6605a

File tree

6 files changed

+88
-2
lines changed

6 files changed

+88
-2
lines changed

.goreleaser.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ builds:
88
flags:
99
- -mod=vendor
1010
- -v
11+
ldflags:
12+
- -X github.com/hostinger/api-cli/utils/version.Build={{ .ShortCommit }}
13+
- -X github.com/hostinger/api-cli/utils/version.Major={{ .Major }}
14+
- -X github.com/hostinger/api-cli/utils/version.Minor={{ .Minor }}
15+
- -X github.com/hostinger/api-cli/utils/version.Patch={{ .Patch }}
1116
goos:
1217
- windows
1318
- darwin

api/request.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package api
22

33
import (
4+
"context"
5+
"fmt"
46
"github.com/hostinger/api-cli/client"
7+
"github.com/hostinger/api-cli/utils"
58
"github.com/oapi-codegen/oapi-codegen/v2/pkg/securityprovider"
69
"github.com/spf13/viper"
710
"log"
11+
"net/http"
812
)
913

1014
func Request() client.ClientWithResponsesInterface {
@@ -18,10 +22,19 @@ func Request() client.ClientWithResponsesInterface {
1822
apiUrl = "https://developers.hostinger.com"
1923
}
2024

21-
c, err := client.NewClientWithResponses(apiUrl, client.WithRequestEditorFn(bearerToken.Intercept))
25+
c, err := client.NewClientWithResponses(
26+
apiUrl,
27+
client.WithRequestEditorFn(bearerToken.Intercept),
28+
client.WithRequestEditorFn(addUserAgent),
29+
)
2230
if err != nil {
2331
log.Fatal(err)
2432
}
2533

2634
return c
2735
}
36+
37+
func addUserAgent(ctx context.Context, req *http.Request) error {
38+
req.Header.Set("User-Agent", fmt.Sprintf("hostinger-cli/%s", utils.CLIVersion.String(false)))
39+
return nil
40+
}

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func init() {
4444
})
4545

4646
RootCmd.AddCommand(vps.GroupCmd)
47+
RootCmd.AddCommand(VersionCmd)
4748
}
4849

4950
func initConfig() {

cmd/version.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"github.com/hostinger/api-cli/utils"
6+
"github.com/spf13/cobra"
7+
)
8+
9+
var VersionCmd = &cobra.Command{
10+
Use: "version",
11+
Short: "Show the current version",
12+
Run: func(cmd *cobra.Command, args []string) {
13+
fmt.Println(utils.CLIVersion.String(true))
14+
},
15+
}

main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package main
22

3-
import "github.com/hostinger/api-cli/cmd"
3+
import (
4+
"github.com/hostinger/api-cli/cmd"
5+
)
46

57
func main() {
68
cmd.Execute()

utils/version.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package utils
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"strconv"
7+
)
8+
9+
type Version struct {
10+
Major, Minor, Patch int
11+
Build string
12+
}
13+
14+
var (
15+
Build string
16+
Major string
17+
Minor string
18+
Patch string
19+
20+
CLIVersion Version
21+
)
22+
23+
func init() {
24+
if Build != "" {
25+
CLIVersion.Build = Build
26+
}
27+
if Major != "" {
28+
i, _ := strconv.Atoi(Major)
29+
CLIVersion.Major = i
30+
}
31+
if Minor != "" {
32+
i, _ := strconv.Atoi(Minor)
33+
CLIVersion.Minor = i
34+
}
35+
if Patch != "" {
36+
i, _ := strconv.Atoi(Patch)
37+
CLIVersion.Patch = i
38+
}
39+
}
40+
41+
func (v Version) String(withBuild bool) string {
42+
var buffer bytes.Buffer
43+
if withBuild {
44+
buffer.WriteString(fmt.Sprintf("%d.%d.%d (Build: %s)", v.Major, v.Minor, v.Patch, v.Build))
45+
} else {
46+
buffer.WriteString(fmt.Sprintf("%d.%d.%d", v.Major, v.Minor, v.Patch))
47+
}
48+
49+
return buffer.String()
50+
}

0 commit comments

Comments
 (0)