github.com/tilt-dev/tilt@v0.36.0/internal/xdg/fake.go (about)

     1  package xdg
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/spf13/afero"
     9  )
    10  
    11  type FakeBase struct {
    12  	Dir string
    13  	FS  afero.Fs
    14  }
    15  
    16  func NewFakeBase(p string, fs afero.Fs) *FakeBase {
    17  	return &FakeBase{Dir: p, FS: fs}
    18  }
    19  
    20  var _ Base = &FakeBase{}
    21  
    22  func (b *FakeBase) createPath(prefix, relPath string) (string, error) {
    23  	p := filepath.Join(b.Dir, prefix, relPath)
    24  	dir := filepath.Dir(p)
    25  	err := b.FS.MkdirAll(dir, os.ModeDir|0700)
    26  	if err != nil {
    27  		return "", fmt.Errorf("creating dir %s: %v", dir, err)
    28  	}
    29  	return p, nil
    30  }
    31  
    32  func (b *FakeBase) CacheFile(relPath string) (string, error) {
    33  	return b.createPath("cache", relPath)
    34  }
    35  func (b *FakeBase) ConfigFile(relPath string) (string, error) {
    36  	return b.createPath("config", relPath)
    37  }
    38  func (b *FakeBase) DataFile(relPath string) (string, error) {
    39  	return b.createPath("data", relPath)
    40  }
    41  func (b *FakeBase) StateFile(relPath string) (string, error) {
    42  	return b.createPath("state", relPath)
    43  }
    44  func (b *FakeBase) RuntimeFile(relPath string) (string, error) {
    45  	return b.createPath("runtime", relPath)
    46  }