Skip to content

Commit

Permalink
Add a linter to disable fmt.Printf in code (#4154)
Browse files Browse the repository at this point in the history
Printf is often used while developping to inspect or debug code. It
happens that these statements are forgotten and commited. We can prevent
that by using a linter to disable them.

We have some cases where fmt.Printf was used to print useful stuff, like
in the CLI. They have been rewritten in fmt.Fprintf to avoid been
flagged by the linter.
  • Loading branch information
nono authored Oct 10, 2023
2 parents 1eb60a7 + 9a957b3 commit 10923c7
Show file tree
Hide file tree
Showing 16 changed files with 78 additions and 67 deletions.
6 changes: 6 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ linters:
- bidichk
- exportloopref
- errname
- forbidigo
- gocritic
- gofmt
- govet
Expand All @@ -60,6 +61,11 @@ linters:
# Default: false
fast: false
linters-settings:
forbidigo:
# Forbid fmt.Printf, as they are often used for debugging and they should
# not be commited. The good cases can be written fmt.Fprintf(os.Stdout, ).
forbid:
- fmt.Printf
gocritic:
disabled-checks:
- appendAssign
Expand Down
2 changes: 1 addition & 1 deletion client/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ func (ac *AdminClient) Export(opts *ExportOptions) error {
filename = params["filename"]
}

fmt.Printf("Exporting archive %d/%d (%s)... ", i+1, partsCount, filename)
fmt.Fprintf(os.Stdout, "Exporting archive %d/%d (%s)... ", i+1, partsCount, filename)

filepath := path.Join(opts.LocalPath, filename)
f, err := os.OpenFile(filepath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0600)
Expand Down
14 changes: 7 additions & 7 deletions cmd/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ var activateMaintenanceKonnectorsCmd = &cobra.Command{
break
}
if len(locale) > 5 {
fmt.Printf("Invalid locale name: %q\n", locale)
fmt.Fprintf(os.Stdout, "Invalid locale name: %q\n", locale)
continue
}
shortMessage := prompt("Short message:")
Expand Down Expand Up @@ -366,7 +366,7 @@ func installApp(cmd *cobra.Command, args []string, appType string) error {
}
return err
}
fmt.Printf("Application installed successfully on %s\n", in.Attrs.Domain)
fmt.Fprintf(os.Stdout, "Application installed successfully on %s\n", in.Attrs.Domain)
return nil
})
}
Expand All @@ -393,7 +393,7 @@ func installApp(cmd *cobra.Command, args []string, appType string) error {
if err != nil {
return err
}
fmt.Printf("%s (%s) has been installed on %s\n", slug, manifest.Attrs.Version, flagDomain)
fmt.Fprintf(os.Stdout, "%s (%s) has been installed on %s\n", slug, manifest.Attrs.Version, flagDomain)

return nil
}
Expand All @@ -420,7 +420,7 @@ func updateApp(cmd *cobra.Command, args []string, appType string) error {
}
return err
}
fmt.Printf("Application updated successfully on %s\n", in.Attrs.Domain)
fmt.Fprintf(os.Stdout, "Application updated successfully on %s\n", in.Attrs.Domain)
return nil
})
}
Expand Down Expand Up @@ -457,7 +457,7 @@ func updateApp(cmd *cobra.Command, args []string, appType string) error {
} else if app.IsMoreRecent(newManifest.Attrs.Version, manifest.Attrs.Version) {
msg = "%s has been downgraded to %s\n"
}
fmt.Printf(msg, args[0], newManifest.Attrs.Version)
fmt.Fprintf(os.Stdout, msg, args[0], newManifest.Attrs.Version)

return nil
}
Expand All @@ -478,7 +478,7 @@ func uninstallApp(cmd *cobra.Command, args []string, appType string) error {
if err != nil {
return err
}
fmt.Printf("%s has been uninstalled\n", manifest.Attrs.Slug)
fmt.Fprintf(os.Stdout, "%s has been uninstalled\n", manifest.Attrs.Slug)
return nil
}

Expand Down Expand Up @@ -525,7 +525,7 @@ func showWebAppTriggers(cmd *cobra.Command, args []string, appType string) error

var triggerIDs []string
if manifest.Attrs.Services == nil {
fmt.Printf("No triggers\n")
fmt.Fprintf(os.Stdout, "No triggers\n")
return nil
}
for _, service := range *manifest.Attrs.Services {
Expand Down
6 changes: 3 additions & 3 deletions cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ triggers of the same type, for the same worker, and with the same arguments.
if len(result) > 0 {
for _, r := range result {
j, _ := json.Marshal(r)
fmt.Printf("%s\n", j)
fmt.Fprintf(os.Stdout, "%s\n", j)
}
os.Exit(1)
}
Expand Down Expand Up @@ -167,7 +167,7 @@ generation smaller than their generation.
if len(result) > 0 {
for _, r := range result {
j, _ := json.Marshal(r)
fmt.Printf("%s\n", j)
fmt.Fprintf(os.Stdout, "%s\n", j)
}
os.Exit(1)
}
Expand Down Expand Up @@ -220,7 +220,7 @@ check via the flags.
if len(result) > 0 {
for _, r := range result {
j, _ := json.Marshal(r)
fmt.Printf("%s\n", j)
fmt.Fprintf(os.Stdout, "%s\n", j)
}
os.Exit(1)
}
Expand Down
9 changes: 5 additions & 4 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ $ bmFjbNFjY+XZkS26YtVPUIKKm/JdnAGwG30n6A4ypS1p1dHev8hOtaRbW+lGneoO7PS9JCW8U5GSXh
return err
}
data := base64.StdEncoding.EncodeToString(dataEncrypted)
fmt.Printf("%s\n", data)
fmt.Fprintf(os.Stdout, "%s\n", data)

return nil
},
Expand Down Expand Up @@ -198,7 +198,7 @@ var decryptCredentialsDataCmd = &cobra.Command{
return err
}

fmt.Printf("%s\n", decrypted)
fmt.Fprintf(os.Stdout, "%s\n", decrypted)

return nil
},
Expand All @@ -222,7 +222,7 @@ var encryptCredentialsCmd = &cobra.Command{
if err != nil {
return err
}
fmt.Printf("Encrypted credentials: %s\n", encryptedCreds)
fmt.Fprintf(os.Stdout, "Encrypted credentials: %s\n", encryptedCreds)
return nil
},
}
Expand Down Expand Up @@ -251,10 +251,11 @@ var decryptCredentialsCmd = &cobra.Command{
return fmt.Errorf("Could not decrypt cipher text: %s", err)
}

fmt.Printf(`Decrypted credentials:
fmt.Fprintf(os.Stdout, `Decrypted credentials:
login: %q
password: %q
`, login, password)

return nil
},
}
Expand Down
19 changes: 10 additions & 9 deletions cmd/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"net/url"
"os"
"strings"

"github.com/cozy/cozy-stack/client/request"
Expand Down Expand Up @@ -58,14 +59,14 @@ cozy-stack feature show displays the feature flags that are shown by apps.
return err
}
for k, v := range obj.Data.Attributes {
fmt.Printf("- %s: %s\n", k, string(v))
fmt.Fprintf(os.Stdout, "- %s: %s\n", k, string(v))
}
if len(obj.Included) > 0 {
fmt.Printf("\nSources:\n")
fmt.Fprintf(os.Stdout, "\nSources:\n")
for _, source := range obj.Included {
fmt.Printf("- %s\n", source.ID)
fmt.Fprintf(os.Stdout, "- %s\n", source.ID)
for k, v := range source.Attributes {
fmt.Printf("\t- %s: %s\n", k, string(v))
fmt.Fprintf(os.Stdout, "\t- %s: %s\n", k, string(v))
}
}
}
Expand Down Expand Up @@ -109,7 +110,7 @@ If you give a null value, the flag will be removed.
return err
}
for k, v := range obj {
fmt.Printf("- %s: %s\n", k, string(v))
fmt.Fprintf(os.Stdout, "- %s: %s\n", k, string(v))
}
return nil
},
Expand Down Expand Up @@ -159,7 +160,7 @@ All the sets can be removed by setting an empty list ('').
return err
}
for _, set := range sets {
fmt.Printf("- %v\n", set)
fmt.Fprintf(os.Stdout, "- %v\n", set)
}
return nil
},
Expand Down Expand Up @@ -200,7 +201,7 @@ To remove a flag, set it to an empty array (or null).
return err
}
for k, v := range obj {
fmt.Printf("- %s: %s\n", k, string(v))
fmt.Fprintf(os.Stdout, "- %s: %s\n", k, string(v))
}
return nil
},
Expand Down Expand Up @@ -234,7 +235,7 @@ These flags are read only and can only be updated by changing configuration and
return err
}
for k, v := range obj {
fmt.Printf("- %s: %s\n", k, string(v))
fmt.Fprintf(os.Stdout, "- %s: %s\n", k, string(v))
}
return nil
},
Expand Down Expand Up @@ -271,7 +272,7 @@ If you give a null value, the flag will be removed.
return err
}
for k, v := range obj {
fmt.Printf("- %s: %s\n", k, string(v))
fmt.Fprintf(os.Stdout, "- %s: %s\n", k, string(v))
}
return nil
},
Expand Down
24 changes: 12 additions & 12 deletions cmd/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,29 +121,29 @@ var usageFilesCmd = &cobra.Command{
if err != nil {
return err
}
fmt.Printf("Usage: %v\n", info["used"])
fmt.Fprintf(os.Stdout, "Usage: %v\n", info["used"])

if files, ok := info["files"]; ok {
fmt.Printf(" Including latest version of files: %v\n", files)
fmt.Fprintf(os.Stdout, " Including latest version of files: %v\n", files)
}
if versions, ok := info["versions"]; ok {
fmt.Printf(" Including older versions of files: %v\n", versions)
fmt.Fprintf(os.Stdout, " Including older versions of files: %v\n", versions)
}

if flagIncludeTrash {
if trashed, ok := info["trashed"]; ok {
fmt.Printf(" Including trashed files: %v\n", trashed)
fmt.Fprintf(os.Stdout, " Including trashed files: %v\n", trashed)
}
}

if quota, ok := info["quota"]; ok {
fmt.Printf("Quota: %v\n", quota)
fmt.Fprintf(os.Stdout, "Quota: %v\n", quota)
}
if count, ok := info["doc_count"]; ok {
fmt.Printf("Documents count: %v\n", count)
fmt.Fprintf(os.Stdout, "Documents count: %v\n", count)
}
if count, ok := info["versions_count"]; ok {
fmt.Printf("Versions Documents count: %v\n", count)
fmt.Fprintf(os.Stdout, "Versions Documents count: %v\n", count)
}
return nil
},
Expand Down Expand Up @@ -338,7 +338,7 @@ func treeCmd(c *client.Client, root string, w io.Writer, verbose bool) error {
return err
}
if verbose {
fmt.Printf("%s ", doc.ID)
fmt.Fprintf(os.Stdout, "%s ", doc.ID)
}

attrs := doc.Attrs
Expand Down Expand Up @@ -461,11 +461,11 @@ func importFiles(c *client.Client, from, to string, match *regexp.Regexp) error
return err
}
if !fromInfos.IsDir() {
fmt.Printf("Importing file %s to cozy://%s\n", from, to)
fmt.Fprintf(os.Stdout, "Importing file %s to cozy://%s\n", from, to)
return i.upload(from, to)
}

fmt.Printf("Importing from %s to cozy://%s\n", from, to)
fmt.Fprintf(os.Stdout, "Importing from %s to cozy://%s\n", from, to)

return filepath.Walk(from, func(localname string, f os.FileInfo, err error) error {
if err != nil {
Expand All @@ -485,14 +485,14 @@ func importFiles(c *client.Client, from, to string, match *regexp.Regexp) error

distname := path.Join(to, strings.TrimPrefix(localname, from))
if f.IsDir() {
fmt.Printf("create dir %s\n", distname)
fmt.Fprintf(os.Stdout, "create dir %s\n", distname)
if !flagImportDryRun {
if _, err = i.mkdir(distname); err != nil {
return err
}
}
} else {
fmt.Printf("copying file %s to %s\n", localname, distname)
fmt.Fprintf(os.Stdout, "copying file %s to %s\n", localname, distname)
if !flagImportDryRun {
return i.upload(localname, distname)
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var mimeFixerCmd = &cobra.Command{
if class == attrs.Class {
return nil
}
fmt.Printf("Fix %s: %s -> %s\n", attrs.Name, attrs.Class, class)
fmt.Fprintf(os.Stdout, "Fix %s: %s -> %s\n", attrs.Name, attrs.Class, class)
_, err = c.UpdateAttrsByID(doc.ID, &client.FilePatch{
Rev: doc.Rev,
Attrs: client.FilePatchAttrs{
Expand Down Expand Up @@ -91,7 +91,7 @@ var jobsFixer = &cobra.Command{
return err
}

fmt.Printf("Cleaned %d jobs on %s\n", result.Deleted, args[0])
fmt.Fprintf(os.Stdout, "Cleaned %d jobs on %s\n", result.Deleted, args[0])
return nil
},
}
Expand Down Expand Up @@ -227,11 +227,11 @@ var contactEmailsFixer = &cobra.Command{
address = strings.TrimSpace(address)
_, err := mail.ParseAddress(address)
if err == nil {
fmt.Printf(" Email fixed: \"%s\"\"%s\"\n", old, address)
fmt.Fprintf(os.Stdout, " Email fixed: \"%s\"\"%s\"\n", old, address)
changed = true
email["address"] = address
} else {
fmt.Printf(" Invalid email: \"%s\"\"%s\"\n", old, address)
fmt.Fprintf(os.Stdout, " Invalid email: \"%s\"\"%s\"\n", old, address)
}
}
}
Expand Down
Loading

0 comments on commit 10923c7

Please sign in to comment.