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 <hudson@cyzhu.com>
This commit is contained in:
parent
bc05100159
commit
2eb82b6c61
|
|
@ -62,27 +62,27 @@ func execOptions(cmd *cobra.Command) (types.ContainerExecOptions, error) {
|
|||
return types.ContainerExecOptions{}, err
|
||||
}
|
||||
|
||||
flagI, err := cmd.Flags().GetBool("interactive")
|
||||
isInteractive, err := cmd.Flags().GetBool("interactive")
|
||||
if err != nil {
|
||||
return types.ContainerExecOptions{}, err
|
||||
}
|
||||
flagT, err := cmd.Flags().GetBool("tty")
|
||||
isTerminal, err := cmd.Flags().GetBool("tty")
|
||||
if err != nil {
|
||||
return types.ContainerExecOptions{}, err
|
||||
}
|
||||
flagD, err := cmd.Flags().GetBool("detach")
|
||||
isDetach, err := cmd.Flags().GetBool("detach")
|
||||
if err != nil {
|
||||
return types.ContainerExecOptions{}, err
|
||||
}
|
||||
|
||||
if flagI {
|
||||
if flagD {
|
||||
if isInteractive {
|
||||
if isDetach {
|
||||
return types.ContainerExecOptions{}, errors.New("currently flag -i and -d cannot be specified together (FIXME)")
|
||||
}
|
||||
}
|
||||
|
||||
if flagT {
|
||||
if flagD {
|
||||
if isTerminal {
|
||||
if isDetach {
|
||||
return types.ContainerExecOptions{}, errors.New("currently flag -t and -d cannot be specified together (FIXME)")
|
||||
}
|
||||
}
|
||||
|
|
@ -111,9 +111,9 @@ func execOptions(cmd *cobra.Command) (types.ContainerExecOptions, error) {
|
|||
|
||||
return types.ContainerExecOptions{
|
||||
GOptions: globalOptions,
|
||||
TTY: flagT,
|
||||
Interactive: flagI,
|
||||
Detach: flagD,
|
||||
TTY: isTerminal,
|
||||
Interactive: isInteractive,
|
||||
Detach: isDetach,
|
||||
Workdir: workdir,
|
||||
Env: env,
|
||||
EnvFile: envFile,
|
||||
|
|
|
|||
|
|
@ -204,7 +204,7 @@ func GenerateSharingPIDOpts(ctx context.Context, targetCon containerd.Container)
|
|||
}
|
||||
|
||||
// Start starts `container` with `attach` flag. If `attach` is true, it will attach to the container's stdio.
|
||||
func Start(ctx context.Context, container containerd.Container, flagA bool, flagI bool, client *containerd.Client, detachKeys string) (err error) {
|
||||
func Start(ctx context.Context, container containerd.Container, isAttach bool, isInteractive bool, client *containerd.Client, detachKeys string) (err error) {
|
||||
// defer the storage of start error in the dedicated label
|
||||
defer func() {
|
||||
if err != nil {
|
||||
|
|
@ -232,9 +232,9 @@ func Start(ctx context.Context, container containerd.Container, flagA bool, flag
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
flagT := process.Process.Terminal
|
||||
isTerminal := process.Process.Terminal
|
||||
var con console.Console
|
||||
if (flagI || flagA) && flagT {
|
||||
if (isInteractive || isAttach) && isTerminal {
|
||||
con, err = consoleutil.Current()
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -270,12 +270,12 @@ func Start(ctx context.Context, container containerd.Container, flagA bool, flag
|
|||
}
|
||||
detachC := make(chan struct{})
|
||||
attachStreamOpt := []string{}
|
||||
if flagA {
|
||||
// In start, flagA attaches only STDOUT/STDERR
|
||||
if isAttach {
|
||||
// In start, isAttach attaches only STDOUT/STDERR
|
||||
// source: https://github.com/containerd/nerdctl/blob/main/docs/command-reference.md#whale-nerdctl-start
|
||||
attachStreamOpt = []string{"STDOUT", "STDERR"}
|
||||
}
|
||||
task, err := taskutil.NewTask(ctx, client, container, attachStreamOpt, flagI, flagT, true, con, logURI, detachKeys, namespace, detachC)
|
||||
task, err := taskutil.NewTask(ctx, client, container, attachStreamOpt, isInteractive, isTerminal, true, con, logURI, detachKeys, namespace, detachC)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -283,10 +283,10 @@ func Start(ctx context.Context, container containerd.Container, flagA bool, flag
|
|||
if err := task.Start(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if !flagA {
|
||||
if !isAttach {
|
||||
return nil
|
||||
}
|
||||
if flagA && flagT {
|
||||
if isAttach && isTerminal {
|
||||
if err := consoleutil.HandleConsoleResize(ctx, task, con); err != nil {
|
||||
log.G(ctx).WithError(err).Error("console resize")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ import (
|
|||
|
||||
// NewTask is from https://github.com/containerd/containerd/blob/v1.4.3/cmd/ctr/commands/tasks/tasks_unix.go#L70-L108
|
||||
func NewTask(ctx context.Context, client *containerd.Client, container containerd.Container,
|
||||
attachStreamOpt []string, flagI, flagT, flagD bool, con console.Console, logURI, detachKeys, namespace string, detachC chan<- struct{}) (containerd.Task, error) {
|
||||
attachStreamOpt []string, isInteractive, isTerminal, isDetach bool, con console.Console, logURI, detachKeys, namespace string, detachC chan<- struct{}) (containerd.Task, error) {
|
||||
|
||||
var t containerd.Task
|
||||
closer := func() {
|
||||
|
|
@ -67,9 +67,9 @@ func NewTask(ctx context.Context, client *containerd.Client, container container
|
|||
if len(attachStreamOpt) != 0 {
|
||||
log.G(ctx).Debug("attaching output instead of using the log-uri")
|
||||
// when attaching a TTY we use writee for stdio and binary for log persistence
|
||||
if flagT {
|
||||
if isTerminal {
|
||||
var in io.Reader
|
||||
if flagI {
|
||||
if isInteractive {
|
||||
// FIXME: check IsTerminal on Windows too
|
||||
if runtime.GOOS != "windows" && !term.IsTerminal(0) {
|
||||
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
|
|||
ioCreator = cioutil.NewContainerIO(namespace, logURI, false, streams.stdIn, streams.stdOut, streams.stdErr)
|
||||
}
|
||||
|
||||
} else if flagT && flagD {
|
||||
} else if isTerminal && isDetach {
|
||||
u, err := url.Parse(logURI)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -113,12 +113,12 @@ func NewTask(ctx context.Context, client *containerd.Client, container container
|
|||
ioCreator = cio.TerminalBinaryIO(parsedPath, map[string]string{
|
||||
args[0]: args[1],
|
||||
})
|
||||
} else if flagT && !flagD {
|
||||
} else if isTerminal && !isDetach {
|
||||
if con == nil {
|
||||
return nil, errors.New("got nil con with flagT=true")
|
||||
return nil, errors.New("got nil con with isTerminal=true")
|
||||
}
|
||||
var in io.Reader
|
||||
if flagI {
|
||||
if isInteractive {
|
||||
// FIXME: check IsTerminal on Windows too
|
||||
if runtime.GOOS != "windows" && !term.IsTerminal(0) {
|
||||
return nil, errors.New("the input device is not a TTY")
|
||||
|
|
@ -130,7 +130,7 @@ func NewTask(ctx context.Context, client *containerd.Client, container container
|
|||
}
|
||||
}
|
||||
ioCreator = cioutil.NewContainerIO(namespace, logURI, true, in, os.Stdout, os.Stderr)
|
||||
} else if flagD && logURI != "" && logURI != "none" {
|
||||
} else if isDetach && logURI != "" && logURI != "none" {
|
||||
u, err := url.Parse(logURI)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -138,7 +138,7 @@ func NewTask(ctx context.Context, client *containerd.Client, container container
|
|||
ioCreator = cio.LogURI(u)
|
||||
} else {
|
||||
var in io.Reader
|
||||
if flagI {
|
||||
if isInteractive {
|
||||
if sv, err := infoutil.ServerSemVer(ctx, client); err != nil {
|
||||
log.G(ctx).Warn(err)
|
||||
} else if sv.LessThan(semver.MustParse("1.6.0-0")) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue