Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 9c3c5aa

Browse files
committed
Replace fmt.Sprintf w/ fmt.Errorf where possible
- normalize spacing
1 parent fbdd744 commit 9c3c5aa

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

iaas_cli/iaas_clients/vcenter_client.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package iaas_clients
22

33
import (
44
"encoding/json"
5-
"errors"
65
"fmt"
76
"net/url"
87
"os"
@@ -21,27 +20,34 @@ type VcenterClient struct {
2120
}
2221

2322
func NewVcenterClient(username string, password string, u string, caCertFile string, runner iaas_cli.CliRunner) *VcenterClient {
24-
2523
encodedUser := url.QueryEscape(username)
2624
encodedPassword := url.QueryEscape(password)
2725
urlWithCredentials := fmt.Sprintf("%s:%s@%s", encodedUser, encodedPassword, u)
2826
urlWithRedactedPassword := fmt.Sprintf("%s:REDACTED@%s", encodedUser, u)
29-
return &VcenterClient{Url: u, credentialUrl: urlWithCredentials, redactedUrl: urlWithRedactedPassword, caCertFile: caCertFile, Runner: runner}
27+
28+
return &VcenterClient{
29+
Url: u,
30+
credentialUrl: urlWithCredentials,
31+
redactedUrl: urlWithRedactedPassword,
32+
caCertFile: caCertFile,
33+
Runner: runner,
34+
}
3035
}
3136

3237
func (c *VcenterClient) ValidateUrl() error {
3338
args := []string{"about", "-u", c.Url}
34-
errMsg := fmt.Sprintf("vcenter_client - unable to validate url: %s", c.Url)
39+
errMsg := "unable to validate url"
3540
if c.caCertFile != "" {
3641
args = append(args, fmt.Sprintf("-tls-ca-certs=%s", c.caCertFile))
37-
errMsg = fmt.Sprintf("vcenter_client - invalid ca certs or url: %s", c.Url)
42+
errMsg = "invalid ca certs or url"
3843
}
44+
3945
errCode := c.Runner.Run(args)
4046
if errCode != 0 {
41-
return errors.New(errMsg)
47+
return fmt.Errorf("vcenter_client - %s: %s", errMsg, c.Url)
4248
}
43-
return nil
4449

50+
return nil
4551
}
4652

4753
func (c *VcenterClient) ValidateCredentials() error {
@@ -84,6 +90,7 @@ func (c *VcenterClient) ListDevices(vmInventoryPath string) ([]string, error) {
8490
devices = append(devices, r.FindString(entry))
8591
}
8692
}
93+
8794
return devices, nil
8895
}
8996
func (c *VcenterClient) RemoveDevice(vmInventoryPath string, deviceName string) error {
@@ -92,16 +99,17 @@ func (c *VcenterClient) RemoveDevice(vmInventoryPath string, deviceName string)
9299
if errCode != 0 {
93100
return fmt.Errorf("vcenter_client - %s could not be removed", deviceName)
94101
}
102+
95103
return nil
96104
}
97105

98106
func (c *VcenterClient) EjectCDRom(vmInventoryPath string, deviceName string) error {
99-
100107
args := c.buildGovcCommand("device.cdrom.eject", "-vm", vmInventoryPath, "-device", deviceName)
101108
errCode := c.Runner.Run(args)
102109
if errCode != 0 {
103110
return fmt.Errorf("vcenter_client - %s could not be ejected", deviceName)
104111
}
112+
105113
return nil
106114
}
107115

@@ -115,6 +123,7 @@ func (c *VcenterClient) ExportVM(vmInventoryPath string, destination string) err
115123
if errCode != 0 {
116124
return fmt.Errorf("vcenter_client - %s could not be exported", vmInventoryPath)
117125
}
126+
118127
return nil
119128
}
120129

@@ -125,6 +134,7 @@ func (c *VcenterClient) UploadArtifact(vmInventoryPath, artifact, destination, u
125134
if errCode != 0 {
126135
return fmt.Errorf("vcenter_client - %s could not be uploaded", artifact)
127136
}
137+
128138
return nil
129139
}
130140

@@ -136,6 +146,7 @@ func (c *VcenterClient) MakeDirectory(vmInventoryPath, path, username, password
136146
if errCode != 0 {
137147
return fmt.Errorf("vcenter_client - directory `%s` could not be created", path)
138148
}
149+
139150
return nil
140151
}
141152

@@ -150,8 +161,8 @@ func (c *VcenterClient) Start(vmInventoryPath, username, password, command strin
150161
if exitCode != 0 {
151162
return "", fmt.Errorf("vcenter_client - '%s' returned exit code: %d", command, exitCode)
152163
}
153-
// We trim this suffix since govc outputs the pid with an '\n' in the output
154-
return strings.TrimSuffix(pid, "\n"), nil
164+
165+
return strings.TrimSuffix(pid, "\n"), nil // trim since govc outputs the pid with an '\n' in the output
155166
}
156167

157168
type govcPS struct {
@@ -195,6 +206,7 @@ func (c *VcenterClient) buildGovcCommand(args ...string) []string {
195206
commonArgs = append(commonArgs, fmt.Sprintf("-tls-ca-certs=%s", c.caCertFile))
196207
}
197208
args = append(args[:1], append(commonArgs, args[1:]...)...)
209+
198210
return args
199211
}
200212

@@ -204,6 +216,7 @@ func (c *VcenterClient) IsPoweredOff(vmInventoryPath string) (bool, error) {
204216
if exitCode != 0 {
205217
return false, fmt.Errorf("vcenter_client - failed to get vm info, govc exit code: %d", exitCode)
206218
}
219+
207220
if err != nil {
208221
return false, fmt.Errorf("vcenter_client - failed to determine vm power state: %s", err)
209222
}

package_stemcell/packager/vmdk_packager.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,14 @@ func (c *VmdkPackager) ConvertVMX2OVA(vmx, ova string) error {
207207

208208
cmd := exec.Command(ovfpath, vmx, ova)
209209
cmd.Stderr = &stderr
210-
if err := cmd.Start(); err != nil {
210+
211+
err = cmd.Start()
212+
if err != nil {
211213
return fmt.Errorf("ovftool: %s", err)
212214
}
213215
c.Logger.Printf("converting vmx to ova with cmd: %s %s", cmd.Path, cmd.Args[1:])
214216

215-
// Wait for process exit or interupt
217+
// Wait for process exit or interrupt
216218
errCh := make(chan error, 1)
217219
go func() { errCh <- cmd.Wait() }()
218220

@@ -223,9 +225,9 @@ func (c *VmdkPackager) ConvertVMX2OVA(vmx, ova string) error {
223225
cmd.Process.Kill() //nolint:errcheck
224226
}
225227
return ErrInterrupt
226-
case err := <-errCh:
227-
if err != nil {
228-
return fmt.Errorf(errFmt, err, stderr.String()) //nolint:staticcheck
228+
case chanErr := <-errCh:
229+
if chanErr != nil {
230+
return fmt.Errorf(errFmt, chanErr, stderr.String()) //nolint:staticcheck
229231
}
230232
}
231233

@@ -392,8 +394,7 @@ func IsValidVMDK(vmdk string) (bool, error) {
392394
func (c *VmdkPackager) ValidateFreeSpaceForPackage(fs filesystem.FileSystem) error {
393395
fi, err := os.Stat(c.BuildOptions.VMDKFile)
394396
if err != nil {
395-
errorMsg := fmt.Sprintf("could not get vmdk info: %s", err)
396-
return errors.New(errorMsg)
397+
return fmt.Errorf("could not get vmdk info: %s", err)
397398
}
398399
vmdkSize := fi.Size()
399400

@@ -404,21 +405,16 @@ func (c *VmdkPackager) ValidateFreeSpaceForPackage(fs filesystem.FileSystem) err
404405

405406
enoughSpace, requiredSpace, err := hasAtLeastFreeDiskSpace(minSpace, fs, filepath.Dir(c.BuildOptions.VMDKFile))
406407
if err != nil {
407-
errorMsg := fmt.Sprintf("could not check free space on disk: %s", err)
408-
return errors.New(errorMsg)
408+
return fmt.Errorf("could not check free space on disk: %s", err)
409409
}
410410

411411
if !enoughSpace {
412-
errorMsg := fmt.Sprintf("Not enough space to create stemcell. Free up %d MB and try again", requiredSpace/(1024*1024))
413-
return errors.New(errorMsg)
414-
412+
return fmt.Errorf("Not enough space to create stemcell. Free up %d MB and try again", requiredSpace/(1024*1024)) //nolint:staticcheck
415413
}
416414
return nil
417-
418415
}
419416

420417
func hasAtLeastFreeDiskSpace(minFreeSpace uint64, fs filesystem.FileSystem, path string) (bool, uint64, error) {
421-
422418
freeSpace, err := fs.GetAvailableDiskSpace(path)
423419

424420
if err != nil {

0 commit comments

Comments
 (0)