add support for excludes in input dir

This adds support for `--include` and `--exclude` for directory inputs.

For instance, this is what you would want to use when passing dagger
repository as an input:

```
inputs:
    repository:
        dir:
            path: .
            exclude:
                - '**/node_modules'
                - cmd/dagger/dagger
                - cmd/dagger/dagger-debug
```

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi
2021-05-28 16:04:16 -07:00
parent 2f9a5df397
commit b627b4bc88
16 changed files with 103 additions and 93 deletions

View File

@@ -61,18 +61,20 @@ func (i Input) Compile(key string, state *State) (*compiler.Value, error) {
}
// An input artifact loaded from a local directory
func DirInput(path string, include []string) Input {
func DirInput(path string, include []string, exclude []string) Input {
return Input{
Dir: &dirInput{
Path: path,
Include: include,
Exclude: exclude,
},
}
}
type dirInput struct {
Path string `json:"path,omitempty"`
Include []string `json:"include,omitempty"`
Path string `yaml:"path,omitempty"`
Include []string `yaml:"include,omitempty"`
Exclude []string `yaml:"exclude,omitempty"`
}
func (dir dirInput) Compile(_ string, state *State) (*compiler.Value, error) {
@@ -88,6 +90,14 @@ func (dir dirInput) Compile(_ string, state *State) (*compiler.Value, error) {
return nil, err
}
}
excludeLLB := []byte("[]")
if len(dir.Exclude) > 0 {
var err error
excludeLLB, err = json.Marshal(dir.Exclude)
if err != nil {
return nil, err
}
}
p := dir.Path
if !filepath.IsAbs(p) {
@@ -98,18 +108,19 @@ func (dir dirInput) Compile(_ string, state *State) (*compiler.Value, error) {
}
llb := fmt.Sprintf(
`#up: [{do:"local",dir:"%s", include:%s}]`,
`#up: [{do:"local",dir:"%s", include:%s, exclude:%s}]`,
p,
includeLLB,
excludeLLB,
)
return compiler.Compile("", llb)
}
// An input artifact loaded from a git repository
type gitInput struct {
Remote string `json:"remote,omitempty"`
Ref string `json:"ref,omitempty"`
Dir string `json:"dir,omitempty"`
Remote string `yaml:"remote,omitempty"`
Ref string `yaml:"ref,omitempty"`
Dir string `yaml:"dir,omitempty"`
}
func GitInput(remote, ref, dir string) Input {
@@ -145,7 +156,7 @@ func DockerInput(ref string) Input {
}
type dockerInput struct {
Ref string `json:"ref,omitempty"`
Ref string `yaml:"ref,omitempty"`
}
func (i dockerInput) Compile(_ string, _ *State) (*compiler.Value, error) {

View File

@@ -26,7 +26,7 @@ type State struct {
// Cue module containing the environment plan
// The input's top-level artifact is used as a module directory.
func (s *State) PlanSource() Input {
return DirInput(s.Plan, []string{"*.cue", "cue.mod"})
return DirInput(s.Plan, []string{"*.cue", "cue.mod"}, []string{})
}
func (s *State) SetInput(key string, value Input) error {