github.com/octohelm/wagon@v0.0.0-20240308040401-88662650dc0b/pkg/engine/pipleline.go (about) 1 package engine 2 3 import ( 4 "bytes" 5 cueerrors "cuelang.org/go/cue/errors" 6 "fmt" 7 "github.com/octohelm/wagon/pkg/engine/plan/task/core" 8 "github.com/pkg/errors" 9 "golang.org/x/net/context" 10 "strings" 11 ) 12 13 type Pipeline struct { 14 Action []string `arg:""` 15 ImagePullPrefix string `flag:",omitempty"` 16 Plan string `flag:",omitempty" alias:"p"` 17 Output string `flag:",omitempty" alias:"o"` 18 } 19 20 func (c *Pipeline) SetDefaults() { 21 if c.Plan == "" { 22 c.Plan = "./wagon.cue" 23 } 24 } 25 26 func (c *Pipeline) Run(ctx context.Context) error { 27 p, err := New( 28 ctx, 29 WithPlan(c.Plan), 30 WithOutput(c.Output), 31 ) 32 if err != nil { 33 return err 34 } 35 36 ctx = core.ContextWithImagePullPrefixier(ctx, &imagePullPrefixier{prefix: c.ImagePullPrefix}) 37 38 if err := p.Run(ctx, c.Action...); err != nil { 39 // print full cue errors if exists 40 if errlist := cueerrors.Errors(err); len(errlist) > 0 { 41 buf := bytes.NewBuffer(nil) 42 for i := range errlist { 43 cueerrors.Print(buf, errlist[i], nil) 44 } 45 return errors.New(buf.String()) 46 } 47 return err 48 } 49 return nil 50 } 51 52 type imagePullPrefixier struct { 53 prefix string 54 } 55 56 func (c *imagePullPrefixier) ImagePullPrefix(name string) string { 57 if c.prefix != "" { 58 if !strings.HasPrefix(name, c.prefix) { 59 if n := len(strings.Split(name, "/")); n <= 2 { 60 switch n { 61 case 1: 62 name = fmt.Sprintf("docker.io/library/%s", name) 63 case 2: 64 name = fmt.Sprintf("docker.io/%s", name) 65 } 66 } 67 68 return c.prefix + name 69 } 70 } 71 return name 72 }