github.com/hairyhenderson/templater@v3.5.0+incompatible/funcs/filepath.go (about)

     1  package funcs
     2  
     3  import (
     4  	"path/filepath"
     5  	"sync"
     6  
     7  	"github.com/hairyhenderson/gomplate/conv"
     8  )
     9  
    10  var (
    11  	fpf     *FilePathFuncs
    12  	fpfInit sync.Once
    13  )
    14  
    15  // FilePathNS - the Path namespace
    16  func FilePathNS() *FilePathFuncs {
    17  	fpfInit.Do(func() { fpf = &FilePathFuncs{} })
    18  	return fpf
    19  }
    20  
    21  // AddFilePathFuncs -
    22  func AddFilePathFuncs(f map[string]interface{}) {
    23  	f["filepath"] = FilePathNS
    24  }
    25  
    26  // FilePathFuncs -
    27  type FilePathFuncs struct {
    28  }
    29  
    30  // Base -
    31  func (f *FilePathFuncs) Base(in interface{}) string {
    32  	return filepath.Base(conv.ToString(in))
    33  }
    34  
    35  // Clean -
    36  func (f *FilePathFuncs) Clean(in interface{}) string {
    37  	return filepath.Clean(conv.ToString(in))
    38  }
    39  
    40  // Dir -
    41  func (f *FilePathFuncs) Dir(in interface{}) string {
    42  	return filepath.Dir(conv.ToString(in))
    43  }
    44  
    45  // Ext -
    46  func (f *FilePathFuncs) Ext(in interface{}) string {
    47  	return filepath.Ext(conv.ToString(in))
    48  }
    49  
    50  // FromSlash -
    51  func (f *FilePathFuncs) FromSlash(in interface{}) string {
    52  	return filepath.FromSlash(conv.ToString(in))
    53  }
    54  
    55  // IsAbs -
    56  func (f *FilePathFuncs) IsAbs(in interface{}) bool {
    57  	return filepath.IsAbs(conv.ToString(in))
    58  }
    59  
    60  // Join -
    61  func (f *FilePathFuncs) Join(elem ...interface{}) string {
    62  	s := conv.ToStrings(elem...)
    63  	return filepath.Join(s...)
    64  }
    65  
    66  // Match -
    67  func (f *FilePathFuncs) Match(pattern, name interface{}) (matched bool, err error) {
    68  	return filepath.Match(conv.ToString(pattern), conv.ToString(name))
    69  }
    70  
    71  // Rel -
    72  func (f *FilePathFuncs) Rel(basepath, targpath interface{}) (string, error) {
    73  	return filepath.Rel(conv.ToString(basepath), conv.ToString(targpath))
    74  }
    75  
    76  // Split -
    77  func (f *FilePathFuncs) Split(in interface{}) []string {
    78  	dir, file := filepath.Split(conv.ToString(in))
    79  	return []string{dir, file}
    80  }
    81  
    82  // ToSlash -
    83  func (f *FilePathFuncs) ToSlash(in interface{}) string {
    84  	return filepath.ToSlash(conv.ToString(in))
    85  }
    86  
    87  // VolumeName -
    88  func (f *FilePathFuncs) VolumeName(in interface{}) string {
    89  	return filepath.VolumeName(conv.ToString(in))
    90  }