github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/tiltfile/watch/tiltignore.go (about)

     1  package watch
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"github.com/moby/buildkit/frontend/dockerfile/dockerignore"
    10  
    11  	"github.com/tilt-dev/tilt/pkg/model"
    12  )
    13  
    14  const TiltignoreFileName = ".tiltignore"
    15  
    16  // .tiltignore sits next to Tiltfile
    17  func TiltignorePath(tiltfilePath string) string {
    18  	return filepath.Join(filepath.Dir(tiltfilePath), TiltignoreFileName)
    19  }
    20  
    21  func ReadTiltignore(tiltignorePath string) (model.Dockerignore, error) {
    22  	tiltignoreContents, err := os.ReadFile(tiltignorePath)
    23  
    24  	// missing tiltignore is fine, but a filesystem error is not
    25  	if err != nil {
    26  		if os.IsNotExist(err) {
    27  			return model.Dockerignore{}, nil
    28  		}
    29  		return model.Dockerignore{}, err
    30  	}
    31  
    32  	patterns, err := dockerignore.ReadAll(bytes.NewBuffer(tiltignoreContents))
    33  	if err != nil {
    34  		return model.Dockerignore{}, fmt.Errorf("Parsing .tiltignore: %v", err)
    35  	}
    36  
    37  	return model.Dockerignore{
    38  		LocalPath: filepath.Dir(tiltignorePath),
    39  		Source:    tiltignorePath,
    40  		Patterns:  patterns,
    41  	}, nil
    42  }