github.com/searKing/golang/go@v1.2.117/os/exec/cancel.go (about) 1 // Copyright 2020 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package exec 6 7 import ( 8 "context" 9 "io" 10 ) 11 12 func CommandWithCancel(handle func(reader io.Reader), name string, arg ...string) (err error) { 13 cs, err := newCommandServerWithCancel(handle, name, arg...) 14 err = cs.wait() 15 if err != nil { 16 cs.Stop() 17 return err 18 } 19 return nil 20 } 21 func newCommandServerWithCancel(handle func(reader io.Reader), name string, arg ...string) (*commandServer, error) { 22 ctx, stop := context.WithCancel(context.Background()) 23 return newCommandServer(ctx, stop, handle, name, arg...) 24 }