-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
47 lines (37 loc) · 1.35 KB
/
utils.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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Jackal Authors
// Package zoci contains functions for interacting with Jackal packages stored in OCI registries.
package zoci
import (
"errors"
"fmt"
"strings"
"github.com/defenseunicorns/pkg/helpers"
"github.com/racer159/jackal/src/types"
"oras.land/oras-go/v2/registry"
)
// ReferenceFromMetadata returns a reference for the given metadata.
func ReferenceFromMetadata(registryLocation string, metadata *types.JackalMetadata, build *types.JackalBuildData) (string, error) {
ver := metadata.Version
if len(ver) == 0 {
return "", errors.New("version is required for publishing")
}
if !strings.HasSuffix(registryLocation, "/") {
registryLocation = registryLocation + "/"
}
registryLocation = strings.TrimPrefix(registryLocation, helpers.OCIURLPrefix)
format := "%s%s:%s"
raw := fmt.Sprintf(format, registryLocation, metadata.Name, ver)
if build != nil && build.Flavor != "" {
raw = fmt.Sprintf("%s-%s", raw, build.Flavor)
}
ref, err := registry.ParseReference(raw)
if err != nil {
return "", fmt.Errorf("failed to parse %s: %w", raw, err)
}
return ref.String(), nil
}
// GetInitPackageURL returns the URL for the init package for the given version.
func GetInitPackageURL(version string) string {
return fmt.Sprintf("ghcr.io/racer159/packages/init:%s", version)
}