cmd: Fix issues raised by Golangci

Issues included:

- unreachable code
- unneeded type assertions
- unhandled errors
- use of deprecated API
- unused code

https://github.com/containers/toolbox/pull/979
This commit is contained in:
Ondřej Míchal 2022-01-09 18:20:24 +02:00
parent 46ff946190
commit d323143c46
3 changed files with 22 additions and 39 deletions

View File

@ -31,7 +31,7 @@ import (
"github.com/godbus/dbus/v5"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/term"
)
const (
@ -445,7 +445,7 @@ func createContainer(container, image, release string, showCommandToEnter bool)
stdoutFd := os.Stdout.Fd()
stdoutFdInt := int(stdoutFd)
if logLevel := logrus.GetLevel(); logLevel < logrus.DebugLevel && terminal.IsTerminal(stdoutFdInt) {
if logLevel := logrus.GetLevel(); logLevel < logrus.DebugLevel && term.IsTerminal(stdoutFdInt) {
s.Prefix = fmt.Sprintf("Creating container %s: ", container)
s.Writer = os.Stdout
s.Start()
@ -627,30 +627,6 @@ func getServiceSocket(serviceName string, unitName string) (string, error) {
return "", fmt.Errorf("failed to find a SOCK_STREAM socket for %s", unitName)
}
func isPathReadWrite(path string) (bool, error) {
logrus.Debugf("Checking if %s is mounted read-only or read-write", path)
mountPoint, err := utils.GetMountPoint(path)
if err != nil {
return false, fmt.Errorf("failed to get the mount-point of %s: %s", path, err)
}
logrus.Debugf("Mount-point of %s is %s", path, mountPoint)
mountFlags, err := utils.GetMountOptions(mountPoint)
if err != nil {
return false, fmt.Errorf("failed to get the mount options of %s: %s", mountPoint, err)
}
logrus.Debugf("Mount flags of %s on the host are %s", path, mountFlags)
if !strings.Contains(mountFlags, "ro") {
return true, nil
}
return false, nil
}
func pullImage(image, release string) (bool, error) {
if ok := utils.ImageReferenceCanBeID(image); ok {
logrus.Debugf("Looking for image %s", image)
@ -718,7 +694,7 @@ func pullImage(image, release string) (bool, error) {
stdoutFd := os.Stdout.Fd()
stdoutFdInt := int(stdoutFd)
if logLevel := logrus.GetLevel(); logLevel < logrus.DebugLevel && terminal.IsTerminal(stdoutFdInt) {
if logLevel := logrus.GetLevel(); logLevel < logrus.DebugLevel && term.IsTerminal(stdoutFdInt) {
s := spinner.New(spinner.CharSets[9], 500*time.Millisecond)
s.Prefix = fmt.Sprintf("Pulling %s: ", imageFull)
s.Writer = os.Stdout

View File

@ -85,7 +85,10 @@ func init() {
"home",
"",
"Create a user inside the toolbox container whose login directory is HOME")
initContainerCmd.MarkFlagRequired("home")
err := initContainerCmd.MarkFlagRequired("home")
if err != nil {
panic("Could not mark flag --home as required")
}
flags.BoolVar(&initContainerFlags.homeLink,
"home-link",
@ -108,19 +111,28 @@ func init() {
"shell",
"",
"Create a user inside the toolbox container whose login shell is SHELL")
initContainerCmd.MarkFlagRequired("shell")
err = initContainerCmd.MarkFlagRequired("shell")
if err != nil {
panic("Could not mark flag --shell as required")
}
flags.IntVar(&initContainerFlags.uid,
"uid",
0,
"Create a user inside the toolbox container whose numerical user ID is UID")
initContainerCmd.MarkFlagRequired("uid")
err = initContainerCmd.MarkFlagRequired("uid")
if err != nil {
panic("Could not mark flag --uid as required")
}
flags.StringVar(&initContainerFlags.user,
"user",
"",
"Create a user inside the toolbox container whose login name is USER")
initContainerCmd.MarkFlagRequired("user")
err = initContainerCmd.MarkFlagRequired("user")
if err != nil {
panic("Could not mark flag --user as required")
}
initContainerCmd.SetHelpFunc(initContainerHelp)
rootCmd.AddCommand(initContainerCmd)
@ -349,8 +361,6 @@ func initContainer(cmd *cobra.Command, args []string) error {
logrus.Warnf("Received an error from the file system watcher: %v", err)
}
}
return nil
}
func initContainerHelp(cmd *cobra.Command, args []string) {

View File

@ -364,8 +364,6 @@ func runCommandWithFallbacks(container string, command []string, emitEscapeSeque
return nil
}
}
panic("code should not be reached")
}
func runHelp(cmd *cobra.Command, args []string) {
@ -476,12 +474,11 @@ func getEntryPointAndPID(container string) (string, int, error) {
var entryPointPIDInt int
switch entryPointPID.(type) {
switch entryPointPID := entryPointPID.(type) {
case float64:
entryPointPIDFloat := entryPointPID.(float64)
entryPointPIDInt = int(entryPointPIDFloat)
entryPointPIDInt = int(entryPointPID)
case int:
entryPointPIDInt = entryPointPID.(int)
entryPointPIDInt = entryPointPID
default:
return "", 0, fmt.Errorf("failed to inspect entry point PID of container %s", container)
}