Skip to content

Commit ed2c77b

Browse files
author
Arthur Amstutz
committed
feat: Add ability to use go-ovh in a WebAssembly binary
Signed-off-by: Arthur Amstutz <[email protected]>
1 parent e02aa17 commit ed2c77b

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

ovh/configuration.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"os"
88
"os/user"
9+
"runtime"
910
"strings"
1011

1112
"golang.org/x/oauth2/clientcredentials"
@@ -66,6 +67,12 @@ func expandConfigPaths() []interface{} {
6667
// loadINI builds a ini.File from the configuration paths provided in configPaths.
6768
// It's a helper for loadConfig.
6869
func loadINI() (*ini.File, error) {
70+
// Don't try to load configuration from the
71+
// filesystem when compiling for WebAssembly
72+
if runtime.GOARCH == "wasm" && runtime.GOOS == "js" {
73+
return ini.Empty(), nil
74+
}
75+
6976
paths := expandConfigPaths()
7077
if len(paths) == 0 {
7178
return ini.Empty(), nil
@@ -195,12 +202,20 @@ func getConfigValue(cfg *ini.File, section, name, def string) string {
195202
return fromEnv
196203
}
197204

205+
if !cfg.HasSection(section) {
206+
return def
207+
}
208+
198209
// Attempt to load from configuration
199210
fromSection := cfg.Section(section)
200211
if fromSection == nil {
201212
return def
202213
}
203214

215+
if !fromSection.HasKey(name) {
216+
return def
217+
}
218+
204219
fromSectionKey := fromSection.Key(name)
205220
if fromSectionKey == nil {
206221
return def

ovh/configuration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func TestMissingParam(t *testing.T) {
143143

144144
client.endpoint = ""
145145
err := client.loadConfig("")
146-
td.CmpString(t, err, `unknown endpoint '', consider checking 'Endpoints' list or using an URL`)
146+
td.CmpNoError(t, err)
147147

148148
client.AppKey = ""
149149
err = client.loadConfig("ovh-eu")

ovh/ovh_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -401,10 +401,10 @@ func TestGetResponseUnmarshalNumber(t *testing.T) {
401401
func TestConstructors(t *testing.T) {
402402
assert, require := td.AssertRequire(t)
403403

404-
// Error: missing Endpoint
404+
// Missing Endpoint, defaulting to ovh-eu
405405
client, err := NewClient("", MockApplicationKey, MockApplicationSecret, MockConsumerKey)
406-
assert.Nil(client)
407-
assert.String(err, `unknown endpoint '', consider checking 'Endpoints' list or using an URL`)
406+
assert.NotNil(client)
407+
assert.CmpNoError(err)
408408

409409
// Error: missing ApplicationKey
410410
client, err = NewClient("ovh-eu", "", MockApplicationSecret, MockConsumerKey)
@@ -449,10 +449,10 @@ func TestConstructors(t *testing.T) {
449449
func TestConstructorsOAuth2(t *testing.T) {
450450
assert, require := td.AssertRequire(t)
451451

452-
// Error: missing Endpoint
452+
// Missing Endpoint: defaulting to ovh-eu
453453
client, err := NewOAuth2Client("", "aaaaaaaa", "bbbbbbbbbbbbbbbbbbbbbbbbbbbb")
454-
assert.Nil(client)
455-
assert.String(err, `unknown endpoint '', consider checking 'Endpoints' list or using an URL`)
454+
assert.NotNil(client)
455+
assert.CmpNoError(err)
456456

457457
// Error: missing Client ID
458458
client, err = NewOAuth2Client("ovh-eu", "", "MockApplicationSecret")
@@ -489,10 +489,10 @@ func TestConstructorsOAuth2(t *testing.T) {
489489
func TestConstructorsAccessToken(t *testing.T) {
490490
assert, require := td.AssertRequire(t)
491491

492-
// Error: missing Endpoint
492+
// Missing Endpoint: defaulting to ovh-eu
493493
client, err := NewAccessTokenClient("", "aaaaaaaa")
494-
assert.Nil(client)
495-
assert.String(err, `unknown endpoint '', consider checking 'Endpoints' list or using an URL`)
494+
assert.NotNil(client)
495+
assert.CmpNoError(err)
496496

497497
// Next: success cases
498498
expected := td.Struct(&Client{

0 commit comments

Comments
 (0)