providers/os: Add listFilesDate function to list files by date
This commit is contained in:
		
							parent
							
								
									22ad9ccec5
								
							
						
					
					
						commit
						aebb4ac18b
					
				@ -2,6 +2,8 @@ package os
 | 
			
		||||
 | 
			
		||||
import "os"
 | 
			
		||||
import "io"
 | 
			
		||||
import "time"
 | 
			
		||||
import "sort"
 | 
			
		||||
import "io/fs"
 | 
			
		||||
import "html/template"
 | 
			
		||||
import "git.tebibyte.media/sashakoshka/step"
 | 
			
		||||
@ -32,6 +34,7 @@ func (this *Provider) FuncMapFor (document *step.Document) template.FuncMap {
 | 
			
		||||
		"readFile":      stat.funcReadFile,
 | 
			
		||||
		"readDir":       stat.funcReadDir,
 | 
			
		||||
		"listFiles":     stat.funcListFiles,
 | 
			
		||||
		"listFilesDate": stat.funcListFilesDate,
 | 
			
		||||
		"writeFile":     stat.funcWriteFile,
 | 
			
		||||
		"appendFile":    stat.funcAppendFile,
 | 
			
		||||
		"renameFile":    stat.funcRenameFile,
 | 
			
		||||
@ -94,6 +97,29 @@ func (this *state) funcReadDir (name string) ([]fs.DirEntry, error) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *state) funcListFiles (name string) ([]string, error) {
 | 
			
		||||
	name, err := this.document.Rel(name)
 | 
			
		||||
	if err != nil { return nil, err }
 | 
			
		||||
	entries, err := os.ReadDir(name)
 | 
			
		||||
	if err != nil { return nil, err }
 | 
			
		||||
	sort.Slice(entries, func(left, right int) bool{
 | 
			
		||||
		var leftTime time.Time
 | 
			
		||||
		if leftInfo, err := entries[left].Info(); err != nil {
 | 
			
		||||
			leftTime = leftInfo.ModTime()
 | 
			
		||||
		}
 | 
			
		||||
		var rightTime time.Time
 | 
			
		||||
		if rightInfo, err := entries[right].Info(); err != nil {
 | 
			
		||||
			rightTime = rightInfo.ModTime()
 | 
			
		||||
		}
 | 
			
		||||
		return leftTime.Before(rightTime)
 | 
			
		||||
	})
 | 
			
		||||
	strings := make([]string, len(entries))
 | 
			
		||||
	for index, entry := range entries {
 | 
			
		||||
		strings[index] = entry.Name()
 | 
			
		||||
	}
 | 
			
		||||
	return strings, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *state) funcListFilesDate (name string) ([]string, error) {
 | 
			
		||||
	name, err := this.document.Rel(name)
 | 
			
		||||
	if err != nil { return nil, err }
 | 
			
		||||
	entries, err := os.ReadDir(name)
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user