github.com/zhgqiang/libcompose@v0.4.1-0.20210112080336-bff7ba3690e1/project/project_run.go (about) 1 package project 2 3 import ( 4 "fmt" 5 6 "golang.org/x/net/context" 7 8 "github.com/zhgqiang/libcompose/project/events" 9 "github.com/zhgqiang/libcompose/project/options" 10 ) 11 12 // Run executes a one off command (like `docker run image command`). 13 func (p *Project) Run(ctx context.Context, serviceName string, commandParts []string, opts options.Run) (int, error) { 14 if !p.ServiceConfigs.Has(serviceName) { 15 return 1, fmt.Errorf("%s is not defined in the template", serviceName) 16 } 17 18 if err := p.initialize(ctx); err != nil { 19 return 1, err 20 } 21 var exitCode int 22 err := p.forEach([]string{}, wrapperAction(func(wrapper *serviceWrapper, wrappers map[string]*serviceWrapper) { 23 wrapper.Do(wrappers, events.ServiceRunStart, events.ServiceRun, func(service Service) error { 24 if service.Name() == serviceName { 25 code, err := service.Run(ctx, commandParts, opts) 26 exitCode = code 27 return err 28 } 29 return nil 30 }) 31 }), func(service Service) error { 32 return service.Create(ctx, options.Create{}) 33 }) 34 return exitCode, err 35 }