Skip to content

Commit 1d04ce1

Browse files
committed
fix opts maybe nill err
Signed-off-by: ningmingxiao <[email protected]>
1 parent 1c99ade commit 1d04ce1

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

runc.go

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,11 @@ func (r *Runc) Create(context context.Context, id, bundle string, opts *CreateOp
159159
return err
160160
}
161161
args = append(args, oargs...)
162+
} else {
163+
opts = &CreateOpts{}
162164
}
163165
cmd := r.command(context, append(args, id)...)
164-
if opts != nil && opts.IO != nil {
166+
if opts.IO != nil {
165167
opts.Set(cmd)
166168
}
167169
cmd.ExtraFiles = opts.ExtraFiles
@@ -178,7 +180,7 @@ func (r *Runc) Create(context context.Context, id, bundle string, opts *CreateOp
178180
if err != nil {
179181
return err
180182
}
181-
if opts != nil && opts.IO != nil {
183+
if opts.IO != nil {
182184
if c, ok := opts.IO.(StartCloser); ok {
183185
if err := c.CloseAfterStart(); err != nil {
184186
return err
@@ -230,6 +232,9 @@ func (o *ExecOpts) args() (out []string, err error) {
230232
// Exec executes an additional process inside the container based on a full
231233
// OCI Process specification
232234
func (r *Runc) Exec(context context.Context, id string, spec specs.Process, opts *ExecOpts) error {
235+
if opts == nil {
236+
opts = &ExecOpts{}
237+
}
233238
if opts.Started != nil {
234239
defer close(opts.Started)
235240
}
@@ -244,15 +249,15 @@ func (r *Runc) Exec(context context.Context, id string, spec specs.Process, opts
244249
return err
245250
}
246251
args := []string{"exec", "--process", f.Name()}
247-
if opts != nil {
248-
oargs, err := opts.args()
249-
if err != nil {
250-
return err
251-
}
252+
oargs, err := opts.args()
253+
if err != nil {
254+
return err
255+
}
256+
if len(oargs) > 0 {
252257
args = append(args, oargs...)
253258
}
254259
cmd := r.command(context, append(args, id)...)
255-
if opts != nil && opts.IO != nil {
260+
if opts.IO != nil {
256261
opts.Set(cmd)
257262
}
258263
if cmd.Stdout == nil && cmd.Stderr == nil {
@@ -270,7 +275,7 @@ func (r *Runc) Exec(context context.Context, id string, spec specs.Process, opts
270275
if opts.Started != nil {
271276
opts.Started <- cmd.Process.Pid
272277
}
273-
if opts != nil && opts.IO != nil {
278+
if opts.IO != nil {
274279
if c, ok := opts.IO.(StartCloser); ok {
275280
if err := c.CloseAfterStart(); err != nil {
276281
return err
@@ -287,19 +292,22 @@ func (r *Runc) Exec(context context.Context, id string, spec specs.Process, opts
287292
// Run runs the create, start, delete lifecycle of the container
288293
// and returns its exit status after it has exited
289294
func (r *Runc) Run(context context.Context, id, bundle string, opts *CreateOpts) (int, error) {
295+
if opts == nil {
296+
opts = &CreateOpts{}
297+
}
290298
if opts.Started != nil {
291299
defer close(opts.Started)
292300
}
293301
args := []string{"run", "--bundle", bundle}
294-
if opts != nil {
295-
oargs, err := opts.args()
296-
if err != nil {
297-
return -1, err
298-
}
302+
oargs, err := opts.args()
303+
if err != nil {
304+
return -1, err
305+
}
306+
if len(oargs) > 0 {
299307
args = append(args, oargs...)
300308
}
301309
cmd := r.command(context, append(args, id)...)
302-
if opts != nil && opts.IO != nil {
310+
if opts.IO != nil {
303311
opts.Set(cmd)
304312
}
305313
ec, err := Monitor.Start(cmd)

0 commit comments

Comments
 (0)