providers/os: Add file statting
This commit is contained in:
		
							parent
							
								
									91e72ac49e
								
							
						
					
					
						commit
						ffb0eb894a
					
				@ -2,11 +2,23 @@ package os
 | 
			
		||||
 | 
			
		||||
import "os"
 | 
			
		||||
import "io"
 | 
			
		||||
import "time"
 | 
			
		||||
import "io/fs"
 | 
			
		||||
import "html/template"
 | 
			
		||||
import "git.tebibyte.media/sashakoshka/step"
 | 
			
		||||
 | 
			
		||||
var _ step.FuncProvider = new(Provider)
 | 
			
		||||
 | 
			
		||||
// FileInfo is like the fs.FileInfo interface, but it directly contains the
 | 
			
		||||
// values instead of having getters for them.
 | 
			
		||||
type FileInfo struct {
 | 
			
		||||
	Name    string
 | 
			
		||||
	Size    int64
 | 
			
		||||
	Mode    fs.FileMode
 | 
			
		||||
	ModTime time.Time
 | 
			
		||||
	IsDir   bool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Provider provides OS functions.
 | 
			
		||||
type Provider struct {
 | 
			
		||||
	
 | 
			
		||||
@ -16,6 +28,7 @@ type Provider struct {
 | 
			
		||||
func (this *Provider) FuncMap () template.FuncMap {
 | 
			
		||||
	return template.FuncMap {
 | 
			
		||||
		"fileExists": funcFileExists,
 | 
			
		||||
		"statFile":   funcStatFile,
 | 
			
		||||
		"readFile":   funcReadFile,
 | 
			
		||||
		"readDir":    funcReadDir,
 | 
			
		||||
		"writeFile":  funcWriteFile,
 | 
			
		||||
@ -29,6 +42,18 @@ func funcFileExists (name string) (bool) {
 | 
			
		||||
	return err == nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func funcStatFile (name string) (FileInfo, error) {
 | 
			
		||||
	info, err := os.Stat(name)
 | 
			
		||||
	if err != nil { return FileInfo { }, err }
 | 
			
		||||
	return FileInfo {
 | 
			
		||||
		Name:    info.Name(),
 | 
			
		||||
		Size:    info.Size(),
 | 
			
		||||
		Mode:    info.Mode(),
 | 
			
		||||
		ModTime: info.ModTime(),
 | 
			
		||||
		IsDir:   info.IsDir(),
 | 
			
		||||
	}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func funcReadFile (name string) (string, error) {
 | 
			
		||||
	file, err := os.Open(name)
 | 
			
		||||
	if err != nil { return "", err  }
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user