Skip to content

Commit 2eb82b6

Browse files
committed
rename flag variables to descriptive boolean names
Improves code readability by replacing short flag names (flagA, flagI, flagT, flagD) with descriptive boolean names (isAttach, isInteractive, isTerminal, isDetach). Signed-off-by: ChengyuZhu6 <[email protected]>
1 parent bc05100 commit 2eb82b6

File tree

3 files changed

+27
-27
lines changed

3 files changed

+27
-27
lines changed

cmd/nerdctl/container/container_exec.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,27 +62,27 @@ func execOptions(cmd *cobra.Command) (types.ContainerExecOptions, error) {
6262
return types.ContainerExecOptions{}, err
6363
}
6464

65-
flagI, err := cmd.Flags().GetBool("interactive")
65+
isInteractive, err := cmd.Flags().GetBool("interactive")
6666
if err != nil {
6767
return types.ContainerExecOptions{}, err
6868
}
69-
flagT, err := cmd.Flags().GetBool("tty")
69+
isTerminal, err := cmd.Flags().GetBool("tty")
7070
if err != nil {
7171
return types.ContainerExecOptions{}, err
7272
}
73-
flagD, err := cmd.Flags().GetBool("detach")
73+
isDetach, err := cmd.Flags().GetBool("detach")
7474
if err != nil {
7575
return types.ContainerExecOptions{}, err
7676
}
7777

78-
if flagI {
79-
if flagD {
78+
if isInteractive {
79+
if isDetach {
8080
return types.ContainerExecOptions{}, errors.New("currently flag -i and -d cannot be specified together (FIXME)")
8181
}
8282
}
8383

84-
if flagT {
85-
if flagD {
84+
if isTerminal {
85+
if isDetach {
8686
return types.ContainerExecOptions{}, errors.New("currently flag -t and -d cannot be specified together (FIXME)")
8787
}
8888
}
@@ -111,9 +111,9 @@ func execOptions(cmd *cobra.Command) (types.ContainerExecOptions, error) {
111111

112112
return types.ContainerExecOptions{
113113
GOptions: globalOptions,
114-
TTY: flagT,
115-
Interactive: flagI,
116-
Detach: flagD,
114+
TTY: isTerminal,
115+
Interactive: isInteractive,
116+
Detach: isDetach,
117117
Workdir: workdir,
118118
Env: env,
119119
EnvFile: envFile,

pkg/containerutil/containerutil.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ func GenerateSharingPIDOpts(ctx context.Context, targetCon containerd.Container)
204204
}
205205

206206
// Start starts `container` with `attach` flag. If `attach` is true, it will attach to the container's stdio.
207-
func Start(ctx context.Context, container containerd.Container, flagA bool, flagI bool, client *containerd.Client, detachKeys string) (err error) {
207+
func Start(ctx context.Context, container containerd.Container, isAttach bool, isInteractive bool, client *containerd.Client, detachKeys string) (err error) {
208208
// defer the storage of start error in the dedicated label
209209
defer func() {
210210
if err != nil {
@@ -232,9 +232,9 @@ func Start(ctx context.Context, container containerd.Container, flagA bool, flag
232232
if err != nil {
233233
return err
234234
}
235-
flagT := process.Process.Terminal
235+
isTerminal := process.Process.Terminal
236236
var con console.Console
237-
if (flagI || flagA) && flagT {
237+
if (isInteractive || isAttach) && isTerminal {
238238
con, err = consoleutil.Current()
239239
if err != nil {
240240
return err
@@ -270,23 +270,23 @@ func Start(ctx context.Context, container containerd.Container, flagA bool, flag
270270
}
271271
detachC := make(chan struct{})
272272
attachStreamOpt := []string{}
273-
if flagA {
274-
// In start, flagA attaches only STDOUT/STDERR
273+
if isAttach {
274+
// In start, isAttach attaches only STDOUT/STDERR
275275
// source: https://github.com/containerd/nerdctl/blob/main/docs/command-reference.md#whale-nerdctl-start
276276
attachStreamOpt = []string{"STDOUT", "STDERR"}
277277
}
278-
task, err := taskutil.NewTask(ctx, client, container, attachStreamOpt, flagI, flagT, true, con, logURI, detachKeys, namespace, detachC)
278+
task, err := taskutil.NewTask(ctx, client, container, attachStreamOpt, isInteractive, isTerminal, true, con, logURI, detachKeys, namespace, detachC)
279279
if err != nil {
280280
return err
281281
}
282282

283283
if err := task.Start(ctx); err != nil {
284284
return err
285285
}
286-
if !flagA {
286+
if !isAttach {
287287
return nil
288288
}
289-
if flagA && flagT {
289+
if isAttach && isTerminal {
290290
if err := consoleutil.HandleConsoleResize(ctx, task, con); err != nil {
291291
log.G(ctx).WithError(err).Error("console resize")
292292
}

pkg/taskutil/taskutil.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import (
4343

4444
// NewTask is from https://github.com/containerd/containerd/blob/v1.4.3/cmd/ctr/commands/tasks/tasks_unix.go#L70-L108
4545
func NewTask(ctx context.Context, client *containerd.Client, container containerd.Container,
46-
attachStreamOpt []string, flagI, flagT, flagD bool, con console.Console, logURI, detachKeys, namespace string, detachC chan<- struct{}) (containerd.Task, error) {
46+
attachStreamOpt []string, isInteractive, isTerminal, isDetach bool, con console.Console, logURI, detachKeys, namespace string, detachC chan<- struct{}) (containerd.Task, error) {
4747

4848
var t containerd.Task
4949
closer := func() {
@@ -67,9 +67,9 @@ func NewTask(ctx context.Context, client *containerd.Client, container container
6767
if len(attachStreamOpt) != 0 {
6868
log.G(ctx).Debug("attaching output instead of using the log-uri")
6969
// when attaching a TTY we use writee for stdio and binary for log persistence
70-
if flagT {
70+
if isTerminal {
7171
var in io.Reader
72-
if flagI {
72+
if isInteractive {
7373
// FIXME: check IsTerminal on Windows too
7474
if runtime.GOOS != "windows" && !term.IsTerminal(0) {
7575
return nil, errors.New("the input device is not a TTY")
@@ -86,7 +86,7 @@ func NewTask(ctx context.Context, client *containerd.Client, container container
8686
ioCreator = cioutil.NewContainerIO(namespace, logURI, false, streams.stdIn, streams.stdOut, streams.stdErr)
8787
}
8888

89-
} else if flagT && flagD {
89+
} else if isTerminal && isDetach {
9090
u, err := url.Parse(logURI)
9191
if err != nil {
9292
return nil, err
@@ -113,12 +113,12 @@ func NewTask(ctx context.Context, client *containerd.Client, container container
113113
ioCreator = cio.TerminalBinaryIO(parsedPath, map[string]string{
114114
args[0]: args[1],
115115
})
116-
} else if flagT && !flagD {
116+
} else if isTerminal && !isDetach {
117117
if con == nil {
118-
return nil, errors.New("got nil con with flagT=true")
118+
return nil, errors.New("got nil con with isTerminal=true")
119119
}
120120
var in io.Reader
121-
if flagI {
121+
if isInteractive {
122122
// FIXME: check IsTerminal on Windows too
123123
if runtime.GOOS != "windows" && !term.IsTerminal(0) {
124124
return nil, errors.New("the input device is not a TTY")
@@ -130,15 +130,15 @@ func NewTask(ctx context.Context, client *containerd.Client, container container
130130
}
131131
}
132132
ioCreator = cioutil.NewContainerIO(namespace, logURI, true, in, os.Stdout, os.Stderr)
133-
} else if flagD && logURI != "" && logURI != "none" {
133+
} else if isDetach && logURI != "" && logURI != "none" {
134134
u, err := url.Parse(logURI)
135135
if err != nil {
136136
return nil, err
137137
}
138138
ioCreator = cio.LogURI(u)
139139
} else {
140140
var in io.Reader
141-
if flagI {
141+
if isInteractive {
142142
if sv, err := infoutil.ServerSemVer(ctx, client); err != nil {
143143
log.G(ctx).Warn(err)
144144
} else if sv.LessThan(semver.MustParse("1.6.0-0")) {

0 commit comments

Comments
 (0)