@@ -38,9 +38,11 @@ type Function struct {
38
38
// NewFork createas and initializes a Fork
39
39
// A Fork object can be manipluated to control how a process is launched.
40
40
// E.g. you can set new namespaces in the SysProcAttr property...
41
- // or, you can set custom args with the (optional) variatic args aparameters.
42
- // If you set args, the first should be the program name (Args[0]), which may
43
- // Which may or may not match the executable.
41
+ //
42
+ // or, you can set custom args with the (optional) variatic args aparameters.
43
+ // If you set args, the first should be the program name (Args[0]), which may
44
+ // Which may or may not match the executable.
45
+ //
44
46
// If no args are specified, args is set to []string{os.Args[0]}
45
47
func NewFork (n string , fn interface {}, args ... string ) (f * Function ) {
46
48
f = & Function {}
@@ -92,6 +94,35 @@ func (f *Function) Fork(args ...interface{}) (err error) {
92
94
return
93
95
}
94
96
97
+ // Combine NewFork and Fork with previous function configuration
98
+ func (f * Function ) ReFork (args ... interface {}) (err error ) {
99
+ previous := f .c
100
+ f .c = exec.Cmd {}
101
+ f .c .Path , _ = os .Executable ()
102
+ f .c .Args = previous .Args
103
+ f .c .Stderr = f .Stderr
104
+ f .c .Stdout = f .Stdout
105
+ f .c .Stdin = f .Stdin
106
+ f .c .SysProcAttr = f .SysProcAttr
107
+ f .c .Env = os .Environ ()
108
+ f .c .Env = append (f .c .Env , nameVar + "=" + f .Name )
109
+ af , err := ioutil .TempFile ("" , "gofork_*" )
110
+ f .c .Env = append (f .c .Env , argsVar + "=" + af .Name ())
111
+ if err != nil {
112
+ return
113
+ }
114
+ enc := gob .NewEncoder (af )
115
+ for _ , iv := range args {
116
+ enc .EncodeValue (reflect .ValueOf (iv ))
117
+ }
118
+ af .Close ()
119
+ if err = f .c .Start (); err != nil {
120
+ return
121
+ }
122
+ f .Process = f .c .Process
123
+ return
124
+ }
125
+
95
126
// Wait provides a wrapper around exec.Cmd.Wait()
96
127
func (f * Function ) Wait () (err error ) {
97
128
if err = f .c .Wait (); err != nil {
0 commit comments