Skip to content

Oppodelldog/filediscovery

Repository files navigation

Go Report Card Coverage Status License Build Status pkg.go.dev

Filediscovery

this module helps to find a file in various file locations

Example

See the example in test/example_test.go.

    //noinspection All
    package main

    import "github.com/Oppodelldog/filediscovery/filediscovery"
    func main(){
    
        var envVarName = "MYAPP_CONFIG_FILE"

        fileLocationProviders := []filediscovery.FileLocationProvider{
            filediscovery.WorkingDirProvider(),
            filediscovery.ExecutableDirProvider(),
            filediscovery.EnvVarFilePathProvider(envVarName),
            filediscovery.HomeConfigDirProvider(".config","myapp"),
        }
    
        discovery := filediscovery.New(fileLocationProviders)
    
        filePath, err := discovery.Discover("file_to_discover.yml")
        _ , _= filePath,err
        // 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:

    package main

    // type FileLocationProvider func(fileName string) (string, error)
    
    //noinspection All
    func myLocationProvider(filename string)(string, error){
        somePath := "get it from your custom lookup strategy"

        return somePath,nil
    }

About

go module that helps to find a file in various places

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors