You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: packages.go
    path of file: ./repos/terraform/internal/rpcapi
    
    the code of the file until where you have to start completion: // Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

package rpcapi

import (
	"context"
	"fmt"
	"strings"

	"github.com/apparentlymart/go-versions/versions"
	"github.com/hashicorp/terraform-svchost/disco"

	"github.com/hashicorp/terraform/internal/addrs"
	"github.com/hashicorp/terraform/internal/getmodules"
	"github.com/hashicorp/terraform/internal/getproviders"
	"github.com/hashicorp/terraform/internal/providercache"
	"github.com/hashicorp/terraform/internal/registry"
	"github.com/hashicorp/terraform/internal/registry/regsrc"
	"github.com/hashicorp/terraform/internal/rpcapi/terraform1"
)

var _ terraform1.PackagesServer = (*packagesServer)(nil)

func newPackagesServer(services *disco.Disco) *packagesServer {
	return &packagesServer{
		services: services,

		// This function lets us control the provider source during tests.
		providerSourceFn: func(services *disco.Disco) getproviders.Source {
			// TODO: Implement loading from alternate sources like network or filesystem
			//  mirrors.
			return getproviders.NewRegistrySource(services)
		},
	}
}

type providerSourceFn func(services *disco.Disco) getproviders.Source

type packagesServer struct {
	terraform1.UnimplementedPackagesServer

	services         *disco.Disco
	providerSourceFn providerSourceFn
}

func (p *packagesServer) ProviderPackageVersions(ctx context.Context, request *terraform1.ProviderPackageVersions_Request) (*terraform1.ProviderPackageVersions_Response, error) {
	response := new(terraform1.ProviderPackageVersions_Response)

	source := p.providerSourceFn(p.services)
	provider, diags := addrs.ParseProviderSourceString(request.SourceAddr)
	response.Diagnostics = append(response.Diagnostics, diagnosticsToProto(diags)...)
	if diags.HasErrors() {
		return response, nil
	}

	versions, warnings, err := source.AvailableVersions(ctx, provider)

	displayWarnings := make([]string, len(warnings))
	for ix, warning := range warnings {
		displayWarnings[ix] = fmt.Sprintf("- %s", warning)
	}
	if len(displayWarnings) > 0 {
		response.Diagnostics = append(response.Diagnostics, &terraform1.Diagnostic{
			Severity: terraform1.Diagnostic_WARNING,
			Summary:  "Additional provider information from registry",
			Detail:   fmt.Sprintf("The remote registry returned warnings for %s:\n%s", provider.ForDisplay(), strings.Join(displayWarnings, "\n")),
		})
	}

	if err != nil {
		// TODO: Parse the different error types so we can provide specific
		//  error diagnostics, see commands/init.go:621.
		response.Diagnostics = append(response.Diagnostics, &terraform1.Diagnostic{
			Severity: terraform1.Diagnostic_ERROR,
			Summary:  "Failed to query available provider packages",
			Detail:   fmt.Sprintf("Cou
    
func (p *packagesServer) FetchProviderPackage(ctx context.Context, request *terraform1.FetchProviderPackage_Request) (*terraform1.FetchProviderPackage_Response, error) {

	response := new(terraform1.FetchProviderPackage_Response)

	version, err := versions.ParseVersion(request.Version)
	if err != nil {
		response.Diagnostics = append(response.Diagnostics, &terraform1.Diagnostic{
			Severity: terraform1.Diagnostic_ERROR,
			Summary:  "Invalid platform",
			Detail:   fmt.Sprintf("The requested version %s is invalid: %s.", request.Version, err),
		})
		return response, nil
	}

	source := p.providerSourceFn(p.services)
	provider, diags := addrs.ParseProviderSourceString(request.SourceAddr)
	response.Diagnostics = append(response.Diagnostics, diagnosticsToProto(diags)...)
	if diags.HasErrors() {
		return response, nil
	}

	var allowedHashes []getproviders.Hash
	for _, hash := range request.Hashes {
		allowedHashes = append(allowedHashes, getproviders.Hash(hash))
	}

	for _, requestPlatform := range request.Platforms {
		result := new(terraform1.FetchProviderPackage_PlatformResult)
		response.Results = append(response.Results, result)

		platform, err := getproviders.ParsePlatform(requestPlatform)
		if err != nil {
			result.Diagnostics = append(result.Diagnostics, &terraform1.Diagnostic{
				Severity: terraform1.Diagnostic_ERROR,
				Summary:  "Invalid platform",
				Detail:   fmt.Sprintf("The requested platform %s is invalid: %s.", requestPlatform, err),
			})
			continue
		}

		meta, err := source.PackageMeta(ctx, provider, version, platform)
		if err != nil {
			// TODO: Parse the different error types so we can provide specific
			//  error diagnostics, see commands/init.go:731.
			result.Diagnostics = append(result.Diagnostics, &terraform1.Diagnostic{
				Severity: terraform1.Diagnostic_ERROR,
				Summary:  "Failed to query provider package metadata",
				Detail:   fmt.Sprintf("Could not retrieve package metadata for provider %s@%s for %s: %s.", provider.ForDisplay(), version.String(), platform.String(), err),
			})
			continue
		}

		into := providercache.NewDirWithPlatform(request.CacheDir, platform)
		authResult, err := into.InstallPackage(ctx, meta, allowedHashes)
		if err != nil {
			// TODO: Parse the different error types so we can provide specific
			//  error diagnostics, see commands/init.go:731.
			result.Diagnostics = append(result.Diagnostics, &terraform1.Diagnostic{
				Severity: terraform1.Diagnostic_ERROR,
				Summary:  "Failed to download provider package",
				Detail:   fmt.Sprintf("Could not download provider %s@%s for %s: %s.", provider.ForDisplay(), version.String(), platform.String(), err),
			})
			continue
		}

		var hashes []string
		if authResult.SignedByAnyParty() {
			for _, hash := range meta.AcceptableHashes() {
				hashes = append(hashes, string(hash))
			}
		}

		providerPackage := into.ProviderVersion(provider, version)
		hash, err := providerPackage.Hash()
		if err != nil {
			result.Diagnostics = append(result.Diagnostics, &terraform1.Diagnostic{
				Severity: terraform1.Diagnostic_ERROR,
				Summary:  "Failed to hash provider package",
				Detail:   fmt.Sprintf("Could not hash provider %s@%s for %s: %s.", provider.ForDisplay(), version.String(), platform.String(), err),
			})
			continue
		}
		hashes = append(hashes, string(hash))
		result.Provider = &terraform1.ProviderPackage{
			SourceAddr: request.SourceAddr,
			Version:    request.Version,
			Hashes:     hashes,
		}
	}

	return response, nil
}