filediscovery

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2019 License: MIT Imports: 7 Imported by: 3

README

Go Report Card Coverage Status License Linux build Windows build godoc

Filediscovery

this module helps to find a file in various file locations

Example

    import "github.com/Oppodelldog/filediscovery"

	fileLocationProviders := []filediscovery.FileLocationProvider{
		WorkingDirProvider(),
		ExecutableDirProvider(),
		EnvVarFilePathProvider(envVarName),
		HomeConfigDirProvider(".config","myapp"),
	}

	discovery := filediscovery.New(fileLocationProviders)

	filePath, err := discovery.Discover("file_to_discover.yml")

	// filePath - contains the first existing file in sequential order of given file providers
	// err - nil if file was found. if no file was found it displays helpful error information

Advanced

If you'd like to implement a custom file Provider, you just need to implement the FileLoactionProvider function type. Here's a sample demonstration:

    // type FileLocationProvider func(fileName string) (string, error)

    func myLocationProvider(filename string)(string, error){
        somePath := myCustomLookupStrategy()
        return somePath,nil
    }

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FileDiscoverer

type FileDiscoverer interface {

	// Discover tries to find the given fileName in all FileLocationProviders. The providers are checked in given sequence.
	// the first matching result will be returned. If the file could not be found and error is returned as if any other
	// error occurs.
	Discover(fileName string) (string, error)
}

FileDiscoverer defines logic to discover a file.

func New

func New(fileLocationProviders []FileLocationProvider) FileDiscoverer

New creates a new FileDiscoverer and takes a list of FileLocationProviders which specify possible location a given file will be searched in.

type FileDiscovery

type FileDiscovery struct {
	// contains filtered or unexported fields
}

func (*FileDiscovery) Discover

func (fd *FileDiscovery) Discover(fileName string) (string, error)

Discover tries to find the given fileName in all FileLocationProviders. The providers are checked in given sequence. the first matching result will be returned. If the file could not be found and error is returned as if any other error occurs.

Example
// for this demonstration we create a test file in /tmp
testFilePath := "/tmp/test-file.yml"
err := ioutil.WriteFile(testFilePath, []byte("test"), 0666)
if err != nil {
	panic("error writing test file")
}

// Discovery needs at least one FileLocationProvider which provides a file location to search for.
// There are already some providers available, but let's create a new one, for the sake of completion.
// In this case the FileLocation provided will be the /tmp folder.
tempDirLocationProvider := func(fileName string) (string, error) {
	someFileLocation := "/tmp"
	suggestedFilePath := path.Join(someFileLocation, fileName)
	return suggestedFilePath, nil
}

// create the discovery
discovery := New(
	[]FileLocationProvider{
		tempDirLocationProvider,
	},
)

// search the file
fileFoundAtPath, _ := discovery.Discover("test-file.yml")

// we receive the full path of the file found in /tmp
fmt.Println(fileFoundAtPath)
Output:

/tmp/test-file.yml

type FileLocationProvider

type FileLocationProvider func(fileName string) (string, error)

FileLocationProvider provides a possible file location to FileDiscoverer

func EnvVarFilePathProvider

func EnvVarFilePathProvider(envVar string) FileLocationProvider

EnvVarFilePathProvider provides a filePath in the given environment variable. In contrast to other FileLocationProviders, this file location provider expects a complete filePath in the given environment variable.

func ExecutableDirProvider

func ExecutableDirProvider(subFolders ...string) FileLocationProvider

ExecutableDirProvider provides the executables directory as a possible file location

func HomeConfigDirProvider

func HomeConfigDirProvider(subFolders ...string) FileLocationProvider

HomeConfigDirProvider provides the working directory as a possible file location

func WorkingDirProvider

func WorkingDirProvider(subFolders ...string) FileLocationProvider

WorkingDirProvider provides the working directory as a possible file location

Jump to

Keyboard shortcuts

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