Documentation
¶
Overview ¶
Package uriget provides a mechanism for "loading" the contents of a file from a uri with flexible support for different uri schemes. This is primarily used for loading Score provisioner files for various Score implementations. There are similar packages such as hashicorp/go-getter, however this version is maintained to have zero dependencies and use the binaries that exist on the local system where possible.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetFile ¶
GetFile attempts to parse and retrieve file contents from the given url/uri. The scheme is used to inform what sources are supported and how the remainder of the url should be handled. Supported schemes: - http/https: reads the file using http. - file or no scheme: attempts to read the file from local file system. - git-ssh / git-https: attempts to perform a sparse checkout of just the target file.
Example (Git) ¶
buff, err := GetFile(context.Background(), "git-https://github.com/score-spec/score.dev.git/README.md") fmt.Println(len(buff) > 0, err)
Output: true <nil>
Example (Http) ¶
buff, err := GetFile(context.Background(), "http://example.com") fmt.Println(len(buff) > 0, err) _, err = GetFile(context.Background(), "https://example.com/does/not/exist") fmt.Println(err)
Output: true <nil> GET https://example.com/does/not/exist non-200 status code: 404
Example (Local) ¶
buff, err := GetFile(context.Background(), "../README.md") fmt.Println(len(buff) > 0, err) _, err = GetFile(context.Background(), "./does/not/exist.txt") fmt.Println(err)
Output: true <nil> open ./does/not/exist.txt: no such file or directory
Example (Oci) ¶
testUrl := "oci://ghcr.io/score-spec/score-compose:0.18.0"
buff, err := GetFile(context.Background(), testUrl)
if err != nil {
fmt.Println("failed to pull OCI image:", err)
return
}
fmt.Println(len(buff) > 0)
Output: true
Example (OciNoTag) ¶
testUrl := "oci://ghcr.io/score-spec/score-compose"
buff, err := GetFile(context.Background(), testUrl)
if err != nil {
fmt.Println("failed to pull OCI image:", err)
return
}
fmt.Println(len(buff) > 0)
Output: true
Example (OciWithDigest) ¶
testUrl := "oci://ghcr.io/score-spec/score-compose@sha256:f3d8d5485a751cbdc91e073df1b6fbcde83f85a86ee3bc7d53e05b00452baedd"
buff, err := GetFile(context.Background(), testUrl)
if err != nil {
fmt.Println("failed to pull OCI image:", err)
return
}
fmt.Println(len(buff) > 0)
Output: true
Types ¶
type HttpDoer ¶
HttpDoer is an http.Client interface used for overrides during testing or other http fetching implementations.
type Option ¶
type Option func(*options)
Option is an option function that modifies the options structure in place.
func WithHttpClient ¶
WithHttpClient sets the http client that may be used.
Example ¶
customClient := &http.Client{
Transport: &http.Transport{
Proxy: func(*http.Request) (*url.URL, error) {
return nil, fmt.Errorf("no proxy")
},
},
}
_, err := GetFile(context.Background(), "https://example.com", WithHttpClient(customClient))
fmt.Println(err)
Output: failed to make get request: Get "https://example.com": no proxy