Skip to content

Commit b2a5dcc

Browse files
author
dwc0011
committed
refactor to declare variables outside of if
1 parent acfb6fe commit b2a5dcc

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

builder/azure/chroot/step_mount_device.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"context"
1111
"fmt"
1212
"os"
13+
"os/exec"
1314
"path/filepath"
1415
"runtime"
1516
"strings"
@@ -38,13 +39,7 @@ func (s *StepMountDevice) Run(ctx context.Context, state multistep.StateBag) mul
3839
ui := state.Get("ui").(packersdk.Ui)
3940
device := state.Get("device").(string)
4041
config := state.Get("config").(*Config)
41-
4242
isManualMount := s.Command != ""
43-
44-
if !isManualMount {
45-
wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper)
46-
}
47-
4843
ictx := config.ctx
4944

5045
ictx.Data = &struct{ Device string }{Device: filepath.Base(device)}
@@ -87,13 +82,15 @@ func (s *StepMountDevice) Run(ctx context.Context, state multistep.StateBag) mul
8782

8883
ui.Say("Mounting the root device...")
8984
stderr := new(bytes.Buffer)
85+
var cmd *exec.Cmd
9086
if !isManualMount {
9187
// build mount options from mount_options config, useful for nouuid options
9288
// or other specific device type settings for mount
9389
opts := ""
9490
if len(s.MountOptions) > 0 {
9591
opts = "-o " + strings.Join(s.MountOptions, " -o ")
9692
}
93+
wrappedCommand := state.Get("wrappedCommand").(common.CommandWrapper)
9794
mountCommand, err := wrappedCommand(
9895
fmt.Sprintf("mount %s %s %s", opts, deviceMount, mountPath))
9996
if err != nil {
@@ -103,11 +100,11 @@ func (s *StepMountDevice) Run(ctx context.Context, state multistep.StateBag) mul
103100
return multistep.ActionHalt
104101
}
105102
log.Printf("[DEBUG] (step mount) mount command is %s", mountCommand)
106-
cmd := common.ShellCommand(mountCommand)
103+
cmd = common.ShellCommand(mountCommand)
107104

108105
} else {
109106
log.Printf("[DEBUG] (step mount) mount command is %s", s.Command)
110-
cmd := common.ShellCommand(fmt.Sprintf("%s %s", s.Command, mountPath))
107+
cmd = common.ShellCommand(fmt.Sprintf("%s %s", s.Command, mountPath))
111108
}
112109

113110
cmd.Stderr = stderr

0 commit comments

Comments
 (0)