Skip to content

Commit 1a88c95

Browse files
authored
fix: use errors and string concatenation to avoid fmt (#371)
Signed-off-by: Chris Gianelloni <[email protected]>
1 parent 282b452 commit 1a88c95

File tree

6 files changed

+31
-61
lines changed

6 files changed

+31
-61
lines changed

cmd/cardano-up/validate.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@ func validateCommand() *cobra.Command {
6464
os.Exit(1)
6565
}
6666
slog.Info(
67-
fmt.Sprintf(
68-
"Validating packages in path %s",
69-
absPackagesDir,
70-
),
67+
"Validating packages in path " + absPackagesDir,
7168
)
7269
if err := pm.ValidatePackages(); err != nil {
7370
slog.Error("problems were found")

pkgmgr/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ func NewDefaultConfig() (Config, error) {
4343
err,
4444
)
4545
}
46-
userBinDir := fmt.Sprintf("%s/.local/bin", userHomeDir)
47-
userDataDir := fmt.Sprintf("%s/.local/share", userHomeDir)
46+
userBinDir := userHomeDir + "/.local/bin"
47+
userDataDir := userHomeDir + "/.local/share"
4848
userCacheDir, err := os.UserCacheDir()
4949
if err != nil {
5050
return Config{}, fmt.Errorf(

pkgmgr/docker.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func (d *DockerService) Start() error {
115115
if err != nil {
116116
return err
117117
}
118-
d.logger.Debug(fmt.Sprintf("starting container %s", d.ContainerName))
118+
d.logger.Debug("starting container " + d.ContainerName)
119119
if err := client.ContainerStart(
120120
context.Background(),
121121
d.ContainerId,
@@ -137,7 +137,7 @@ func (d *DockerService) Stop() error {
137137
if err != nil {
138138
return err
139139
}
140-
d.logger.Debug(fmt.Sprintf("stopping container %s", d.ContainerName))
140+
d.logger.Debug("stopping container " + d.ContainerName)
141141
stopTimeout := 60
142142
if err := client.ContainerStop(
143143
context.Background(),
@@ -186,7 +186,7 @@ func (d *DockerService) Create() error {
186186
groupID := os.Getgid()
187187
userAndGroup := fmt.Sprintf("%d:%d", userID, groupID)
188188
// Create container
189-
d.logger.Debug(fmt.Sprintf("creating container %s", d.ContainerName))
189+
d.logger.Debug("creating container " + d.ContainerName)
190190
resp, err := client.ContainerCreate(
191191
context.Background(),
192192
&container.Config{
@@ -225,13 +225,13 @@ func (d *DockerService) Remove() error {
225225
return err
226226
}
227227
if running {
228-
return fmt.Errorf("can't remove a running container")
228+
return errors.New("can't remove a running container")
229229
}
230230
client, err := d.getClient()
231231
if err != nil {
232232
return err
233233
}
234-
d.logger.Debug(fmt.Sprintf("removing container %s", d.ContainerName))
234+
d.logger.Debug("removing container " + d.ContainerName)
235235
if err := client.ContainerRemove(
236236
context.Background(),
237237
d.ContainerId,

pkgmgr/package.go

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,7 @@ func (p Package) install(
189189
)
190190
} else if !ok {
191191
cfg.Logger.Debug(
192-
fmt.Sprintf(
193-
"skipping install step due to condition: %s",
194-
installStep.Condition,
195-
),
192+
"skipping install step due to condition: " + installStep.Condition,
196193
)
197194
continue
198195
}
@@ -302,10 +299,7 @@ func (p Package) uninstall(
302299
return NewInstallStepConditionError(installStep.Condition, err)
303300
} else if !ok {
304301
cfg.Logger.Debug(
305-
fmt.Sprintf(
306-
"skipping uninstall step due to condition: %s",
307-
installStep.Condition,
308-
),
302+
"skipping uninstall step due to condition: " + installStep.Condition,
309303
)
310304
continue
311305
}
@@ -393,10 +387,7 @@ func (p Package) activate(cfg Config, context string) error {
393387
return NewInstallStepConditionError(installStep.Condition, err)
394388
} else if !ok {
395389
cfg.Logger.Debug(
396-
fmt.Sprintf(
397-
"skipping install step due to condition: %s",
398-
installStep.Condition,
399-
),
390+
"skipping install step due to condition: " + installStep.Condition,
400391
)
401392
continue
402393
}
@@ -425,10 +416,7 @@ func (p Package) deactivate(cfg Config, context string) error {
425416
return NewInstallStepConditionError(installStep.Condition, err)
426417
} else if !ok {
427418
cfg.Logger.Debug(
428-
fmt.Sprintf(
429-
"skipping install step due to condition: %s",
430-
installStep.Condition,
431-
),
419+
"skipping install step due to condition: " + installStep.Condition,
432420
)
433421
continue
434422
}
@@ -451,7 +439,7 @@ func (p Package) deactivate(cfg Config, context string) error {
451439
func (p Package) validate(cfg Config) error {
452440
// Check empty name
453441
if p.Name == "" {
454-
return fmt.Errorf("package name cannot be empty")
442+
return errors.New("package name cannot be empty")
455443
}
456444
// Check name matches allowed characters
457445
reName := regexp.MustCompile(`^[-a-zA-Z0-9]+$`)
@@ -460,7 +448,7 @@ func (p Package) validate(cfg Config) error {
460448
}
461449
// Check empty version
462450
if p.Version == "" {
463-
return fmt.Errorf("package version cannot be empty")
451+
return errors.New("package version cannot be empty")
464452
}
465453
// Check version is well formed
466454
if _, err := version.NewVersion(p.Version); err != nil {
@@ -535,7 +523,7 @@ func (p Package) startService(cfg Config, context string) error {
535523
}
536524
// Start the Docker container if it's not running
537525
slog.Info(
538-
fmt.Sprintf("Starting Docker container %s", containerName),
526+
"Starting Docker container " + containerName,
539527
)
540528
if err := dockerService.Start(); err != nil {
541529
startErrors = append(
@@ -588,7 +576,7 @@ func (p Package) stopService(cfg Config, context string) error {
588576
continue
589577
}
590578
// Stop the Docker container
591-
slog.Info(fmt.Sprintf("Stopping container %s", containerName))
579+
slog.Info("Stopping container " + containerName)
592580
if err := dockerService.Stop(); err != nil {
593581
stopErrors = append(
594582
stopErrors,
@@ -685,7 +673,7 @@ type PackageInstallStepDocker struct {
685673

686674
func (p *PackageInstallStepDocker) validate(cfg Config) error {
687675
if p.Image == "" {
688-
return fmt.Errorf("docker image must be provided")
676+
return errors.New("docker image must be provided")
689677
}
690678
// TODO: add more checks
691679
return nil
@@ -808,10 +796,7 @@ func (p *PackageInstallStepDocker) uninstall(
808796
if err != nil {
809797
if err == ErrContainerNotExists {
810798
cfg.Logger.Debug(
811-
fmt.Sprintf(
812-
"container missing on uninstall: %s",
813-
containerName,
814-
),
799+
"container missing on uninstall: " + containerName,
815800
)
816801
} else {
817802
return err
@@ -932,7 +917,7 @@ func (p *PackageInstallStepFile) install(
932917
return err
933918
}
934919
if u.Scheme == "" || u.Host == "" {
935-
return fmt.Errorf("invalid URL given...")
920+
return errors.New("invalid URL given...")
936921
}
937922

938923
// Fetch data
@@ -951,12 +936,12 @@ func (p *PackageInstallStepFile) install(
951936

952937
fileContent = respBody
953938
} else {
954-
return fmt.Errorf("packages must provide content, source, or url for file install types")
939+
return errors.New("packages must provide content, source, or url for file install types")
955940
}
956941
if err := os.WriteFile(filePath, fileContent, fileMode); err != nil {
957942
return err
958943
}
959-
cfg.Logger.Debug(fmt.Sprintf("wrote file %s", filePath))
944+
cfg.Logger.Debug("wrote file " + filePath)
960945
return nil
961946
}
962947

@@ -966,10 +951,10 @@ func (p *PackageInstallStepFile) uninstall(cfg Config, pkgName string) error {
966951
pkgName,
967952
p.Filename,
968953
)
969-
cfg.Logger.Debug(fmt.Sprintf("deleting file %s", filePath))
954+
cfg.Logger.Debug("deleting file " + filePath)
970955
if err := os.Remove(filePath); err != nil {
971956
if !errors.Is(err, fs.ErrNotExist) {
972-
cfg.Logger.Warn(fmt.Sprintf("failed to remove file %s", filePath))
957+
cfg.Logger.Warn("failed to remove file " + filePath)
973958
}
974959
}
975960
return nil
@@ -1043,7 +1028,7 @@ func (p *PackageInstallStepFile) deactivate(cfg Config, pkgName string) error {
10431028
return err
10441029
}
10451030
}
1046-
cfg.Logger.Debug(fmt.Sprintf("removed symlink %s", binPath))
1031+
cfg.Logger.Debug("removed symlink " + binPath)
10471032
}
10481033
return nil
10491034
}

pkgmgr/pkgmgr.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -466,10 +466,7 @@ func (p *PackageManager) Info(pkgs ...string) error {
466466
activeContextName,
467467
)
468468
if infoPkg.PostInstallNotes != "" {
469-
infoOutput += fmt.Sprintf(
470-
"\n\nPost-install notes:\n\n%s",
471-
infoPkg.PostInstallNotes,
472-
)
469+
infoOutput += "\n\nPost-install notes:\n\n" + infoPkg.PostInstallNotes
473470
}
474471
// Gather package services
475472
services, err := infoPkg.Package.services(p.config, infoPkg.Context)
@@ -517,16 +514,10 @@ func (p *PackageManager) Info(pkgs ...string) error {
517514
}
518515
}
519516
if statusOutput != "" {
520-
infoOutput += fmt.Sprintf(
521-
"\n\nServices:\n\n%s",
522-
strings.TrimSuffix(statusOutput, "\n"),
523-
)
517+
infoOutput += "\n\nServices:\n\n" + strings.TrimSuffix(statusOutput, "\n")
524518
}
525519
if portOutput != "" {
526-
infoOutput += fmt.Sprintf(
527-
"\n\nMapped ports:\n\n%s",
528-
strings.TrimSuffix(portOutput, "\n"),
529-
)
520+
infoOutput += "\n\nMapped ports:\n\n" + strings.TrimSuffix(portOutput, "\n")
530521
}
531522
if idx < len(infoPkgs)-1 {
532523
infoOutput += "\n\n---\n\n"
@@ -705,10 +696,7 @@ func (p *PackageManager) ValidatePackages() error {
705696
continue
706697
}
707698
p.config.Logger.Debug(
708-
fmt.Sprintf(
709-
"checking package %s",
710-
pkg.filePath,
711-
),
699+
"checking package " + pkg.filePath,
712700
)
713701
if err := pkg.validate(p.config); err != nil {
714702
foundError = true

pkgmgr/registry.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func registryPackagesUrl(cfg Config, validate bool) ([]Package, error) {
134134
stat.ModTime().Before(time.Now().Add(-24*time.Hour)) {
135135
// Fetch registry ZIP
136136
cfg.Logger.Info(
137-
fmt.Sprintf("Fetching package registry %s", cfg.RegistryUrl),
137+
"Fetching package registry " + cfg.RegistryUrl,
138138
)
139139
resp, err := http.Get(cfg.RegistryUrl)
140140
if err != nil {
@@ -172,7 +172,7 @@ func registryPackagesUrl(cfg Config, validate bool) ([]Package, error) {
172172
}
173173
// Ensure there are no parent dir references in path
174174
if strings.Contains(zipFile.Name, "..") {
175-
return nil, fmt.Errorf("parent path reference in zip name")
175+
return nil, errors.New("parent path reference in zip name")
176176
}
177177
// #nosec G305
178178
outPath := filepath.Join(
@@ -181,7 +181,7 @@ func registryPackagesUrl(cfg Config, validate bool) ([]Package, error) {
181181
)
182182
// Ensure our path is sane to prevent the gosec issue above
183183
if !strings.HasPrefix(outPath, filepath.Clean(cachePath)) {
184-
return nil, fmt.Errorf("zip extraction path mismatch")
184+
return nil, errors.New("zip extraction path mismatch")
185185
}
186186
// Create parent dir(s)
187187
if err := os.MkdirAll(filepath.Dir(outPath), fs.ModePerm); err != nil {

0 commit comments

Comments
 (0)