Skip to content

Commit 60b2eea

Browse files
committed
Add a commented configuration file
1 parent 7a70251 commit 60b2eea

File tree

2 files changed

+134
-2
lines changed

2 files changed

+134
-2
lines changed

scw/config.go

+67-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,50 @@ 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+
{{ if gt (len .Profiles) 0 }}{{ range $k,$v := .Profiles }}
51+
{{ $k }}:
52+
{{ if $v.AccessKey }}access_key: {{ $v.AccessKey }}{{ else }}# access_key: SCW11111111111111111{{ 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+
# organization_id: 11111111-1111-1111-1111-111111111111
61+
# default_zone: fr-par-1
62+
# default_region: fr-par
63+
{{ end }}
64+
`
65+
1966
type Config struct {
2067
Profile `yaml:",inline"`
2168
ActiveProfile *string `yaml:"active_profile,omitempty"`
@@ -149,13 +196,31 @@ func (c *Config) Save() error {
149196
return c.SaveTo(GetConfigPath())
150197
}
151198

199+
func (c *Config) ConfigFile() ([]byte, error) {
200+
tmpl, err := template.New("configuration").Parse(configFileTemplate)
201+
if err != nil {
202+
fmt.Print(err)
203+
return nil, err
204+
}
205+
206+
var buf bytes.Buffer
207+
err = tmpl.Execute(&buf, c)
208+
if err != nil {
209+
fmt.Println(err)
210+
return nil, err
211+
}
212+
fmt.Println(buf.String())
213+
214+
return buf.Bytes(), nil
215+
}
216+
152217
// SaveTo will save the config to the given path. This action will
153218
// overwrite the previous file when it exists.
154219
func (c *Config) SaveTo(path string) error {
155220
path = filepath.Clean(path)
156221

157-
// STEP 1: marshal config
158-
file, err := yaml.Marshal(c)
222+
// STEP 1: Render the configuration file as a file
223+
file, err := c.ConfigFile()
159224
if err != nil {
160225
return err
161226
}

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, newConfig)
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, newConfig)
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, newConfig)
587+
}

0 commit comments

Comments
 (0)