Skip to content

Commit

Permalink
refactor: Add helper function for run cmd
Browse files Browse the repository at this point in the history
Signed-off-by: Anders F Björklund <[email protected]>
  • Loading branch information
afbjorklund committed Jan 2, 2024
1 parent 88f9b9d commit 23fd254
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions cmd/limactl/guest_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}
}
Expand Down

0 comments on commit 23fd254

Please sign in to comment.