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,50 @@ 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
+ {{ 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
+
19
66
type Config struct {
20
67
Profile `yaml:",inline"`
21
68
ActiveProfile * string `yaml:"active_profile,omitempty"`
@@ -149,13 +196,31 @@ func (c *Config) Save() error {
149
196
return c .SaveTo (GetConfigPath ())
150
197
}
151
198
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
+
152
217
// SaveTo will save the config to the given path. This action will
153
218
// overwrite the previous file when it exists.
154
219
func (c * Config ) SaveTo (path string ) error {
155
220
path = filepath .Clean (path )
156
221
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 ( )
159
224
if err != nil {
160
225
return err
161
226
}
0 commit comments