Documentation
¶
Overview ¶
Package parts is used to traverse or read files in one or more directories using Debian's run-parts layout. Filters can be used to only include:
- Files that match a regular expression.
- Files that match a file type mask, e.g., regular files, directories, etc.
- Files that match a file permission mask, e.g., executables.
Directories are specified in a slice. Files from directories appearing earlier in the slice that have the same base name as a file from a directory appearing later in the slice take precedence. e.g., using the following directory layouts:
── etc
│ ├── 10-both.conf
│ ├── 10-only-etc.conf
│ ├── 20-only-etc.conf
│ └── 30-symlink.conf -> ../usr/lib/30-symlink.conf
├── test.conf
└── usr
└── lib
├── 10-both.conf
├── 10-executable.sh
├── 10-only-lib.conf
├── 20-only-lib.conf
├── 30-symlink.conf
├── 40-noconf
└── nodigits.conf
The etc/10-both.conf file is used and the /usr/lib/10-both.conf is ignored if etc is configured ahead of /usr/lib.
After duplicates are resolved, the files are lexically sorted (optionally in reverse order).
Using the above layout and using a ".*\.conf" file name filter we can use parts.Readdirnames() to list the files in run-parts order, e.g.,
etc/10-both.conf etc/10-only-etc.conf usr/lib/10-only-lib.conf etc/20-only-etc.conf usr/lib/20-only-lib.conf etc/30-symlink.conf usr/lib/nodigits.conf test.conf
Or we can read the concatenated contents of all these files using parts.Read().
Index ¶
Examples ¶
Constants ¶
const ( // The single letters are the abbreviations // used by the String method's formatting. ModeDir = FileMode(os.ModeDir) // d: is a directory ModeAppend = FileMode(os.ModeAppend) // a: append-only ModeExclusive = FileMode(os.ModeExclusive) // l: exclusive use ModeTemporary = FileMode(os.ModeTemporary) // T: temporary file; Plan 9 only ModeSymlink = FileMode(os.ModeSymlink) // L: symbolic link ModeDevice = FileMode(os.ModeDevice) // D: device file ModeNamedPipe = FileMode(os.ModeNamedPipe) // p: named pipe (FIFO) ModeSocket = FileMode(os.ModeSocket) // S: Unix domain socket12 ModeSetuid = FileMode(os.ModeSetuid) // u: setuid ModeSetgid = FileMode(os.ModeSetgid) // g: setgid ModeCharDevice = FileMode(os.ModeCharDevice) // c: Unix character device, when ModeDevice is set ModeSticky = FileMode(os.ModeSticky) // t: sticky ModeRegular = 1 << (31 - iota) // f: regular file // Mask for the type bits. ModeType = ModeDir | ModeSymlink | ModeNamedPipe | ModeSocket | ModeDevice | ModeRegular // Unix permission bits ModePerm FileMode = 0777 )
const ( DefaultModeTypeFilter = ModeRegular DefaultModePermFilter = ModePerm ExecutableModePermFilter = 0111 ExecutableModeTypeFilter = ModeRegular DefaultRegExpFilter = ".*" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
Reverse bool
ModeTypeFilter FileMode
ModePermFilter FileMode
RegExpFilter *regexp.Regexp
}
Config is used to pass parameter to NewConfig.
func NewConfig ¶
func NewConfig(reverse bool, modeTypeFilter FileMode, modePermFilter FileMode, regExpFilter string) (*Config, error)
NewConfig constructor. Can fail if regular expressions do not compile.
func NewDefaultConfig ¶
func NewDefaultConfig() *Config
NewDefaultConfig returns a default Config constructor.
type FileMode ¶
FileMode is an extension of the os.FileMode struct. It adds a ModeRegular bit to make testing for a regular file explicit.
func LstatMode ¶
LstatMode returns the FileMode for the named path. If the path is a symbolic link, the returned FileMode describes the symbolic link. If there is an error, it will be of type *PathError.
Example ¶
for _, path := range testDataSymlinks {
mode, _ := parts.LstatMode(path)
fmt.Println(mode)
}
Output: Lrwxrwxrwx
func StatMode ¶
StatMode returns the FileMode for the named path. If there is an error, it will be of type *PathError.
Example ¶
for _, path := range testDataExecutables {
mode, _ := parts.StatMode(path)
fmt.Println(mode)
}
Output: frwxr-xr-x
func (FileMode) IsExecutable ¶
IsExecutable reports whether m describes an executablexs file.
type Parts ¶
Parts encapsulates data and functions used to process "run-parts" directories.
func NewParts ¶
NewParts is the Parts constructor. A default configuration is used if config is nil.
func (*Parts) Read ¶
Read reads the contents of the parts directory into buffer b.
Example ¶
config, _ := parts.NewConfig( false, parts.DefaultModeTypeFilter, parts.DefaultModePermFilter, `\.conf$`) p := parts.NewParts(testDataPaths, config) defer p.Close() b, _ := ioutil.ReadAll(p) contents := string(b) fmt.Print(contents)
Output: 10-both.conf 10-only-etc.conf 10-only-lib.conf 20-only-etc.conf 20-only-lib.conf 30-symlink.conf nodigits.conf test.conf
func (*Parts) Readdirnames ¶
Readdirnames returns a list of files in paths that follow the "run-parts" naming convention.
Example ¶
fmt.Println("List executable files in run-parts order:")
config, _ := parts.NewConfig(
false,
parts.ExecutableModeTypeFilter,
parts.ExecutableModePermFilter,
parts.DefaultRegExpFilter)
p := parts.NewParts(testDataPaths, config)
names, _ := p.Readdirnames(0)
for _, name := range names {
fmt.Printf("\t%s\n", name)
}
fmt.Println("\nList files with extension '.conf' in run-parts order:")
config, _ = parts.NewConfig(
false,
parts.DefaultModeTypeFilter,
parts.DefaultModePermFilter,
`\.conf$`)
p = parts.NewParts(testDataPaths, config)
names, _ = p.Readdirnames(0)
for _, name := range names {
fmt.Printf("\t%s\n", name)
}
fmt.Println("\nList files with extension '.conf' in reverse run-parts order:")
config, _ = parts.NewConfig(
true,
parts.DefaultModeTypeFilter,
parts.DefaultModePermFilter,
`\.conf$`)
p = parts.NewParts(testDataPaths, config)
names, _ = p.Readdirnames(0)
for _, name := range names {
fmt.Printf("\t%s\n", name)
}
Output: List executable files in run-parts order: testdata/usr/lib/10-executable.sh List files with extension '.conf' in run-parts order: testdata/etc/10-both.conf testdata/etc/10-only-etc.conf testdata/usr/lib/10-only-lib.conf testdata/etc/20-only-etc.conf testdata/usr/lib/20-only-lib.conf testdata/etc/30-symlink.conf testdata/usr/lib/nodigits.conf testdata/test.conf List files with extension '.conf' in reverse run-parts order: testdata/test.conf testdata/usr/lib/nodigits.conf testdata/etc/30-symlink.conf testdata/usr/lib/20-only-lib.conf testdata/etc/20-only-etc.conf testdata/usr/lib/10-only-lib.conf testdata/etc/10-only-etc.conf testdata/etc/10-both.conf