github.com/octohelm/wagon@v0.0.0-20240308040401-88662650dc0b/pkg/engine/plan/task/fs_copy.go (about) 1 package task 2 3 import ( 4 "context" 5 "dagger.io/dagger" 6 "github.com/octohelm/wagon/pkg/engine/daggerutil" 7 "github.com/octohelm/wagon/pkg/engine/plan/task/core" 8 "strings" 9 ) 10 11 func init() { 12 core.DefaultFactory.Register(&Copy{}) 13 } 14 15 type Copy struct { 16 core.Task 17 18 Input core.FS `json:"input"` 19 Contents core.FS `json:"contents"` 20 Source string `json:"source" default:"/"` 21 Dest string `json:"dest" default:"/"` 22 Include []string `json:"include"` 23 Exclude []string `json:"exclude"` 24 25 Output core.FS `json:"-" wagon:"generated,name=output"` 26 } 27 28 func (cp *Copy) Do(ctx context.Context) error { 29 return daggerutil.Do(ctx, func(c *dagger.Client) error { 30 contents := cp.Contents.LoadDirectory(c) 31 32 src, err := contents.Directory(cp.Source).Sync(ctx) 33 if err != nil { 34 // path /dist/txt is a file, not a directory 35 if !strings.Contains(err.Error(), "not a directory") { 36 return err 37 } 38 // try copy file 39 f, err := contents.File(cp.Source).Sync(ctx) 40 if err != nil { 41 return err 42 } 43 return cp.Output.SetDirectoryIDBy(ctx, cp.Input.LoadDirectory(c).WithFile(cp.Dest, f)) 44 } 45 46 ct := cp.Input.LoadDirectory(c). 47 WithDirectory(cp.Dest, src, dagger.DirectoryWithDirectoryOpts{ 48 Include: cp.Include, 49 Exclude: cp.Exclude, 50 }) 51 52 return cp.Output.SetDirectoryIDBy(ctx, ct) 53 }) 54 }