Extract the custom endpoint and the bucket name from the location string The location string can be either a bucket name or a URL i.e bucket-name or https://custom-domain/bucket-name
(creds *Credentials)
| 114 | // The location string can be either a bucket name or a URL |
| 115 | // i.e bucket-name or https://custom-domain/bucket-name |
| 116 | func extractLocationAndBucket(creds *Credentials) (string, string, error) { |
| 117 | // Older versions of the credentials didn't have the location field |
| 118 | // and just the bucket name was stored in the bucket name field |
| 119 | if creds.BucketName != "" { |
| 120 | return "", creds.BucketName, nil |
| 121 | } |
| 122 | |
| 123 | // Newer versions of the credentials have the location field which can contain the endpoint |
| 124 | // so we override the bucket and set the endpoint if needed |
| 125 | parsedLocation, err := url.Parse(creds.Location) |
| 126 | if err != nil { |
| 127 | return "", "", fmt.Errorf("failed to parse location: %w", err) |
| 128 | } |
| 129 | |
| 130 | host := parsedLocation.Host |
| 131 | // It's a bucket name |
| 132 | if host == "" { |
| 133 | return "", creds.Location, nil |
| 134 | } |
| 135 | |
| 136 | endpoint := fmt.Sprintf("%s://%s", parsedLocation.Scheme, host) |
| 137 | // It's a URL, extract bucket name from the path |
| 138 | if pathSegments := strings.Split(parsedLocation.Path, "/"); len(pathSegments) > 1 { |
| 139 | return endpoint, pathSegments[1], nil |
| 140 | } |
| 141 | |
| 142 | return "", "", fmt.Errorf("the location doesn't contain a bucket name") |
| 143 | } |
| 144 | |
| 145 | // Exists check that the artifact is already present in the repository |
| 146 | func (b *Backend) Exists(ctx context.Context, digest string) (bool, error) { |