Fix QF1003: could use tagged switch" staticcheck warning

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
Kir Kolyshkin 2025-03-27 15:14:48 -07:00
parent 0dddc5e3c0
commit 0105131b5c
8 changed files with 40 additions and 34 deletions

View File

@ -1182,9 +1182,10 @@ func (c *Container) cleanupRuntime(ctx context.Context) error {
// If we were Stopped, we are now Exited, as we've removed ourself
// from the runtime.
// If we were Created, we are now Configured.
if c.state.State == define.ContainerStateStopped {
switch c.state.State {
case define.ContainerStateStopped:
c.state.State = define.ContainerStateExited
} else if c.state.State == define.ContainerStateCreated {
case define.ContainerStateCreated:
c.state.State = define.ContainerStateConfigured
}
@ -1219,10 +1220,11 @@ func (c *Container) reinit(ctx context.Context, retainRetries bool) error {
// Performs all necessary steps to start a container that is not running
// Does not lock or check validity, requires to run on the same thread that holds the lock for the container.
func (c *Container) initAndStart(ctx context.Context) (retErr error) {
// If we are ContainerStateUnknown, throw an error
if c.state.State == define.ContainerStateUnknown {
// If we are ContainerState{Unknown,Removing}, throw an error.
switch c.state.State {
case define.ContainerStateUnknown:
return fmt.Errorf("container %s is in an unknown state: %w", c.ID(), define.ErrCtrStateInvalid)
} else if c.state.State == define.ContainerStateRemoving {
case define.ContainerStateRemoving:
return fmt.Errorf("cannot start container %s as it is being removed: %w", c.ID(), define.ErrCtrStateInvalid)
}
@ -1688,14 +1690,14 @@ func (c *Container) restartWithTimeout(ctx context.Context, timeout uint) (retEr
return err
}
if c.state.State == define.ContainerStateStopped {
// Reinitialize the container if we need to
switch c.state.State {
case define.ContainerStateStopped:
// Reinitialize the container if we need to.
if err := c.reinit(ctx, false); err != nil {
return err
}
} else if c.state.State == define.ContainerStateConfigured ||
c.state.State == define.ContainerStateExited {
// Initialize the container
case define.ContainerStateConfigured, define.ContainerStateExited:
// Initialize the container.
if err := c.init(ctx, false); err != nil {
return err
}

View File

@ -229,26 +229,26 @@ func Attach(ctx context.Context, nameOrID string, stdin io.Reader, stdout io.Wri
return err
}
switch {
case fd == 0:
switch fd {
case 0:
if isSet.stdout {
if _, err := stdout.Write(frame[0:l]); err != nil {
return err
}
}
case fd == 1:
case 1:
if isSet.stdout {
if _, err := stdout.Write(frame[0:l]); err != nil {
return err
}
}
case fd == 2:
case 2:
if isSet.stderr {
if _, err := stderr.Write(frame[0:l]); err != nil {
return err
}
}
case fd == 3:
case 3:
return fmt.Errorf("from service from stream: %s", frame)
default:
return fmt.Errorf("unrecognized channel '%d' in header, 0-3 supported", fd)
@ -558,8 +558,8 @@ func ExecStartAndAttach(ctx context.Context, sessionID string, options *ExecStar
return err
}
switch {
case fd == 0:
switch fd {
case 0:
if options.GetAttachInput() {
// Write STDIN to STDOUT (echoing characters
// typed by another attach session)
@ -567,19 +567,19 @@ func ExecStartAndAttach(ctx context.Context, sessionID string, options *ExecStar
return err
}
}
case fd == 1:
case 1:
if options.GetAttachOutput() {
if _, err := options.GetOutputStream().Write(frame[0:l]); err != nil {
return err
}
}
case fd == 2:
case 2:
if options.GetAttachError() {
if _, err := options.GetErrorStream().Write(frame[0:l]); err != nil {
return err
}
}
case fd == 3:
case 3:
return fmt.Errorf("from service from stream: %s", frame)
default:
return fmt.Errorf("unrecognized channel '%d' in header, 0-3 supported", fd)

View File

@ -73,9 +73,10 @@ func GenerateContainerFilterFuncs(filter string, filterValues []string, r *libpo
return false
}
state := status.String()
if status == define.ContainerStateConfigured {
switch status {
case define.ContainerStateConfigured:
state = "created"
} else if status == define.ContainerStateStopped {
case define.ContainerStateStopped:
state = "exited"
}
for _, filterValue := range filterValues {

View File

@ -75,9 +75,10 @@ func GeneratePodFilterFunc(filter string, filterValues []string, r *libpod.Runti
}
for _, ctrStatus := range ctrStatuses {
state := ctrStatus.String()
if ctrStatus == define.ContainerStateConfigured {
switch ctrStatus {
case define.ContainerStateConfigured:
state = "created"
} else if ctrStatus == define.ContainerStateStopped {
case define.ContainerStateStopped:
state = "exited"
}
for _, filterValue := range filterValues {

View File

@ -347,9 +347,10 @@ func ParseQuantity(str string) (Quantity, error) {
}
// So that no one but us has to think about suffixes, remove it.
if base == 10 {
switch base {
case 10:
amount.SetScale(amount.Scale() + Scale(exponent).infScale())
} else if base == 2 {
case 2:
// numericSuffix = 2 ** exponent
numericSuffix := big.NewInt(1).Lsh(bigOne, uint(exponent))
ub := amount.UnscaledBig()

View File

@ -288,11 +288,11 @@ func ParseCgroupNamespace(ns string) (Namespace, error) {
// form.
func ParseIPCNamespace(ns string) (Namespace, error) {
toReturn := Namespace{}
switch {
case ns == "shareable", ns == "":
switch ns {
case "shareable", "":
toReturn.NSMode = Shareable
return toReturn, nil
case ns == "none":
case "none":
toReturn.NSMode = None
return toReturn, nil
}

View File

@ -238,8 +238,8 @@ func setContainerNameForTemplate(startCommand []string, info *containerInfo) ([]
break
}
}
switch {
case nameIx == -1:
switch nameIx {
case -1:
// if not found, add --name argument in the command slice before the "run" argument.
// it's assumed that the command slice contains this argument.
runIx := -1

View File

@ -704,11 +704,12 @@ var _ = Describe("Podman kube generate", func() {
// have anything for protocol under the ports as tcp is the default
// for k8s
Expect(port.Protocol).To(BeEmpty())
if port.HostPort == 4008 {
switch port.HostPort {
case 4008:
foundPort400x++
} else if port.HostPort == 5008 {
case 5008:
foundPort500x++
} else {
default:
foundOtherPort++
}
}