Skip to content

Commit 496fd26

Browse files
committed
Add a commented configuration file
1 parent 45f3763 commit 496fd26

File tree

2 files changed

+230
-5
lines changed

2 files changed

+230
-5
lines changed

scw/config.go

+67-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package scw
22

33
import (
4+
"bytes"
45
"io/ioutil"
56
"os"
67
"path/filepath"
8+
"text/template"
79

810
"github.com/scaleway/scaleway-sdk-go/internal/auth"
911
"github.com/scaleway/scaleway-sdk-go/internal/errors"
@@ -16,6 +18,52 @@ const (
1618
defaultConfigPermission = 0600
1719
)
1820

21+
const configFileTemplate = `# Scaleway configuration file
22+
23+
# You need an access key and a secret key to connect to Scaleway API.
24+
# Generate your token at the following address: https://console.scaleway.com/account/credentials
25+
26+
# An access key is a secret key identifier.
27+
{{ if .AccessKey }}access_key: {{.AccessKey}}{{ else }}# access_key: SCW11111111111111111{{ end }}
28+
29+
# The secret key is the value that can be used to authenticate against the API (the value used in X-Auth-Token HTTP-header).
30+
# The secret key MUST stay secret and not given to anyone or published online.
31+
{{ if .SecretKey }}secret_key: {{ .SecretKey }}{{ else }}# secret_key: 11111111-1111-1111-1111-111111111111{{ end }}
32+
33+
# Your organization ID is the identifier of your account inside Scaleway infrastructure.
34+
{{ if .DefaultOrganizationID }}default_organization_id: {{ .DefaultOrganizationID }}{{ else }}# default_organization_id: 11111111-1111-1111-1111-111111111111{{ end }}
35+
36+
# A region is represented as a geographical area such as France (Paris) or the Netherlands (Amsterdam).
37+
# It can contain multiple Availability Zones.
38+
# Example of region: fr-par, nl-ams
39+
{{ if .DefaultRegion }}default_region: {{ .DefaultRegion }}{{ else }}# default_region: fr-par{{ end }}
40+
41+
# A region can be split into many availability zones (AZ).
42+
# Latency between multiple AZ of the same region are low as they have a common network layer.
43+
# Example of zones: fr-par-1, nl-ams-1
44+
{{ if .DefaultZone }}default_zone: {{.DefaultZone}}{{ else }}# default_zone: fr-par-1{{ end }}
45+
46+
# Scaleway CLI also supports profiles that will have a different set of credentials.
47+
# You can define a CLI profile using the following syntax:
48+
{{ if gt (len .Profiles) 0 }}
49+
{{- range $k,$v := .Profiles }}
50+
{{ $k }}:
51+
{{ if $v.AccessKey }}access_key: {{ $v.AccessKey }}{{ else }}# access_key: SCW11111111111111111{{ end }}
52+
{{ if $v.SecretKey }}secret_key: {{ $v.SecretKey }}{{ else }}# secret_key: 11111111-1111-1111-1111-111111111111{{ end }}
53+
{{ if $v.DefaultOrganizationID }}default_organization_id: {{ $v.DefaultOrganizationID }}{{ else }}# default_organization_id: 11111111-1111-1111-1111-111111111111{{ end }}
54+
{{ if $v.DefaultZone }}default_zone: {{ $v.DefaultZone }}{{ else }}# default_zone: fr-par-1{{ end }}
55+
{{ if $v.DefaultRegion }}default_region: {{ $v.DefaultRegion }}{{ else }}# default_region: fr-par{{ end }}
56+
{{ end }}
57+
{{- else }}
58+
# myProfile:
59+
# access_key: 11111111-1111-1111-1111-111111111111
60+
# secret_key: 11111111-1111-1111-1111-111111111111
61+
# organization_id: 11111111-1111-1111-1111-111111111111
62+
# default_zone: fr-par-1
63+
# default_region: fr-par
64+
{{ end -}}
65+
`
66+
1967
type Config struct {
2068
Profile `yaml:",inline"`
2169
ActiveProfile *string `yaml:"active_profile,omitempty"`
@@ -150,13 +198,29 @@ func (c *Config) Save() error {
150198
return c.SaveTo(GetConfigPath())
151199
}
152200

201+
// HumanConfig will generate a config file with documented arguments.
202+
func (c *Config) HumanConfig() (string, error) {
203+
tmpl, err := template.New("configuration").Parse(configFileTemplate)
204+
if err != nil {
205+
return "", err
206+
}
207+
208+
var buf bytes.Buffer
209+
err = tmpl.Execute(&buf, c)
210+
if err != nil {
211+
return "", err
212+
}
213+
214+
return buf.String(), nil
215+
}
216+
153217
// SaveTo will save the config to the given path. This action will
154218
// overwrite the previous file when it exists.
155219
func (c *Config) SaveTo(path string) error {
156220
path = filepath.Clean(path)
157221

158-
// STEP 1: marshal config
159-
file, err := yaml.Marshal(c)
222+
// STEP 1: Render the configuration file as a file
223+
file, err := c.HumanConfig()
160224
if err != nil {
161225
return err
162226
}
@@ -168,7 +232,7 @@ func (c *Config) SaveTo(path string) error {
168232
}
169233

170234
// STEP 3: write new config file
171-
err = ioutil.WriteFile(path, file, defaultConfigPermission)
235+
err = ioutil.WriteFile(path, []byte(file), defaultConfigPermission)
172236
if err != nil {
173237
return err
174238
}

scw/config_test.go

+163-2
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,12 @@ func TestSaveConfig(t *testing.T) {
228228
testhelpers.AssertNoError(t, test.config.Save())
229229

230230
// test expected files
231-
for fileName, expectedContent := range test.expectedFiles {
231+
for fileName := range test.expectedFiles {
232+
expectedContent, err := test.config.HumanConfig()
233+
testhelpers.AssertNoError(t, err)
232234
content, err := ioutil.ReadFile(filepath.Join(dir, fileName))
233235
testhelpers.AssertNoError(t, err)
234-
testhelpers.Equals(t, expectedContent, "\n"+string(content))
236+
testhelpers.Equals(t, expectedContent, string(content))
235237
}
236238

237239
})
@@ -580,3 +582,162 @@ func z(value Zone) *Zone {
580582
func b(value bool) *bool {
581583
return &value
582584
}
585+
586+
func TestConfig_ConfigFile(t *testing.T) {
587+
588+
type testCase struct {
589+
config *Config
590+
result string
591+
}
592+
593+
run := func(c *testCase) func(t *testing.T) {
594+
return func(t *testing.T) {
595+
config, err := c.config.HumanConfig()
596+
testhelpers.AssertNoError(t, err)
597+
testhelpers.Equals(t, c.result, config)
598+
}
599+
}
600+
601+
t.Run("empty", run(&testCase{
602+
config: &Config{},
603+
result: `# Scaleway configuration file
604+
605+
# You need an access key and a secret key to connect to Scaleway API.
606+
# Generate your token at the following address: https://console.scaleway.com/account/credentials
607+
608+
# An access key is a secret key identifier.
609+
# access_key: SCW11111111111111111
610+
611+
# The secret key is the value that can be used to authenticate against the API (the value used in X-Auth-Token HTTP-header).
612+
# The secret key MUST stay secret and not given to anyone or published online.
613+
# secret_key: 11111111-1111-1111-1111-111111111111
614+
615+
# Your organization ID is the identifier of your account inside Scaleway infrastructure.
616+
# default_organization_id: 11111111-1111-1111-1111-111111111111
617+
618+
# A region is represented as a geographical area such as France (Paris) or the Netherlands (Amsterdam).
619+
# It can contain multiple Availability Zones.
620+
# Example of region: fr-par, nl-ams
621+
# default_region: fr-par
622+
623+
# A region can be split into many availability zones (AZ).
624+
# Latency between multiple AZ of the same region are low as they have a common network layer.
625+
# Example of zones: fr-par-1, nl-ams-1
626+
# default_zone: fr-par-1
627+
628+
# Scaleway CLI also supports profiles that will have a different set of credentials.
629+
# You can define a CLI profile using the following syntax:
630+
631+
# myProfile:
632+
# access_key: 11111111-1111-1111-1111-111111111111
633+
# secret_key: 11111111-1111-1111-1111-111111111111
634+
# organization_id: 11111111-1111-1111-1111-111111111111
635+
# default_zone: fr-par-1
636+
# default_region: fr-par
637+
`,
638+
}))
639+
640+
t.Run("partial", run(&testCase{
641+
config: &Config{
642+
Profile: Profile{
643+
AccessKey: s(v2ValidAccessKey),
644+
}},
645+
result: `# Scaleway configuration file
646+
647+
# You need an access key and a secret key to connect to Scaleway API.
648+
# Generate your token at the following address: https://console.scaleway.com/account/credentials
649+
650+
# An access key is a secret key identifier.
651+
access_key: ACCESS_KEY
652+
653+
# The secret key is the value that can be used to authenticate against the API (the value used in X-Auth-Token HTTP-header).
654+
# The secret key MUST stay secret and not given to anyone or published online.
655+
# secret_key: 11111111-1111-1111-1111-111111111111
656+
657+
# Your organization ID is the identifier of your account inside Scaleway infrastructure.
658+
# default_organization_id: 11111111-1111-1111-1111-111111111111
659+
660+
# A region is represented as a geographical area such as France (Paris) or the Netherlands (Amsterdam).
661+
# It can contain multiple Availability Zones.
662+
# Example of region: fr-par, nl-ams
663+
# default_region: fr-par
664+
665+
# A region can be split into many availability zones (AZ).
666+
# Latency between multiple AZ of the same region are low as they have a common network layer.
667+
# Example of zones: fr-par-1, nl-ams-1
668+
# default_zone: fr-par-1
669+
670+
# Scaleway CLI also supports profiles that will have a different set of credentials.
671+
# You can define a CLI profile using the following syntax:
672+
673+
# myProfile:
674+
# access_key: 11111111-1111-1111-1111-111111111111
675+
# secret_key: 11111111-1111-1111-1111-111111111111
676+
# organization_id: 11111111-1111-1111-1111-111111111111
677+
# default_zone: fr-par-1
678+
# default_region: fr-par
679+
`,
680+
}))
681+
682+
t.Run("full", run(&testCase{
683+
config: &Config{
684+
Profile: Profile{
685+
AccessKey: s(v2ValidAccessKey),
686+
SecretKey: s(v2ValidSecretKey),
687+
},
688+
ActiveProfile: s(v2ValidProfile),
689+
Profiles: map[string]*Profile{
690+
"profile1": {
691+
AccessKey: s(v2ValidAccessKey2),
692+
SecretKey: s(v2ValidSecretKey2),
693+
},
694+
"profile2": {
695+
AccessKey: s(v2ValidAccessKey2),
696+
SecretKey: s(v2ValidSecretKey2),
697+
},
698+
},
699+
},
700+
result: `# Scaleway configuration file
701+
702+
# You need an access key and a secret key to connect to Scaleway API.
703+
# Generate your token at the following address: https://console.scaleway.com/account/credentials
704+
705+
# An access key is a secret key identifier.
706+
access_key: ACCESS_KEY
707+
708+
# The secret key is the value that can be used to authenticate against the API (the value used in X-Auth-Token HTTP-header).
709+
# The secret key MUST stay secret and not given to anyone or published online.
710+
secret_key: 7363616c-6577-6573-6862-6f7579616161
711+
712+
# Your organization ID is the identifier of your account inside Scaleway infrastructure.
713+
# default_organization_id: 11111111-1111-1111-1111-111111111111
714+
715+
# A region is represented as a geographical area such as France (Paris) or the Netherlands (Amsterdam).
716+
# It can contain multiple Availability Zones.
717+
# Example of region: fr-par, nl-ams
718+
# default_region: fr-par
719+
720+
# A region can be split into many availability zones (AZ).
721+
# Latency between multiple AZ of the same region are low as they have a common network layer.
722+
# Example of zones: fr-par-1, nl-ams-1
723+
# default_zone: fr-par-1
724+
725+
# Scaleway CLI also supports profiles that will have a different set of credentials.
726+
# You can define a CLI profile using the following syntax:
727+
728+
profile1:
729+
access_key: ACCESS_KEY2
730+
secret_key: 6f6e6574-6f72-756c-6c74-68656d616c6c
731+
# default_organization_id: 11111111-1111-1111-1111-111111111111
732+
# default_zone: fr-par-1
733+
# default_region: fr-par
734+
735+
profile2:
736+
access_key: ACCESS_KEY2
737+
secret_key: 6f6e6574-6f72-756c-6c74-68656d616c6c
738+
# default_organization_id: 11111111-1111-1111-1111-111111111111
739+
# default_zone: fr-par-1
740+
# default_region: fr-par
741+
`,
742+
}))
743+
}

0 commit comments

Comments
 (0)