github.com/wawandco/oxpecker@v1.5.7-0.20210910201653-5958d4afdd89/lifecycle/dev/command.go (about) 1 package dev 2 3 import ( 4 "context" 5 6 "github.com/wawandco/oxpecker/internal/log" 7 "github.com/wawandco/oxpecker/plugins" 8 "golang.org/x/sync/errgroup" 9 ) 10 11 var _ plugins.Command = (*Command)(nil) 12 13 // Command is the dev command, it runs the dev plugins, each one on a different 14 // go routine. the detail to what happen on each of these plugins is up to 15 // each of the Developer plugins. 16 type Command struct { 17 developers []Developer 18 beforeDevs []BeforeDeveloper 19 } 20 21 func (d Command) Name() string { 22 return "dev" 23 } 24 25 func (c Command) Alias() string { 26 return "d" 27 } 28 29 func (d Command) ParentName() string { 30 return "" 31 } 32 33 //HelpText returns the help Text of build function 34 func (d Command) HelpText() string { 35 return "calls NPM or yarn to start webpack watching the assetst" 36 } 37 38 // Run calls each of the beforedeveloper plugins and then 39 // executes Developer plugins in parallel. 40 func (d *Command) Run(ctx context.Context, root string, args []string) error { 41 for _, bd := range d.beforeDevs { 42 err := bd.BeforeDevelop(ctx, root) 43 if err != nil { 44 return err 45 } 46 } 47 48 ctx, cancel := context.WithCancel(ctx) 49 defer cancel() 50 51 wg := &errgroup.Group{} 52 for _, d := range d.developers { 53 g := d.Develop 54 wg.Go(func() error { 55 err := g(ctx, root) 56 if err != nil { 57 log.Error(err.Error()) 58 } 59 60 return nil 61 }) 62 } 63 64 return wg.Wait() 65 } 66 67 // Receive Developer and BeforeDeveloper plugins and store these 68 // in the Command to be used when the command is invoked. 69 func (d *Command) Receive(plugins []plugins.Plugin) { 70 for _, tool := range plugins { 71 if ptool, ok := tool.(Developer); ok { 72 d.developers = append(d.developers, ptool) 73 } 74 75 if bdev, ok := tool.(BeforeDeveloper); ok { 76 d.beforeDevs = append(d.beforeDevs, bdev) 77 } 78 } 79 }