/
repository.go
146 lines (127 loc) · 2.74 KB
/
repository.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
package repository
import (
neturl "net/url"
"os"
"path/filepath"
"strings"
)
type FileInfo interface {
Name() string
IsDir() bool
}
type SimpleFileInfo struct {
name string
isDir bool
}
func (fi *SimpleFileInfo) Name() string {
return fi.name
}
func (fi *SimpleFileInfo) IsDir() bool {
return fi.isDir
}
type Repository interface {
Upload(localPath, repoPath string, meter *Meter) error
Download(repoPath, localPath string, meter *Meter) error
Delete(repoPath string) error
Stat(repoPath string) (FileInfo, error)
List(repoPath string) ([]FileInfo, error)
}
type RepoParseResult struct {
Repo string
scheme string
host string
path string
}
func ParseRepo(repo string) (RepoParseResult, error) {
var result RepoParseResult
if strings.Contains(repo, "://") {
url, err := neturl.Parse(repo)
if err != nil {
return result, err
}
if url.Scheme == "" {
return result, UnsupportedRepositoryError{
Message: "unsupported repository. Relative path is not allowed as a repository path",
}
}
result.Repo = repo
result.scheme = url.Scheme
result.host = url.Host
result.path = url.Path
} else {
i := strings.Index(repo, ":")
if i > 0 {
result.Repo = repo
result.scheme = "ssh"
result.host = repo[0:i]
result.path = repo[i+1:]
} else {
cwd, err := os.Getwd()
if err != nil {
return result, err
}
if !strings.HasPrefix(repo, "/") {
repo, err = filepath.Abs(filepath.Join(cwd, repo))
if err != nil {
return result, err
}
}
result.Repo = repo
result.scheme = "file"
result.host = ""
result.path = repo
}
}
return result, nil
}
func ParseRepoName(result RepoParseResult) (string, error) {
if result.scheme == "ssh" {
name := filepath.Base(result.path)
if name == "/" {
return result.host, nil
}
return name, nil
} else {
url, err := neturl.Parse(result.Repo)
if err != nil {
return "", err
}
if url.Path == "" {
return url.Hostname(), nil
}
name := filepath.Base(url.Path)
if name == "/" {
return url.Hostname(), nil
}
return name, nil
}
}
func NewRepository(result RepoParseResult) (Repository, error) {
repo := result.Repo
host := result.host
path := result.path
switch result.scheme {
case "file":
return NewLocalFileSystemRepository(path)
case "s3":
return NewS3Repository(host, path)
case "gs":
return NewGCSRepository(host, path)
case "rclone":
return NewRcloneRepository(host, path)
case "ssh":
return NewSSHRepository(host, path)
case "http":
return NewHttpRepository(repo)
case "https":
if IsAzureStorageUrl(repo) {
return NewAzureBlobRepository(repo)
} else {
return NewHttpRepository(repo)
}
default:
return nil, UnsupportedRepositoryError{
Message: "unsupported repository",
}
}
}