1
1
package scw
2
2
3
3
import (
4
+ "bytes"
5
+ "fmt"
4
6
"io/ioutil"
5
7
"os"
6
8
"path/filepath"
9
+ "text/template"
7
10
8
11
"github.com./scaleway/scaleway-sdk-go/internal/auth"
9
12
"github.com./scaleway/scaleway-sdk-go/internal/errors"
@@ -16,6 +19,41 @@ const (
16
19
defaultConfigPermission = 0600
17
20
)
18
21
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
+
19
57
type Config struct {
20
58
Profile `yaml:",inline"`
21
59
ActiveProfile * string `yaml:"active_profile,omitempty"`
@@ -149,13 +187,31 @@ func (c *Config) Save() error {
149
187
return c .SaveTo (GetConfigPath ())
150
188
}
151
189
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
+
152
208
// SaveTo will save the config to the given path. This action will
153
209
// overwrite the previous file when it exists.
154
210
func (c * Config ) SaveTo (path string ) error {
155
211
path = filepath .Clean (path )
156
212
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 ( )
159
215
if err != nil {
160
216
return err
161
217
}
0 commit comments