findcfg

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2024 License: BSD-2-Clause-Views Imports: 3 Imported by: 2 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Package findcfg finds a config file.

Go Report Card MIT License

An example

// finder for a YAML file in os.Executable() and os.UserConfigDir()+myapp
finder := findcfg.New( // config.yaml
    findcfg.Name("config"),
    findcfg.YAML(),
    findcfg.ExecutableDir(),
    findcfg.UserConfigDir("myapp"),
)

if found := finder.Find(); found != nil {
	return found.Path
}

return finder.FallbackPath()

go get

go get -u github.com/shu-go/findcfg

Copyright 2024 Shuhei Kubota

Documentation

Overview

Package findcfg finds a config file.

// finder for a YAML file in os.Executable() and os.UserConfigDir()+myapp
finder := findcfg.New(
    findcfg.YAML(),
    findcfg.ExecutableDir(),
    findcfg.UserConfigDir("myapp"),
)
found := finder.Find("config") // config.yaml
if found != nil {
	return found.Path
}
return finder.FallbackPath(configName)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DirFunc

type DirFunc func() (path, desc string)

type Finder

type Finder struct {
	Exts   []string
	Names  []string
	Dirs   []DirFunc
	Exacts []string // absolute file path
}

func New

func New(opts ...FinderOption) *Finder

New returns a Finder with given options.

see FinderOption.

func (*Finder) AddDirs

func (f *Finder) AddDirs(dirs ...DirFunc)

AddDirs adds search paths. If no dirs given by AddDirs and New, the Finder does not find anything.

func (*Finder) AddExact added in v0.1.1

func (f *Finder) AddExact(path string)

AddExact adds an absolute file path.

func (*Finder) AddExts

func (f *Finder) AddExts(exts ...string)

AddExts adds extentions. (needs a period like ".yaml") If no exts given by AddExts and New, the Finder does not find anything.

func (*Finder) AddNames added in v0.2.0

func (f *Finder) AddNames(names ...string)

AddNames adds base names without extentions.

func (*Finder) FallbackPath

func (f *Finder) FallbackPath() string

FallbackPath returns a path that may not be exist. Call this method if Find does not find anything.

This method returns the firstly tried path.

func (*Finder) Find

func (f *Finder) Find() *Found

Find try to find a config file accordding to its dirs and exts.

You should call AddXXX before call this method.

baseName is without ".ext", like "myconfig"

type FinderOption

type FinderOption func(*Finder)

func CurrentDir

func CurrentDir(sub ...string) FinderOption

cwd

func Dir

func Dir(sub ...string) FinderOption

arbitrary dirs

func ExactPath

func ExactPath(exact string) FinderOption

an absolute file path

func ExecutableDir

func ExecutableDir(sub ...string) FinderOption

os.Executable()

func Ext

func Ext(exts ...string) FinderOption

arbitrary exts

func HomeDir

func HomeDir(sub ...string) FinderOption

os.UserHomeDir()

func INI

func INI() FinderOption

.ini

func JSON

func JSON() FinderOption

.json

func Name added in v0.2.0

func Name(names string) FinderOption

arbitrary base names

func TOML

func TOML() FinderOption

.toml

func UserConfigDir

func UserConfigDir(sub ...string) FinderOption

os.UserConfigDir()

func YAML

func YAML() FinderOption

.yaml, .yml

type Found

type Found struct {
	Path    string
	Ext     string
	DirDesc string
}

Jump to

Keyboard shortcuts

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