-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy paths3configfile.go
183 lines (158 loc) · 3.92 KB
/
s3configfile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//go:build darwin || linux || windows
package object
import (
"bytes"
"context"
"encoding/json"
"errors"
"path"
"text/template"
"github.com/scaleway/scaleway-cli/v2/core"
"github.com/scaleway/scaleway-sdk-go/scw"
)
type s3tool string
func (c s3tool) String() string {
return string(c)
}
type supportedTool []s3tool
func (s supportedTool) ToStringArray() []string {
res := make([]string, 0, len(s))
for _, x := range s {
res = append(res, x.String())
}
return res
}
type s3config struct {
AccessKey string
SecretKey string
Region scw.Region
Name string
ctx context.Context
ProjectID string
}
const (
rclone = s3tool("rclone")
s3cmd = s3tool("s3cmd")
mc = s3tool("mc")
)
var supportedTools = supportedTool{
rclone,
s3cmd,
mc,
}
const s3cmdTemplate = `# Generated by scaleway-cli command
# Configuration file for s3cmd https://s3tools.org/s3cmd
# Default location: $HOME/.s3cfg
[default]
access_key = {{ .AccessKey }}
bucket_location = {{ .Region }}
host_base = s3.{{ .Region }}.scw.cloud
host_bucket = %(bucket)s.s3.{{ .Region }}.scw.cloud
secret_key = {{ .SecretKey }}
use_https = True
`
const rcloneTemplate = `# Generated by scaleway-cli command
# Configuration file for rclone https://rclone.org/s3/#scaleway
# Default location: $HOME/.config/rclone/rclone.conf
[{{ .Name }}]
type = s3
provider = Scaleway
env_auth = false
endpoint = s3.{{ .Region }}.scw.cloud
access_key_id = {{ .AccessKey }}@{{ .ProjectID }}
secret_access_key = {{ .SecretKey }}
region = {{ .Region }}
location_constraint =
acl = private
force_path_style = false
server_side_encryption =
storage_class =
`
func newS3Config(ctx context.Context, region scw.Region, name string) (s3config, error) {
client := core.ExtractClient(ctx)
accessKey, accessExists := client.GetAccessKey()
if !accessExists {
return s3config{}, errors.New("no access key found")
}
secretKey, secretExists := client.GetSecretKey()
if !secretExists {
return s3config{}, errors.New("no secret key found")
}
projectID, exists := client.GetDefaultProjectID()
if !exists {
return s3config{}, errors.New("no project ID found")
}
if projectID != "" {
accessKey+="@"+projectID
}
config := s3config{
AccessKey: accessKey,
SecretKey: secretKey,
Region: region,
Name: name,
ctx: ctx,
}
return config, nil
}
func (c s3config) getPath(tool s3tool) (string, error) {
homeDir := core.ExtractUserHomeDir(c.ctx)
switch tool {
case s3cmd:
return path.Join(homeDir, ".s3cfg"), nil
case rclone:
return path.Join(homeDir, ".config", "rclone", "rclone.conf"), nil
case mc:
return path.Join(homeDir, ".mc", "config.json"), nil
default:
return "", errors.New("unknown tool")
}
}
func (c s3config) renderTemplate(configFileTemplate string) (core.RawResult, error) {
tmpl, err := template.New("configuration").Parse(configFileTemplate)
if err != nil {
return nil, err
}
var buf bytes.Buffer
err = tmpl.Execute(&buf, c)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (c s3config) getConfigFile(tool s3tool) (core.RawResult, error) {
switch tool {
case s3cmd:
return c.renderTemplate(s3cmdTemplate)
case rclone:
return c.renderTemplate(rcloneTemplate)
case mc:
type hostconfig struct {
URL string `json:"url"`
AccessKey string `json:"accessKey"`
SecretKey string `json:"secretKey"`
API string `json:"api"`
ProjectID string `json:"projectID"`
}
m := struct {
Version string `json:"version"`
Hosts map[string]hostconfig `json:"hosts"`
}{
Version: "9",
Hosts: map[string]hostconfig{
c.Name: {
URL: "https://s3." + c.Region.String() + ".scw.cloud",
AccessKey: c.AccessKey + "@" + c.ProjectID,
SecretKey: c.SecretKey,
API: "S3v4",
},
},
}
res, err := json.Marshal(m)
if err != nil {
return nil, err
}
return append(res, '\n'), nil
default:
return nil, errors.New("unknown tool")
}
}