file

package
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

package file provides functions that can be used to retrieve files from local and remote locations.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get(ctx context.Context, location string) (string, error)

Get retrieves a file from these locations (in order):

- Local disk

- HTTP or HTTPS URL

- AWS S3

If a file is found, then it is saved as a temporary local file and the name is returned. The caller is responsible for removing files when they are no longer needed; files should be removed even if an error occurs.

Example (Http)
package main

import (
	"context"
	"fmt"
	"os"
	"strings"

	"github.com/brexhq/substation/internal/file"
)

func main() {
	location := "https://example.com"

	// a local copy of the HTTP body is created and must be removed when it's no longer needed, regardless of errors
	path, err := file.Get(context.TODO(), location)
	defer os.Remove(path)

	if err != nil {
		// handle err
		panic(err)
	}

	f, err := os.Open(path)
	if err != nil {
		// handle err
		panic(err)
	}

	defer f.Close()

	buf := make([]byte, 16)
	if _, err = f.Read(buf); err != nil {
		// handle err
		panic(err)
	}

	prefix := strings.HasPrefix(strings.ToUpper(string(buf)), "<!DOCTYPE")
	fmt.Println(prefix)

}
Output:

true
Example (Local)
package main

import (
	"context"
	"fmt"
	"io"
	"os"

	"github.com/brexhq/substation/internal/file"
)

func main() {
	// temp file is used to simulate an open file and must be removed after the test completes
	temp, _ := os.CreateTemp("", "substation")
	defer os.Remove(temp.Name())
	defer temp.Close()

	_, _ = temp.Write([]byte("foo\nbar\nbaz"))

	// a local copy of the file is created and must be removed when it's no longer needed, regardless of errors
	path, err := file.Get(context.TODO(), temp.Name())
	defer os.Remove(path)

	if err != nil {
		// handle err
		panic(err)
	}

	f, err := os.Open(path)
	if err != nil {
		// handle err
		panic(err)
	}

	defer f.Close()

	buf, err := io.ReadAll(f)
	if err != nil {
		// handle err
		panic(err)
	}

	fmt.Println(string(buf))

}
Output:

foo
bar
baz

Types

This section is empty.

Jump to

Keyboard shortcuts

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