diff --git a/cmd/limactl/guest_install.go b/cmd/limactl/guest_install.go index 79e3038be768..0b5d42365a15 100644 --- a/cmd/limactl/guest_install.go +++ b/cmd/limactl/guest_install.go @@ -27,6 +27,14 @@ func newGuestInstallCommand() *cobra.Command { return guestInstallCommand } +func runCmd(name string, flags []string, args ...string) error { + cmd := exec.Command(name, append(flags, args...)...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + logrus.Debugf("executing %v", cmd.Args) + return cmd.Run() +} + func guestInstallAction(_ *cobra.Command, args []string) error { instName := DefaultInstanceName if len(args) > 0 { @@ -61,20 +69,12 @@ func guestInstallAction(_ *cobra.Command, args []string) error { bin := prefix + "/bin/lima-guestagent" logrus.Infof("Copying %q to %s", guestAgentBinary, hostname) scpArgs := []string{guestAgentBinary, hostname + ":" + tmp} - scpCmd := exec.Command("scp", append(sshFlags, scpArgs...)...) - scpCmd.Stdout = os.Stdout - scpCmd.Stderr = os.Stderr - logrus.Debugf("executing %v", scpCmd.Args) - if err := scpCmd.Run(); err != nil { + if err := runCmd("scp", sshFlags, scpArgs...); err != nil { return nil } logrus.Infof("Installing %s to %s", tmp, bin) sshArgs := []string{hostname, "sudo", "install", "-m", "755", tmp, bin} - sshCmd := exec.Command("ssh", append(sshFlags, sshArgs...)...) - sshCmd.Stdout = os.Stdout - sshCmd.Stderr = os.Stderr - logrus.Debugf("executing %v", sshCmd.Args) - if err := sshCmd.Run(); err != nil { + if err := runCmd("ssh", sshFlags, sshArgs...); err != nil { return nil } @@ -88,20 +88,12 @@ func guestInstallAction(_ *cobra.Command, args []string) error { tmp := "/tmp/nerdctl-full.tgz" logrus.Infof("Copying %q to %s", nerdctlFilename, hostname) scpArgs := []string{nerdctlArchive, hostname + ":" + tmp} - scpCmd := exec.Command("scp", append(sshFlags, scpArgs...)...) - scpCmd.Stdout = os.Stdout - scpCmd.Stderr = os.Stderr - logrus.Debugf("executing %v", scpCmd.Args) - if err := scpCmd.Run(); err != nil { + if err := runCmd("scp", sshFlags, scpArgs...); err != nil { return nil } logrus.Infof("Installing %s in %s", tmp, prefix) sshArgs := []string{hostname, "sudo", "tar", "Cxzf", prefix, tmp} - sshCmd := exec.Command("ssh", append(sshFlags, sshArgs...)...) - sshCmd.Stdout = os.Stdout - sshCmd.Stderr = os.Stderr - logrus.Debugf("executing %v", sshCmd.Args) - if err := sshCmd.Run(); err != nil { + if err := runCmd("ssh", sshFlags, sshArgs...); err != nil { return nil } }