Repository navigation
Expand file tree
/
Copy pathdocker.go
More file actions
101 lines (86 loc) · 2.36 KB
/
Copy pathdocker.go
File metadata and controls
101 lines (86 loc) · 2.36 KB
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
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
type DockerError struct {
Code string `json:"code"`
Message string `json:"message,omitempty"`
Detail interface{} `json:"detail,omitempty"`
}
type DockerAuthError struct {
DockerError
AuthRequest WwwAuthRequest
}
type DockerResponse struct {
Name string `json:"name,omitempty"`
DockerContentDigest string
Tags []string `json:"tags,omitempty"`
Repositories []string `json:"repositories,omitempty"`
Errors []DockerError `json:"errors,omitempty"`
}
func (e DockerError) Error() string {
return e.Message
}
func (e DockerAuthError) Error() string {
return e.Message
}
func callDockerRepo(method, url, authHeader string, headers map[string]string) (*DockerResponse, error) {
client := &http.Client{}
req, err := http.NewRequest(method, url, nil)
if err != nil {
fmt.Println("Error creating new request:", err)
return nil, fmt.Errorf("error fetching tags: %s", err)
}
req.Header.Add("Authorization", authHeader)
for key, val := range headers {
req.Header.Add(key, val)
}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error fetching tags:", err)
return nil, fmt.Errorf("error fetching tags: %s", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response body:", err)
return nil, fmt.Errorf("error reading response body: %s", err)
}
if resp.Header.Get("Www-Authenticate") != "" {
wwwAuth := resp.Header.Get("Www-Authenticate")
if wwwAuth != "" {
wwwAuth, err := parseWwwAuthenticateHeader(wwwAuth)
if err == nil {
return nil, &DockerAuthError{
AuthRequest: wwwAuth,
}
}
}
}
var response DockerResponse
if len(body) != 0 {
err = json.Unmarshal(body, &response)
if err != nil {
fmt.Println("Error parsing JSON response:", err)
return nil, fmt.Errorf("error parsing JSON response: %s", err)
}
}
response.DockerContentDigest = resp.Header.Get("Docker-Content-Digest")
if len(response.Errors) > 0 {
wwwAuth := resp.Header.Get("Www-Authenticate")
if wwwAuth != "" {
wwwAuth, err := parseWwwAuthenticateHeader(wwwAuth)
if err == nil {
return nil, &DockerAuthError{
DockerError: response.Errors[0],
AuthRequest: wwwAuth,
}
}
}
return nil, response.Errors[0]
}
return &response, nil
}