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/godbus/dbus/v5"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"golang.org/x/crypto/ssh/terminal" "golang.org/x/term"
) )
const ( const (
@ -445,7 +445,7 @@ func createContainer(container, image, release string, showCommandToEnter bool)
stdoutFd := os.Stdout.Fd() stdoutFd := os.Stdout.Fd()
stdoutFdInt := int(stdoutFd) 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.Prefix = fmt.Sprintf("Creating container %s: ", container)
s.Writer = os.Stdout s.Writer = os.Stdout
s.Start() 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) 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) { func pullImage(image, release string) (bool, error) {
if ok := utils.ImageReferenceCanBeID(image); ok { if ok := utils.ImageReferenceCanBeID(image); ok {
logrus.Debugf("Looking for image %s", image) logrus.Debugf("Looking for image %s", image)
@ -718,7 +694,7 @@ func pullImage(image, release string) (bool, error) {
stdoutFd := os.Stdout.Fd() stdoutFd := os.Stdout.Fd()
stdoutFdInt := int(stdoutFd) 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 := spinner.New(spinner.CharSets[9], 500*time.Millisecond)
s.Prefix = fmt.Sprintf("Pulling %s: ", imageFull) s.Prefix = fmt.Sprintf("Pulling %s: ", imageFull)
s.Writer = os.Stdout s.Writer = os.Stdout

View File

@ -85,7 +85,10 @@ func init() {
"home", "home",
"", "",
"Create a user inside the toolbox container whose login directory is 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, flags.BoolVar(&initContainerFlags.homeLink,
"home-link", "home-link",
@ -108,19 +111,28 @@ func init() {
"shell", "shell",
"", "",
"Create a user inside the toolbox container whose login shell is 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, flags.IntVar(&initContainerFlags.uid,
"uid", "uid",
0, 0,
"Create a user inside the toolbox container whose numerical user ID is UID") "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, flags.StringVar(&initContainerFlags.user,
"user", "user",
"", "",
"Create a user inside the toolbox container whose login name is 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) initContainerCmd.SetHelpFunc(initContainerHelp)
rootCmd.AddCommand(initContainerCmd) 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) logrus.Warnf("Received an error from the file system watcher: %v", err)
} }
} }
return nil
} }
func initContainerHelp(cmd *cobra.Command, args []string) { func initContainerHelp(cmd *cobra.Command, args []string) {

View File

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