@@ -5,40 +5,87 @@ import (
5
5
"io"
6
6
"log"
7
7
"net/url"
8
+ "os"
8
9
"strings"
9
10
10
11
oci "github.com/PingCAP-QE/ee-apps/dl/gen/oci"
11
12
pkgoci "github.com/PingCAP-QE/ee-apps/dl/pkg/oci"
13
+ "gopkg.in/yaml.v3"
14
+ "oras.land/oras-go/v2/registry/remote"
15
+ "oras.land/oras-go/v2/registry/remote/auth"
16
+ "oras.land/oras-go/v2/registry/remote/retry"
12
17
)
13
18
14
19
// oci service example implementation.
15
20
// The example methods log the requests and return zero values.
16
21
type ocisrvc struct {
17
- logger * log.Logger
22
+ logger * log.Logger
23
+ credential * auth.Credential
18
24
}
19
25
20
26
// NewOci returns the oci service implementation.
21
- func NewOci (logger * log.Logger ) oci.Service {
22
- return & ocisrvc {logger }
27
+ func NewOci (logger * log.Logger , cfgFile * string ) oci.Service {
28
+ var cfg pkgoci.Config
29
+ if cfgFile == nil {
30
+ return & ocisrvc {logger : logger , credential : & auth .EmptyCredential }
31
+ }
32
+
33
+ cfgBytes , err := os .ReadFile (* cfgFile )
34
+ if err != nil {
35
+ logger .Fatalf ("Failed to load configuration: %v" , err )
36
+ }
37
+ if err := yaml .Unmarshal (cfgBytes , & cfg ); err != nil {
38
+ logger .Fatalf ("Failed to load configuration: %v" , err )
39
+ }
40
+
41
+ return & ocisrvc {logger : logger , credential : & auth.Credential {
42
+ Username : cfg .Username ,
43
+ Password : cfg .Password ,
44
+ }}
23
45
}
24
46
25
47
// ListFiles implements list-files.
26
48
func (s * ocisrvc ) ListFiles (ctx context.Context , p * oci.ListFilesPayload ) (res []string , err error ) {
27
49
s .logger .Print ("oci.list-files" )
28
50
29
- files , err := pkgoci .ListFiles (ctx , p .Repository , p .Tag )
51
+ repository , err := s .getTargetRepo (p .Repository )
52
+ if err != nil {
53
+ return nil , err
54
+ }
55
+
56
+ files , err := pkgoci .ListFiles (ctx , repository , p .Tag )
30
57
if err != nil {
31
58
return nil , oci .MakeInvalidFilePath (err )
32
59
}
33
60
34
61
return files , nil
35
62
}
36
63
64
+ func (s * ocisrvc ) getTargetRepo (repo string ) (* remote.Repository , error ) {
65
+ repository , err := remote .NewRepository (repo )
66
+ if err != nil {
67
+ return nil , err
68
+ }
69
+
70
+ reg := strings .SplitN (repo , "/" , 2 )[0 ]
71
+ repository .Client = & auth.Client {
72
+ Client : retry .DefaultClient ,
73
+ Cache : auth .DefaultCache ,
74
+ Credential : auth .StaticCredential (reg , * s .credential ),
75
+ }
76
+
77
+ return repository , nil
78
+ }
79
+
37
80
// DownloadFile implements download-file.
38
81
func (s * ocisrvc ) DownloadFile (ctx context.Context , p * oci.DownloadFilePayload ) (res * oci.DownloadFileResult , resp io.ReadCloser , err error ) {
39
82
s .logger .Print ("oci.download-files" )
40
83
41
- rc , length , err := pkgoci .NewFileReadCloser (ctx , p .Repository , p .Tag , p .File )
84
+ repository , err := s .getTargetRepo (p .Repository )
85
+ if err != nil {
86
+ return nil , nil , err
87
+ }
88
+ rc , length , err := pkgoci .NewFileReadCloser (ctx , repository , p .Tag , p .File )
42
89
if err != nil {
43
90
return nil , nil , err
44
91
}
@@ -54,7 +101,11 @@ func (s *ocisrvc) DownloadFile(ctx context.Context, p *oci.DownloadFilePayload)
54
101
func (s * ocisrvc ) DownloadFileSha256 (ctx context.Context , p * oci.DownloadFileSha256Payload ) (res * oci.DownloadFileSha256Result , resp io.ReadCloser , err error ) {
55
102
s .logger .Print ("oci.download-file-sha256" )
56
103
57
- value , err := pkgoci .GetFileSHA256 (ctx , p .Repository , p .Tag , p .File )
104
+ repository , err := s .getTargetRepo (p .Repository )
105
+ if err != nil {
106
+ return nil , nil , err
107
+ }
108
+ value , err := pkgoci .GetFileSHA256 (ctx , repository , p .Tag , p .File )
58
109
if err != nil {
59
110
return nil , nil , err
60
111
}
0 commit comments