|
| 1 | +package ocm_cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +type ComponentGetter struct { |
| 10 | + // Location of the root component in the format <repo>//<component>:<version>. |
| 11 | + rootComponentLocation string |
| 12 | + // Path to the deployment templates resource in the format <componentRef1>/.../<componentRefN>/<resourceName>. |
| 13 | + deploymentTemplates string |
| 14 | + ocmConfig string |
| 15 | + |
| 16 | + // Fields derived during InitializeComponents |
| 17 | + rootComponentVersion *ComponentVersion |
| 18 | + templatesComponentVersion *ComponentVersion |
| 19 | + templatesComponentLocation string |
| 20 | + templatesResourceName string |
| 21 | +} |
| 22 | + |
| 23 | +func NewComponentGetter(rootComponentLocation, deploymentTemplates, ocmConfig string) *ComponentGetter { |
| 24 | + return &ComponentGetter{ |
| 25 | + rootComponentLocation: rootComponentLocation, |
| 26 | + deploymentTemplates: deploymentTemplates, |
| 27 | + ocmConfig: ocmConfig, |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func (g *ComponentGetter) InitializeComponents(ctx context.Context) error { |
| 32 | + repo, err := extractRepoFromLocation(g.rootComponentLocation) |
| 33 | + if err != nil { |
| 34 | + return err |
| 35 | + } |
| 36 | + |
| 37 | + rootComponentVersion, err := GetComponentVersion(ctx, g.rootComponentLocation, g.ocmConfig) |
| 38 | + if err != nil { |
| 39 | + return fmt.Errorf("error getting root component version %s: %w", g.rootComponentLocation, err) |
| 40 | + } |
| 41 | + g.rootComponentVersion = rootComponentVersion |
| 42 | + |
| 43 | + g.deploymentTemplates = strings.TrimSpace(g.deploymentTemplates) |
| 44 | + segments := strings.Split(g.deploymentTemplates, "/") |
| 45 | + if len(segments) == 0 { |
| 46 | + return fmt.Errorf("deploymentTemplates path must contain a resource name or component references and a resource name separated by slashes (ref1/.../refN/resource): %s", g.deploymentTemplates) |
| 47 | + } |
| 48 | + referenceNames := segments[:len(segments)-1] |
| 49 | + g.templatesResourceName = segments[len(segments)-1] |
| 50 | + |
| 51 | + cv := g.rootComponentVersion |
| 52 | + for _, refName := range referenceNames { |
| 53 | + cv, err = getReferencedComponentVersion(ctx, repo, cv, refName, g.ocmConfig) |
| 54 | + if err != nil { |
| 55 | + return fmt.Errorf("error getting referenced component version %s: %w", refName, err) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + g.templatesComponentVersion = cv |
| 60 | + g.templatesComponentLocation = buildLocation(repo, cv.Component.Name, cv.Component.Version) |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
| 64 | +func (g *ComponentGetter) RootComponentVersion() *ComponentVersion { |
| 65 | + return g.rootComponentVersion |
| 66 | +} |
| 67 | + |
| 68 | +func (g *ComponentGetter) TemplatesComponentVersion() *ComponentVersion { |
| 69 | + return g.templatesComponentVersion |
| 70 | +} |
| 71 | + |
| 72 | +func getReferencedComponentVersion(ctx context.Context, repo string, parentCV *ComponentVersion, refName string, ocmConfig string) (*ComponentVersion, error) { |
| 73 | + ref, err := parentCV.GetComponentReference(refName) |
| 74 | + if err != nil { |
| 75 | + return nil, fmt.Errorf("error getting component reference %s: %w", refName, err) |
| 76 | + } |
| 77 | + |
| 78 | + location := buildLocation(repo, ref.ComponentName, ref.Version) |
| 79 | + cv, err := GetComponentVersion(ctx, location, ocmConfig) |
| 80 | + if err != nil { |
| 81 | + return nil, fmt.Errorf("error getting component version %s: %w", location, err) |
| 82 | + } |
| 83 | + |
| 84 | + return cv, nil |
| 85 | +} |
| 86 | + |
| 87 | +func (g *ComponentGetter) DownloadTemplatesResource(ctx context.Context, downloadDir string) error { |
| 88 | + return downloadDirectoryResource(ctx, g.templatesComponentLocation, g.templatesResourceName, downloadDir, g.ocmConfig) |
| 89 | +} |
| 90 | + |
| 91 | +func downloadDirectoryResource(ctx context.Context, componentLocation string, resourceName string, downloadDir string, ocmConfig string) error { |
| 92 | + return Execute(ctx, |
| 93 | + []string{"download", "resources", componentLocation, resourceName}, |
| 94 | + []string{"--downloader", "ocm/dirtree", "--outfile", downloadDir}, |
| 95 | + ocmConfig, |
| 96 | + ) |
| 97 | +} |
| 98 | + |
| 99 | +func extractRepoFromLocation(location string) (string, error) { |
| 100 | + parts := strings.SplitN(location, "//", 2) |
| 101 | + if len(parts) < 2 { |
| 102 | + return "", fmt.Errorf("invalid component location format, expected '<repo>//<component>:<version>': %s", location) |
| 103 | + } |
| 104 | + return parts[0], nil |
| 105 | +} |
| 106 | + |
| 107 | +func buildLocation(repo, name, version string) string { |
| 108 | + return fmt.Sprintf("%s//%s:%s", repo, name, version) |
| 109 | +} |
0 commit comments