Skip to content

Commit 480a330

Browse files
committed
Add a commented configuration file
1 parent 7a70251 commit 480a330

File tree

2 files changed

+125
-2
lines changed

2 files changed

+125
-2
lines changed

scw/config.go

+58-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package scw
22

33
import (
4+
"bytes"
5+
"fmt"
46
"io/ioutil"
57
"os"
68
"path/filepath"
9+
"text/template"
710

811
"github.com./scaleway/scaleway-sdk-go/internal/auth"
912
"github.com./scaleway/scaleway-sdk-go/internal/errors"
@@ -16,6 +19,41 @@ const (
1619
defaultConfigPermission = 0600
1720
)
1821

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

190+
func (c *Config) ConfigFile() ([]byte, error) {
191+
tmpl, err := template.New("configuration").Parse(configFileTemplate)
192+
if err != nil {
193+
fmt.Print(err)
194+
return nil, err
195+
}
196+
197+
var buf bytes.Buffer
198+
err = tmpl.Execute(&buf, c.Profile)
199+
if err != nil {
200+
fmt.Println(err)
201+
return nil, err
202+
}
203+
fmt.Println(string(buf.Bytes()))
204+
205+
return buf.Bytes(), nil
206+
}
207+
152208
// SaveTo will save the config to the given path. This action will
153209
// overwrite the previous file when it exists.
154210
func (c *Config) SaveTo(path string) error {
155211
path = filepath.Clean(path)
156212

157-
// STEP 1: marshal config
158-
file, err := yaml.Marshal(c)
213+
// STEP 1: Render the configuration file as a file
214+
file, err := c.ConfigFile()
159215
if err != nil {
160216
return err
161217
}

scw/config_test.go

+67
Original file line numberDiff line numberDiff line change
@@ -518,3 +518,70 @@ secret_key: 6f6e6574-xxxx-xxxx-xxxx-xxxxxxxxxxxx
518518
testhelpers.Equals(t, v2ValidSecretKey2, *p.SecretKey)
519519

520520
}
521+
522+
func TestConfigFileWithFullProfile(t *testing.T) {
523+
var c = &Config{
524+
Profile: Profile{
525+
AccessKey: s(v2ValidAccessKey),
526+
SecretKey: s(v2ValidSecretKey),
527+
},
528+
ActiveProfile: s(v2ValidProfile),
529+
Profiles: map[string]*Profile{
530+
v2ValidProfile: {
531+
AccessKey: s(v2ValidAccessKey2),
532+
SecretKey: s(v2ValidSecretKey2),
533+
},
534+
},
535+
}
536+
537+
dump, err := c.ConfigFile()
538+
if err != nil {
539+
panic(err)
540+
}
541+
542+
var newConfig = &Config{}
543+
newConfig, err = unmarshalConfV2(dump)
544+
if err != nil {
545+
panic(err)
546+
}
547+
548+
testhelpers.Equals(t, c.Profile, newConfig.Profile)
549+
}
550+
551+
func TestConfigFileWithEmptyProfile(t *testing.T) {
552+
var c = &Config{}
553+
554+
dump, err := c.ConfigFile()
555+
if err != nil {
556+
panic(err)
557+
}
558+
559+
var newConfig = &Config{}
560+
newConfig, err = unmarshalConfV2(dump)
561+
if err != nil {
562+
panic(err)
563+
}
564+
565+
testhelpers.Equals(t, c.Profiles, newConfig.Profiles)
566+
}
567+
568+
func TestConfigFileWithPartialProfile(t *testing.T) {
569+
var c = &Config{
570+
Profile: Profile{
571+
AccessKey: s(v2ValidAccessKey),
572+
},
573+
}
574+
575+
dump, err := c.ConfigFile()
576+
if err != nil {
577+
panic(err)
578+
}
579+
580+
var newConfig = &Config{}
581+
newConfig, err = unmarshalConfV2(dump)
582+
if err != nil {
583+
panic(err)
584+
}
585+
586+
testhelpers.Equals(t, c.Profiles, newConfig.Profiles)
587+
}

0 commit comments

Comments
 (0)