-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote.go
190 lines (170 loc) · 4.26 KB
/
remote.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
184
185
186
187
188
189
190
package remote
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"time"
"cloud.google.com/go/storage"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/google/go-github/v58/github"
"github.com/jszwec/s3fs"
"github.com/k1LoW/ghfs"
"github.com/k1LoW/go-github-client/v58/factory"
"github.com/mauri870/gcsfs"
"google.golang.org/api/option"
)
var globalHTTPClient = &http.Client{
Timeout: 30 * time.Second,
}
var globalGitHubClient *github.Client
var globalS3Client s3iface.S3API
var globalGCSClient *storage.Client
type clientSet struct {
httpClient *http.Client
githubClient *github.Client
s3Client s3iface.S3API
gcsClient *storage.Client
}
func newClientSet(opts []Option) *clientSet {
cs := &clientSet{
httpClient: globalHTTPClient,
githubClient: globalGitHubClient,
s3Client: globalS3Client,
gcsClient: globalGCSClient,
}
for _, opt := range opts {
opt(cs)
}
return cs
}
type Option func(*clientSet)
// HTTPClient set http.Client.
func HTTPClient(c *http.Client) Option {
return func(cs *clientSet) {
cs.httpClient = c
}
}
// GitHubClient set github.Client.
func GitHubClient(c *github.Client) Option {
return func(cs *clientSet) {
cs.githubClient = c
}
}
// S3Client set github.Client.
func S3Client(c s3iface.S3API) Option {
return func(cs *clientSet) {
cs.s3Client = c
}
}
// GCSClient set storage.Client.
func GCSClient(c *storage.Client) Option {
return func(cs *clientSet) {
cs.gcsClient = c
}
}
// Open remote file.
func Open(raw string, opts ...Option) (io.ReadCloser, error) {
u, err := url.Parse(raw)
if err != nil {
return nil, err
}
cs := newClientSet(opts)
switch u.Scheme {
case "http", "https":
return openHTTP(cs.httpClient, u)
case "github":
return openGitHub(cs.githubClient, raw)
case "s3":
return openS3(cs.s3Client, raw)
case "gs", "gcs":
return openGCS(cs.gcsClient, raw)
default:
p := strings.TrimPrefix(strings.TrimPrefix(raw, "file://"), "local://")
return os.Open(p)
}
}
// ReadAll remote file.
func ReadAll(raw string) ([]byte, error) {
r, err := Open(raw)
if err != nil {
return nil, err
}
defer r.Close()
return io.ReadAll(r)
}
func openHTTP(c *http.Client, u *url.URL) (io.ReadCloser, error) {
resp, err := c.Get(u.String())
if err != nil {
return nil, err
}
return resp.Body, nil
}
func openGitHub(c *github.Client, raw string) (io.ReadCloser, error) {
if c == nil && globalGitHubClient == nil {
// initialize globalGitHubClient
c, err := factory.NewGithubClient()
if err != nil {
return nil, fmt.Errorf("github client is not initialized: %w", err)
}
globalGitHubClient = c
}
splitted := strings.Split(strings.TrimPrefix(raw, "github://"), "/")
if len(splitted) < 3 {
return nil, fmt.Errorf("invalid github path: %s", raw)
}
owner := splitted[0]
repo := splitted[1]
name := strings.Join(splitted[2:], "/")
fsys, err := ghfs.New(owner, repo, ghfs.Client(c))
if err != nil {
return nil, err
}
return fsys.Open(name)
}
func openS3(c s3iface.S3API, raw string) (io.ReadCloser, error) {
if c == nil && globalS3Client == nil {
sess, err := session.NewSession()
if err != nil {
return nil, err
}
c = s3.New(sess)
globalS3Client = c
}
splitted := strings.Split(strings.TrimPrefix(raw, "s3://"), "/")
if len(splitted) < 2 {
return nil, fmt.Errorf("invalid s3 path: %s", raw)
}
bucket := splitted[0]
name := strings.Join(splitted[1:], "/")
fsys := s3fs.New(c, bucket)
return fsys.Open(name)
}
func openGCS(c *storage.Client, raw string) (io.ReadCloser, error) {
if c == nil && globalGCSClient == nil {
var err error
ctx := context.Background()
if os.Getenv("GOOGLE_APPLICATION_CREDENTIALS_JSON") != "" {
c, err = storage.NewClient(ctx, option.WithCredentialsJSON([]byte(os.Getenv("GOOGLE_APPLICATION_CREDENTIALS_JSON"))))
} else {
c, err = storage.NewClient(ctx)
}
if err != nil {
return nil, err
}
globalGCSClient = c
}
splitted := strings.Split(strings.TrimPrefix(strings.TrimPrefix(raw, "gs://"), "gcs://"), "/")
if len(splitted) < 2 {
return nil, fmt.Errorf("invalid gcs path: %s", raw)
}
bucket := splitted[0]
name := strings.Join(splitted[1:], "/")
fsys := gcsfs.NewWithClient(c, bucket)
return fsys.Open(name)
}