package
0.0.0-20240624235250-9e89dfb7520d
Repository: https://github.com/tailscale/pkgsitelib.git
Documentation: pkg.go.dev

# README

// Copyright 2019 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file.

// Package fetch provides a way to fetch modules from a proxy. package fetch

import ( "fmt" "io/fs" "os" "path" "strings"

"github.com/tailscale/pkgsitelib/pkg"
"github.com/tailscale/pkgsitelib/pkg/derrors"

)

// extractReadme returns the file path and contents the unit's README, // if there is one. dir is the directory path prefixed with the modulePath. func extractReadme(modulePath, dir, resolvedVersion string, contentDir fs.FS) (_ *internal.Readme, err error) { defer derrors.Wrap(&err, "extractReadme(ctx, %q, %q %q, r)", modulePath, dir, resolvedVersion)

innerPath := rel(dir, modulePath)
if strings.HasPrefix(innerPath, "_") {
	// TODO(matloob): do we want to check each element of the path?
	// The original code didn't.
	return nil, nil
}

f, err := contentDir.Open(innerPath)
if err != nil {
	if os.IsNotExist(err) {
		return nil, nil
	}
	return nil, err
}
defer func() {
	cerr := f.Close()
	if err == nil {
		err = cerr
	}
}()
rdf, ok := f.(fs.ReadDirFile)
if !ok {
	return nil, fmt.Errorf("could not open directory for %v", dir)
}
entries, err := rdf.ReadDir(0)
if err != nil {
	return nil, err
}
var readme *internal.Readme
for _, e := range entries {
	pathname := path.Join(innerPath, e.Name())
	if !e.IsDir() && isReadme(pathname) {
		info, err := e.Info()
		if err != nil {
			return nil, err
		}
		if info.Size() > MaxFileSize {
			return nil, fmt.Errorf("file size %d exceeds max limit %d: %w", info.Size(), MaxFileSize, derrors.ModuleTooLarge)
		}
		c, err := readFSFile(contentDir, pathname, MaxFileSize)
		if err != nil {
			return nil, err
		}

		if readme != nil {
			// Prefer READMEs written in markdown, since we style these on
			// the frontend.
			ext := path.Ext(readme.Filepath)
			if ext == ".md" || ext == ".markdown" {
				continue
			}
		}
		readme = &internal.Readme{
			Filepath: pathname,
			Contents: string(c),
		}
	}
}
return readme, nil

}

var excludedReadmeExts = map[string]bool{".go": true, ".vendor": true}

// isReadme reports whether file is README or if the base name of file, with or // without the extension, is equal to expectedFile. README.go files will return // false. It is case insensitive. It operates on '/'-separated paths. func isReadme(file string) bool { const expectedFile = "README" base := path.Base(file) ext := path.Ext(base) return !excludedReadmeExts[ext] && strings.EqualFold(strings.TrimSuffix(base, ext), expectedFile) }

# Functions

FetchLazyModule queries the proxy or the Go repo for the requested module version, downloads the module zip, and does just enough processing to produce UnitMetas for all the modules.
FetchModule queries the proxy or the Go repo for the requested module version, downloads the module zip, and processes the contents to return an *internal.Module and related information.
FSSignature calculates a signature that uniquely identifies a filesystem.
GetInfo returns the result of a request to the proxy .info endpoint.
LatestModuleVersions uses the proxy to get information about the latest versions of modulePath.
NewDirectoryModuleGetter returns a ModuleGetter for reading a module from a directory.
NewGoPackagesModuleGetter returns a ModuleGetter that loads packages using go/packages.Load(pattern), from the requested directory.
NewGoPackagesStdlibModuleGetter returns a ModuleGetter that loads stdlib packages using go/packages.Load, from the requested GOROOT.
NewModCacheGetter returns a ModuleGetter that reads modules from a filesystem directory organized like the proxy.
No description provided by the author
NewStdlibZipModuleGetter returns a ModuleGetter that loads stdlib packages using stdlib zip files.

# Constants

MaxFileSize is the maximum filesize that is allowed for reading.

# Variables

No description provided by the author
Version and commit time are pre specified when fetching a local module, as these fields are normally obtained from a proxy.
Version and commit time are pre specified when fetching a local module, as these fields are normally obtained from a proxy.
No description provided by the author

# Structs

BadPackageError represents an error loading a package because its contents do not make up a valid package.
No description provided by the author
A LazyModule contains the information needed to compute a FetchResult, but has only done enough work to compute the UnitMetas in the module.

# Interfaces

ModuleGetter gets module data.
SearchableModuleGetter is an additional interface that may be implemented by ModuleGetters to support search.
VolatileModuleGetter is an additional interface that may be implemented by ModuleGetters to support invalidating content.