github.com/wolfi-dev/wolfictl@v0.16.11/pkg/configs/rwfs/os/os.go (about)

     1  package os
     2  
     3  import (
     4  	"io/fs"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/wolfi-dev/wolfictl/pkg/configs/rwfs"
     9  )
    10  
    11  var DefaultFilePerm = fs.FileMode(0o0755)
    12  
    13  type FS struct {
    14  	rootDir string
    15  }
    16  
    17  func (fsys FS) Create(name string) (rwfs.File, error) {
    18  	p := fsys.fullPath(name)
    19  	return os.Create(p)
    20  }
    21  
    22  var _ rwfs.FS = (*FS)(nil)
    23  
    24  func (fsys FS) Open(name string) (fs.File, error) {
    25  	p := fsys.fullPath(name)
    26  	return os.Open(p)
    27  }
    28  
    29  func (fsys FS) OpenAsWritable(name string) (rwfs.File, error) {
    30  	p := fsys.fullPath(name)
    31  	return os.OpenFile(p, os.O_RDWR, DefaultFilePerm)
    32  }
    33  
    34  func (fsys FS) Truncate(name string, size int64) error {
    35  	p := fsys.fullPath(name)
    36  	return os.Truncate(p, size)
    37  }
    38  
    39  func (fsys FS) fullPath(name string) string {
    40  	return filepath.Join(fsys.rootDir, name)
    41  }
    42  
    43  func DirFS(dir string) rwfs.FS {
    44  	return FS{rootDir: dir}
    45  }