Skip to content

Commit d323143

Browse files
committed
cmd: Fix issues raised by Golangci
Issues included: - unreachable code - unneeded type assertions - unhandled errors - use of deprecated API - unused code containers#979
1 parent 46ff946 commit d323143

File tree

3 files changed

+22
-39
lines changed

3 files changed

+22
-39
lines changed

src/cmd/create.go

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
"github.com/godbus/dbus/v5"
3232
"github.com/sirupsen/logrus"
3333
"github.com/spf13/cobra"
34-
"golang.org/x/crypto/ssh/terminal"
34+
"golang.org/x/term"
3535
)
3636

3737
const (
@@ -445,7 +445,7 @@ func createContainer(container, image, release string, showCommandToEnter bool)
445445

446446
stdoutFd := os.Stdout.Fd()
447447
stdoutFdInt := int(stdoutFd)
448-
if logLevel := logrus.GetLevel(); logLevel < logrus.DebugLevel && terminal.IsTerminal(stdoutFdInt) {
448+
if logLevel := logrus.GetLevel(); logLevel < logrus.DebugLevel && term.IsTerminal(stdoutFdInt) {
449449
s.Prefix = fmt.Sprintf("Creating container %s: ", container)
450450
s.Writer = os.Stdout
451451
s.Start()
@@ -627,30 +627,6 @@ func getServiceSocket(serviceName string, unitName string) (string, error) {
627627
return "", fmt.Errorf("failed to find a SOCK_STREAM socket for %s", unitName)
628628
}
629629

630-
func isPathReadWrite(path string) (bool, error) {
631-
logrus.Debugf("Checking if %s is mounted read-only or read-write", path)
632-
633-
mountPoint, err := utils.GetMountPoint(path)
634-
if err != nil {
635-
return false, fmt.Errorf("failed to get the mount-point of %s: %s", path, err)
636-
}
637-
638-
logrus.Debugf("Mount-point of %s is %s", path, mountPoint)
639-
640-
mountFlags, err := utils.GetMountOptions(mountPoint)
641-
if err != nil {
642-
return false, fmt.Errorf("failed to get the mount options of %s: %s", mountPoint, err)
643-
}
644-
645-
logrus.Debugf("Mount flags of %s on the host are %s", path, mountFlags)
646-
647-
if !strings.Contains(mountFlags, "ro") {
648-
return true, nil
649-
}
650-
651-
return false, nil
652-
}
653-
654630
func pullImage(image, release string) (bool, error) {
655631
if ok := utils.ImageReferenceCanBeID(image); ok {
656632
logrus.Debugf("Looking for image %s", image)
@@ -718,7 +694,7 @@ func pullImage(image, release string) (bool, error) {
718694

719695
stdoutFd := os.Stdout.Fd()
720696
stdoutFdInt := int(stdoutFd)
721-
if logLevel := logrus.GetLevel(); logLevel < logrus.DebugLevel && terminal.IsTerminal(stdoutFdInt) {
697+
if logLevel := logrus.GetLevel(); logLevel < logrus.DebugLevel && term.IsTerminal(stdoutFdInt) {
722698
s := spinner.New(spinner.CharSets[9], 500*time.Millisecond)
723699
s.Prefix = fmt.Sprintf("Pulling %s: ", imageFull)
724700
s.Writer = os.Stdout

src/cmd/initContainer.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ func init() {
8585
"home",
8686
"",
8787
"Create a user inside the toolbox container whose login directory is HOME")
88-
initContainerCmd.MarkFlagRequired("home")
88+
err := initContainerCmd.MarkFlagRequired("home")
89+
if err != nil {
90+
panic("Could not mark flag --home as required")
91+
}
8992

9093
flags.BoolVar(&initContainerFlags.homeLink,
9194
"home-link",
@@ -108,19 +111,28 @@ func init() {
108111
"shell",
109112
"",
110113
"Create a user inside the toolbox container whose login shell is SHELL")
111-
initContainerCmd.MarkFlagRequired("shell")
114+
err = initContainerCmd.MarkFlagRequired("shell")
115+
if err != nil {
116+
panic("Could not mark flag --shell as required")
117+
}
112118

113119
flags.IntVar(&initContainerFlags.uid,
114120
"uid",
115121
0,
116122
"Create a user inside the toolbox container whose numerical user ID is UID")
117-
initContainerCmd.MarkFlagRequired("uid")
123+
err = initContainerCmd.MarkFlagRequired("uid")
124+
if err != nil {
125+
panic("Could not mark flag --uid as required")
126+
}
118127

119128
flags.StringVar(&initContainerFlags.user,
120129
"user",
121130
"",
122131
"Create a user inside the toolbox container whose login name is USER")
123-
initContainerCmd.MarkFlagRequired("user")
132+
err = initContainerCmd.MarkFlagRequired("user")
133+
if err != nil {
134+
panic("Could not mark flag --user as required")
135+
}
124136

125137
initContainerCmd.SetHelpFunc(initContainerHelp)
126138
rootCmd.AddCommand(initContainerCmd)
@@ -349,8 +361,6 @@ func initContainer(cmd *cobra.Command, args []string) error {
349361
logrus.Warnf("Received an error from the file system watcher: %v", err)
350362
}
351363
}
352-
353-
return nil
354364
}
355365

356366
func initContainerHelp(cmd *cobra.Command, args []string) {

src/cmd/run.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,6 @@ func runCommandWithFallbacks(container string, command []string, emitEscapeSeque
364364
return nil
365365
}
366366
}
367-
368-
panic("code should not be reached")
369367
}
370368

371369
func runHelp(cmd *cobra.Command, args []string) {
@@ -476,12 +474,11 @@ func getEntryPointAndPID(container string) (string, int, error) {
476474

477475
var entryPointPIDInt int
478476

479-
switch entryPointPID.(type) {
477+
switch entryPointPID := entryPointPID.(type) {
480478
case float64:
481-
entryPointPIDFloat := entryPointPID.(float64)
482-
entryPointPIDInt = int(entryPointPIDFloat)
479+
entryPointPIDInt = int(entryPointPID)
483480
case int:
484-
entryPointPIDInt = entryPointPID.(int)
481+
entryPointPIDInt = entryPointPID
485482
default:
486483
return "", 0, fmt.Errorf("failed to inspect entry point PID of container %s", container)
487484
}

0 commit comments

Comments
 (0)