miki

Miki: personal wiki with the fmd markup format
git clone https://wtenison.com/repo/miki.git
Log | Files | Refs | README | LICENSE

directories.go (2438B)


      1 package filesystem
      2 
      3 import (
      4 	"fmt"
      5 	"os"
      6 	"path/filepath"
      7 )
      8 
      9 type SearchDepth int
     10 
     11 const (
     12 	// NOTE: Should I have an INF option?
     13 	SEARCH_DEPTH_INF  SearchDepth = -1
     14 	SEARCH_DEPTH_ZERO SearchDepth = 0
     15 )
     16 
     17 type DirectoryPackage struct {
     18 	Root            string   // The root directory of the resource.
     19 	DependencyFiles []string // The set of valid resource files.
     20 	// DependencyData  interface{} // Any relevant resource data.
     21 }
     22 
     23 // func MakeDirectoryPkg(resSig DirectorySignature) (DirectoryPackage, error) {
     24 func MakeDirectoryPkg(directoryPath string, depth SearchDepth, filePattern string) (DirectoryPackage, error) {
     25 
     26 	_dirPkg := DirectoryPackage{Root: filepath.Clean(directoryPath)}
     27 
     28 	// Check for valid input parameters.
     29 	if _dirPkg.Root == "" || depth < -1 {
     30 		_retErr := fmt.Errorf("MakeDirectoryPkg:invalid directory signature (%s, %d)", _dirPkg.Root, depth)
     31 		return _dirPkg, _retErr
     32 	}
     33 
     34 	// Check if input directory exists.
     35 	if _, _err := os.Stat(_dirPkg.Root); _err != nil {
     36 		_retErr := fmt.Errorf("MakeDirectoryPkg:os.Stat:%s", _err)
     37 		return _dirPkg, _retErr
     38 	}
     39 
     40 	// Check and collect directory files.
     41 	_fileList, _err := os.ReadDir(_dirPkg.Root)
     42 	if _err != nil {
     43 		_retErr := fmt.Errorf("MakeDirectoryPkg:%s", _err)
     44 		return DirectoryPackage{}, _retErr
     45 	}
     46 
     47 	// A variable to store root sub-directory files.
     48 	_subDirPkgDeps := []string{}
     49 
     50 	for _, _f := range _fileList { // Collete all valid sub-directory files.
     51 
     52 		// Recurse into valid sub-directory files.
     53 		if _f.IsDir() && (depth > SEARCH_DEPTH_ZERO || depth == SEARCH_DEPTH_INF) {
     54 
     55 			// Decrement depth if not infinite.
     56 			if depth != SEARCH_DEPTH_INF {
     57 				depth--
     58 			}
     59 
     60 			// Get sub-directory content.
     61 			_subDir := filepath.Join(directoryPath, "/"+_f.Name())
     62 			_subDirPkg, _err := MakeDirectoryPkg(_subDir, depth, filePattern)
     63 			if _err != nil {
     64 				_retErr := fmt.Errorf("MakeDirectoryPkg:%s", _err)
     65 				return DirectoryPackage{}, _retErr
     66 			}
     67 
     68 			// Add to package dependencies list.
     69 			_subDirPkgDeps = append(_subDirPkgDeps, _subDirPkg.DependencyFiles...)
     70 
     71 		} else { // Add files that match pattern to dependency list.
     72 			if matches, _ := filepath.Match(filePattern, _f.Name()); matches {
     73 				_dirPkg.DependencyFiles = append(_dirPkg.DependencyFiles, filepath.Join(_dirPkg.Root, _f.Name()))
     74 			}
     75 		}
     76 	}
     77 
     78 	// Include any sub-directory file dependencies.
     79 	_dirPkg.DependencyFiles = append(_dirPkg.DependencyFiles, _subDirPkgDeps...)
     80 
     81 	return _dirPkg, nil
     82 }