importer

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 8, 2020 License: Apache-2.0 Imports: 8 Imported by: 0

README

importer

foxygo.at/jsonnext/importer is a jsonnet.Importer implementation that can import from the local filesystem and from https network sources. Normal paths, absolute and relative, are imported from the local filesystem. Paths starting with a double-slash (//) are imported as https URLs with an implicit https: prefix. For example, the path //github.com/grafana/grafonnet-lib/blob/master/grafonnet/grafana.libsonnet refers to the file grafana.libsonnet on the master branch of the grafana/grafonnet-lib repository on GitHub.

The Importer has a SearchPath that can be populated directly or with the AppendSearchFromEnv method which takes a name of an environment variable, splits it on the OS-specific ListSeparator and appends the elements to the existing search path.

The default Fetcher for the importer is the default http.Client. It can be replaced with any type that implements the Get method of http.Client. Most likely it will be overridden with a http.Client that has been constructed with a non-default configuration.

The importer maintains a cache of results as is required by the jsonnet.Importer interface description. Positive and negative results are cached and returned on subsequent calls to import the same path. Errors retrieving a path are not cached and are returned as an error results from the Import method.

Documentation

Overview

Package importer provides a jsonnet Importer that loads files from https URLs or from the filesystem. It maintains a slice of search paths on which it searches for imports, allowing absolute and relative imports.

A search path element may start with a double-slash (`//`) which is interpreted as a HTTPS url without the `https:` scheme prefix. A search path element starting with a single-slash (`/`) is an absolute path in the processes filesystem. A search path without a leading slash is resolved relative to the current working directory of the process.

Only HTTPS is supported for network paths.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Importer

type Importer struct {
	// SearchPath is an ordered slice of paths (network or local filesystem)
	// that is prepended to the imported filename if the filename is not
	// found. Searching stops when it is found.
	SearchPath []string

	// Fetcher is the URLFetcher used to fetch paths. The default is a
	// http.Client that has a `file:` scheme handler.
	Fetcher URLFetcher
	// contains filtered or unexported fields
}

Importer implements the jsonnet.Importer interface, allowing jsonnet code to be imported via https in addition to local files. Filenames starting with a double-slash (`//`) are fetched via HTTPS. Otherwise the path is treated as a local filesystem path.

Once an import path is successfully fetched, either with data or a definitive not found result, that result is cached for the lifetime of the Importer. This is a requirement of the jsonnet.Importer interface so it is not possible for the same import statement from different files to result in different content. If an Importer is shared across multiple jsonnet.VM instances, the the cache will be shared too. There is no cache expiry logic.

func (*Importer) AppendSearchFromEnv

func (i *Importer) AppendSearchFromEnv(envvar string)

AppendSearchFromEnv appends a list of search paths specified in the given environment variable to the search path list. The elements of the path in the variable are separated by the filepath.SplitList() delimiter.

func (*Importer) Import

func (i *Importer) Import(source, imp string) (jsonnet.Contents, string, error)

Import loads imp from a file or a network location. If imp is a relative path, search for it relative to the directory of source and the search path elements. If the import found, return its contents and the absolute location where it was found. If it was not found, or there was an error reading the content, return the error.

Import will cache the result and return it the next time that path is requested.

This method is defined in the jsonnet.Importer interface:

https://godoc.org/github.com/google/go-jsonnet#Importer

type URLFetcher

type URLFetcher interface {
	Get(url string) (*http.Response, error)
}

A URLFetcher retreives a URL returning a http.Response or an error. It is defined such that http.Client implements it, but allows a different implementation or a custom-configured http.Client to be provided to the Importer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL