Skip to content

Commit 1f8bd04

Browse files
authored
useragent: Add ecctl/<version> UserAgent header (#340)
Instead of relying on the `cloud-sdk-go/<version>` default, use ecctl's current version with an `ecctl/` prefix. Signed-off-by: Marc Lopez <marc5.12@outlook.com>
1 parent d90a277 commit 1f8bd04

3 files changed

Lines changed: 28 additions & 3 deletions

File tree

‎cmd/root.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"net/http"
2424
"os"
2525
"path/filepath"
26+
"strings"
2627
"time"
2728

2829
"github.com/elastic/cloud-sdk-go/pkg/api"
@@ -179,6 +180,7 @@ func initApp(cmd *cobra.Command, client *http.Client, v *viper.Viper) error {
179180
Client: client,
180181
OutputDevice: output.NewDevice(defaultOutput),
181182
ErrorDevice: defaultError,
183+
UserAgent: strings.Join([]string{"ecctl", versionInfo.Version}, "/"),
182184
}
183185
if err := v.Unmarshal(&c); err != nil {
184186
return err

‎cmd/root_test.go‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ import (
2727
"testing"
2828

2929
"github.com/elastic/cloud-sdk-go/pkg/multierror"
30+
"github.com/elastic/cloud-sdk-go/pkg/output"
3031
"github.com/spf13/cobra"
3132
"github.com/spf13/viper"
33+
"github.com/stretchr/testify/assert"
3234

3335
cmddeployment "github.com/elastic/ecctl/cmd/deployment"
3436
cmddeploymentplan "github.com/elastic/ecctl/cmd/deployment/plan"
@@ -105,16 +107,21 @@ func TestPopulateValidArgs(t *testing.T) {
105107
}
106108

107109
func TestInitApp(t *testing.T) {
110+
vorig := versionInfo
111+
defer func() { versionInfo = vorig }()
112+
113+
versionInfo.Version = "v2.6.0"
108114
type args struct {
109115
cmd *cobra.Command
110116
client *http.Client
111117
config *ecctl.Config
112118
v *viper.Viper
113119
}
114120
tests := []struct {
115-
name string
116-
args args
117-
err error
121+
name string
122+
args args
123+
err error
124+
wantConfig ecctl.Config
118125
}{
119126
{
120127
name: "version command skips ecctl.Get() bootstrapping",
@@ -171,6 +178,15 @@ func TestInitApp(t *testing.T) {
171178
},
172179
v: viper.New(),
173180
},
181+
wantConfig: ecctl.Config{
182+
Output: "json",
183+
Host: "http://localhost",
184+
APIKey: "some",
185+
Region: "ece-region",
186+
UserAgent: "ecctl/v2.6.0",
187+
OutputDevice: output.NewDevice(defaultOutput),
188+
ErrorDevice: defaultError,
189+
},
174190
},
175191
{
176192
name: "initialises rootCmd app with invalid config and empty file returns an error",
@@ -207,6 +223,12 @@ func TestInitApp(t *testing.T) {
207223
if err := initApp(tt.args.cmd, tt.args.client, tt.args.v); !reflect.DeepEqual(err, tt.err) {
208224
t.Errorf("initApp() error = %v, wantErr %v", err, tt.err)
209225
}
226+
227+
if tt.args.config != nil {
228+
cfg := ecctl.Get().Config
229+
cfg.Client = nil
230+
assert.Equal(t, tt.wantConfig, cfg)
231+
}
210232
})
211233
}
212234
}

‎pkg/ecctl/app.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func NewApplication(c Config) (*App, error) {
5656
},
5757
SkipLogin: c.SkipLogin,
5858
ErrorDevice: c.ErrorDevice,
59+
UserAgent: c.UserAgent,
5960
})
6061
if err != nil {
6162
return nil, err

0 commit comments

Comments
 (0)