@@ -49,19 +49,32 @@ import (
4949 "github.com/containerd/nerdctl/v2/pkg/infoutil"
5050)
5151
52+ // TaskOptions contains options for creating a new task
53+ type TaskOptions struct {
54+ AttachStreamOpt []string
55+ IsInteractive bool
56+ IsTerminal bool
57+ IsDetach bool
58+ Con console.Console
59+ LogURI string
60+ DetachKeys string
61+ Namespace string
62+ DetachC chan <- struct {}
63+ CheckpointDir string
64+ }
65+
5266// NewTask is from https://github.com/containerd/containerd/blob/v1.4.3/cmd/ctr/commands/tasks/tasks_unix.go#L70-L108
53- func NewTask (ctx context.Context , client * containerd.Client , container containerd.Container ,
54- attachStreamOpt []string , isInteractive , isTerminal , isDetach bool , con console.Console , logURI , detachKeys , namespace string , detachC chan <- struct {}, checkpointDir string ) (containerd.Task , error ) {
67+ func NewTask (ctx context.Context , client * containerd.Client , container containerd.Container , opts TaskOptions ) (containerd.Task , error ) {
5568 var (
5669 checkpoint * types.Descriptor
5770 t containerd.Task
5871 err error
5972 )
6073
61- if checkpointDir != "" {
62- tar := archive .Diff (ctx , "" , checkpointDir )
74+ if opts . CheckpointDir != "" {
75+ tar := archive .Diff (ctx , "" , opts . CheckpointDir )
6376 cs := client .ContentStore ()
64- writer , err := cs .Writer (ctx , content .WithRef (checkpointDir ))
77+ writer , err := cs .Writer (ctx , content .WithRef (opts . CheckpointDir ))
6578 if err != nil {
6679 return nil , err
6780 }
@@ -96,8 +109,8 @@ func NewTask(ctx context.Context, client *containerd.Client, container container
96109 }
97110 }
98111 closer := func () {
99- if detachC != nil {
100- detachC <- struct {}{}
112+ if opts . DetachC != nil {
113+ opts . DetachC <- struct {}{}
101114 }
102115 // t will be set by container.NewTask at the end of this function.
103116 //
@@ -113,30 +126,30 @@ func NewTask(ctx context.Context, client *containerd.Client, container container
113126 io .Cancel ()
114127 }
115128 var ioCreator cio.Creator
116- if len (attachStreamOpt ) != 0 {
129+ if len (opts . AttachStreamOpt ) != 0 {
117130 log .G (ctx ).Debug ("attaching output instead of using the log-uri" )
118131 // when attaching a TTY we use writee for stdio and binary for log persistence
119- if isTerminal {
132+ if opts . IsTerminal {
120133 var in io.Reader
121- if isInteractive {
134+ if opts . IsInteractive {
122135 // FIXME: check IsTerminal on Windows too
123136 if runtime .GOOS != "windows" && ! term .IsTerminal (0 ) {
124137 return nil , errors .New ("the input device is not a TTY" )
125138 }
126139 var err error
127- in , err = consoleutil .NewDetachableStdin (con , detachKeys , closer )
140+ in , err = consoleutil .NewDetachableStdin (opts . Con , opts . DetachKeys , closer )
128141 if err != nil {
129142 return nil , err
130143 }
131144 }
132- ioCreator = cioutil .NewContainerIO (namespace , logURI , true , in , con , nil )
145+ ioCreator = cioutil .NewContainerIO (opts . Namespace , opts . LogURI , true , in , opts . Con , nil )
133146 } else {
134- streams := processAttachStreamsOpt (attachStreamOpt )
135- ioCreator = cioutil .NewContainerIO (namespace , logURI , false , streams .stdIn , streams .stdOut , streams .stdErr )
147+ streams := processAttachStreamsOpt (opts . AttachStreamOpt )
148+ ioCreator = cioutil .NewContainerIO (opts . Namespace , opts . LogURI , false , streams .stdIn , streams .stdOut , streams .stdErr )
136149 }
137150
138- } else if isTerminal && isDetach {
139- u , err := url .Parse (logURI )
151+ } else if opts . IsTerminal && opts . IsDetach {
152+ u , err := url .Parse (opts . LogURI )
140153 if err != nil {
141154 return nil , err
142155 }
@@ -162,32 +175,32 @@ func NewTask(ctx context.Context, client *containerd.Client, container container
162175 ioCreator = cio .TerminalBinaryIO (parsedPath , map [string ]string {
163176 args [0 ]: args [1 ],
164177 })
165- } else if isTerminal && ! isDetach {
166- if con == nil {
178+ } else if opts . IsTerminal && ! opts . IsDetach {
179+ if opts . Con == nil {
167180 return nil , errors .New ("got nil con with isTerminal=true" )
168181 }
169182 var in io.Reader
170- if isInteractive {
183+ if opts . IsInteractive {
171184 // FIXME: check IsTerminal on Windows too
172185 if runtime .GOOS != "windows" && ! term .IsTerminal (0 ) {
173186 return nil , errors .New ("the input device is not a TTY" )
174187 }
175188 var err error
176- in , err = consoleutil .NewDetachableStdin (con , detachKeys , closer )
189+ in , err = consoleutil .NewDetachableStdin (opts . Con , opts . DetachKeys , closer )
177190 if err != nil {
178191 return nil , err
179192 }
180193 }
181- ioCreator = cioutil .NewContainerIO (namespace , logURI , true , in , os .Stdout , os .Stderr )
182- } else if isDetach && logURI != "" && logURI != "none" {
183- u , err := url .Parse (logURI )
194+ ioCreator = cioutil .NewContainerIO (opts . Namespace , opts . LogURI , true , in , os .Stdout , os .Stderr )
195+ } else if opts . IsDetach && opts . LogURI != "" && opts . LogURI != "none" {
196+ u , err := url .Parse (opts . LogURI )
184197 if err != nil {
185198 return nil , err
186199 }
187200 ioCreator = cio .LogURI (u )
188201 } else {
189202 var in io.Reader
190- if isInteractive {
203+ if opts . IsInteractive {
191204 if sv , err := infoutil .ServerSemVer (ctx , client ); err != nil {
192205 log .G (ctx ).Warn (err )
193206 } else if sv .LessThan (semver .MustParse ("1.6.0-0" )) {
@@ -205,7 +218,7 @@ func NewTask(ctx context.Context, client *containerd.Client, container container
205218 }
206219 in = stdinC
207220 }
208- ioCreator = cioutil .NewContainerIO (namespace , logURI , false , in , os .Stdout , os .Stderr )
221+ ioCreator = cioutil .NewContainerIO (opts . Namespace , opts . LogURI , false , in , os .Stdout , os .Stderr )
209222 }
210223
211224 taskOpts := []containerd.NewTaskOpts {
0 commit comments