Skip to content

Commit 7415e26

Browse files
cmaglieper1234
andauthored
[skip-changelog] Removed unnecessary fmt.*printf before tr(...) / Fixed i18n early static initialization problem (#1425)
* Removed unnecessary fmt.*printf * removed unused variable * Fixed i18n early static initialization problem #1425 (comment) these particular strings are not correctly handled by the i18n package because they are declared at package level before the call to i18n.Init(), and so are just returned as-is. * Fix lint errors and use error.Is instead of direct comparison * Revert "guard" in Tr function otherwise the unit-tests will fail: === RUN TestBoardOptions --- FAIL: TestBoardOptions (0.00s) panic: i18n.Tr called before i18n.Init() [recovered] panic: i18n.Tr called before i18n.Init() goroutine 9 [running]: testing.tRunner.func1.2(0xc56e00, 0xeaee20) /opt/hostedtoolcache/go/1.16.7/x64/src/testing/testing.go:1143 +0x332 testing.tRunner.func1(0xc0002a2f00) /opt/hostedtoolcache/go/1.16.7/x64/src/testing/testing.go:1146 +0x4b6 panic(0xc56e00, 0xeaee20) /opt/hostedtoolcache/go/1.16.7/x64/src/runtime/panic.go:965 +0x1b9 github.com/arduino/arduino-cli/i18n.Tr(0xdceacd, 0x28, 0x0, 0x0, 0x0, 0x0, 0x1) /home/runner/work/arduino-cli/arduino-cli/i18n/i18n.go:54 +0xd9 github.com/arduino/arduino-cli/arduino/cores.(*Board).GetBuildProperties(0x13bf060, 0xc0003b6150, 0xc000339400, 0x0, 0x0) /home/runner/work/arduino-cli/arduino-cli/arduino/cores/board.go:109 +0x69d github.com/arduino/arduino-cli/arduino/cores.(*Board).GeneratePropertiesForConfiguration(0x13bf060, 0xdb5f54, 0xe, 0xc000309ef0, 0xc911e0, 0xc0003b6000) /home/runner/work/arduino-cli/arduino-cli/arduino/cores/board.go:141 +0x28f github.com/arduino/arduino-cli/arduino/cores.TestBoardOptions(0xc0002a2f00) /home/runner/work/arduino-cli/arduino-cli/arduino/cores/board_test.go:298 +0x4bd testing.tRunner(0xc0002a2f00, 0xe052a8) /opt/hostedtoolcache/go/1.16.7/x64/src/testing/testing.go:1193 +0xef created by testing.(*T).Run /opt/hostedtoolcache/go/1.16.7/x64/src/testing/testing.go:1238 +0x2b3 FAIL github.com/arduino/arduino-cli/arduino/cores 0.021s === RUN TestIndexParsing --- FAIL: TestIndexParsing (0.00s) panic: i18n.Tr called before i18n.Init() [recovered] panic: i18n.Tr called before i18n.Init() * Apply suggestions from code review Co-authored-by: per1234 <[email protected]> Co-authored-by: per1234 <[email protected]>
1 parent 15e2452 commit 7415e26

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+496
-595
lines changed

arduino/builder/compilation_database.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ func LoadCompilationDatabase(file *paths.Path) (*CompilationDatabase, error) {
6060
// see https://clang.llvm.org/docs/JSONCompilationDatabase.html
6161
func (db *CompilationDatabase) SaveToFile() {
6262
if jsonContents, err := json.MarshalIndent(db.Contents, "", " "); err != nil {
63-
fmt.Printf(tr("Error serializing compilation database: %s"), err)
63+
fmt.Println(tr("Error serializing compilation database: %s", err))
6464
return
6565
} else if err := db.File.WriteFile(jsonContents); err != nil {
66-
fmt.Printf(tr("Error writing compilation database: %s"), err)
66+
fmt.Println(tr("Error writing compilation database: %s", err))
6767
}
6868
}
6969

@@ -75,7 +75,7 @@ func dirForCommand(command *exec.Cmd) string {
7575
}
7676
dir, err := os.Getwd()
7777
if err != nil {
78-
fmt.Printf(tr("Error getting current directory for compilation database: %s"), err)
78+
fmt.Println(tr("Error getting current directory for compilation database: %s", err))
7979
return ""
8080
}
8181
return dir

arduino/discovery/discovery.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,16 @@ type discoveryMessage struct {
7171
func (msg discoveryMessage) String() string {
7272
s := fmt.Sprintf("type: %s", msg.EventType)
7373
if msg.Message != "" {
74-
s = fmt.Sprintf(tr("%[1]s, message: %[2]s"), s, msg.Message)
74+
s = tr("%[1]s, message: %[2]s", s, msg.Message)
7575
}
7676
if msg.ProtocolVersion != 0 {
77-
s = fmt.Sprintf(tr("%[1]s, protocol version: %[2]d"), s, msg.ProtocolVersion)
77+
s = tr("%[1]s, protocol version: %[2]d", s, msg.ProtocolVersion)
7878
}
7979
if len(msg.Ports) > 0 {
80-
s = fmt.Sprintf(tr("%[1]s, ports: %[2]s"), s, msg.Ports)
80+
s = tr("%[1]s, ports: %[2]s", s, msg.Ports)
8181
}
8282
if msg.Port != nil {
83-
s = fmt.Sprintf(tr("%[1]s, port: %[2]s"), s, msg.Port)
83+
s = tr("%[1]s, port: %[2]s", s, msg.Port)
8484
}
8585
return s
8686
}

arduino/libraries/librariesmanager/install.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package librariesmanager
1717

1818
import (
1919
"context"
20-
"errors"
2120
"fmt"
2221
"net/url"
2322
"os"
@@ -32,10 +31,16 @@ import (
3231
"gopkg.in/src-d/go-git.v4"
3332
)
3433

34+
type alreadyInstalledError struct{}
35+
36+
func (e *alreadyInstalledError) Error() string {
37+
return tr("library already installed")
38+
}
39+
3540
var (
3641
// ErrAlreadyInstalled is returned when a library is already installed and task
3742
// cannot proceed.
38-
ErrAlreadyInstalled = errors.New(tr("library already installed"))
43+
ErrAlreadyInstalled = &alreadyInstalledError{}
3944
)
4045

4146
// InstallPrerequisiteCheck performs prequisite checks to install a library. It returns the

arduino/libraries/librariesmanager/librariesmanager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ var tr = i18n.Tr
6262
// Add adds a library to the alternatives
6363
func (alts *LibraryAlternatives) Add(library *libraries.Library) {
6464
if len(alts.Alternatives) > 0 && alts.Alternatives[0].Name != library.Name {
65-
panic(fmt.Sprintf(tr("the library name is different from the set (%[1]s != %[2]s)"), alts.Alternatives[0].Name, library.Name))
65+
panic(fmt.Sprintf("the library name is different from the set (%[1]s != %[2]s)", alts.Alternatives[0].Name, library.Name))
6666
}
6767
alts.Alternatives = append(alts.Alternatives, library)
6868
}

arduino/serialutils/serialutils.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,7 @@ func Reset(portToTouch string, wait bool, cb *ResetProgressCallbacks, dryRun boo
130130
// do nothing!
131131
} else {
132132
if err := TouchSerialPortAt1200bps(portToTouch); err != nil {
133-
fmt.Printf(tr("TOUCH: error during reset: %s"), err)
134-
fmt.Println()
133+
fmt.Println(tr("TOUCH: error during reset: %s", err))
135134
}
136135
}
137136
}

arduino/sketch/sketch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ type InvalidSketchFolderNameError struct {
260260
}
261261

262262
func (e *InvalidSketchFolderNameError) Error() string {
263-
return fmt.Sprintf(tr("no valid sketch found in %[1]s: missing %[2]s"), e.SketchFolder, e.SketchFile)
263+
return tr("no valid sketch found in %[1]s: missing %[2]s", e.SketchFolder, e.SketchFile)
264264
}
265265

266266
// CheckForPdeFiles returns all files ending with .pde extension

cli/board/attach.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func initAttachCommand() *cobra.Command {
4242
Run: runAttachCommand,
4343
}
4444
attachCommand.Flags().StringVar(&attachFlags.searchTimeout, "timeout", "5s",
45-
fmt.Sprintf(tr("The connected devices search timeout, raise it if your board doesn't show up (e.g. to %s)."), "10s"))
45+
tr("The connected devices search timeout, raise it if your board doesn't show up (e.g. to %s).", "10s"))
4646
return attachCommand
4747
}
4848

cli/cli.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func NewCommand() *cobra.Command {
7575
PersistentPostRun: postRun,
7676
}
7777

78-
arduinoCli.SetUsageTemplate(usageTemplate)
78+
arduinoCli.SetUsageTemplate(getUsageTemplate())
7979

8080
createCliCommandTree(arduinoCli)
8181

@@ -103,10 +103,10 @@ func createCliCommandTree(cmd *cobra.Command) {
103103
cmd.AddCommand(version.NewCommand())
104104

105105
cmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, tr("Print the logs on the standard output."))
106-
cmd.PersistentFlags().String("log-level", "", fmt.Sprintf(tr("Messages with this level and above will be logged. Valid levels are: %s, %s, %s, %s, %s, %s, %s"), "trace", "debug", "info", "warn", "error", "fatal", "panic"))
106+
cmd.PersistentFlags().String("log-level", "", tr("Messages with this level and above will be logged. Valid levels are: %s", "trace, debug, info, warn, error, fatal, panic"))
107107
cmd.PersistentFlags().String("log-file", "", tr("Path to the file where logs will be written."))
108-
cmd.PersistentFlags().String("log-format", "", fmt.Sprintf(tr("The output format for the logs, can be {%s|%s}."), "text", "json"))
109-
cmd.PersistentFlags().StringVar(&outputFormat, "format", "text", fmt.Sprintf(tr("The output format, can be {%s|%s}."), "text", "json"))
108+
cmd.PersistentFlags().String("log-format", "", tr("The output format for the logs, can be: %s", "text, json"))
109+
cmd.PersistentFlags().StringVar(&outputFormat, "format", "text", tr("The output format for the logs, can be: %s", "text, json"))
110110
cmd.PersistentFlags().StringVar(&configFile, "config-file", "", tr("The custom config file (if not specified the default will be used)."))
111111
cmd.PersistentFlags().StringSlice("additional-urls", []string{}, tr("Comma-separated list of additional URLs for the Boards Manager."))
112112
cmd.PersistentFlags().Bool("no-color", false, "Disable colored output.")
@@ -196,7 +196,7 @@ func preRun(cmd *cobra.Command, args []string) {
196196
if logFile != "" {
197197
file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
198198
if err != nil {
199-
fmt.Printf(tr("Unable to open file for logging: %s"), logFile)
199+
fmt.Println(tr("Unable to open file for logging: %s", logFile))
200200
os.Exit(errorcodes.ErrBadCall)
201201
}
202202

cli/compile/compile.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"bytes"
2020
"context"
2121
"encoding/json"
22-
"fmt"
2322
"os"
2423

2524
"github.com/arduino/arduino-cli/arduino/discovery"
@@ -94,7 +93,7 @@ func NewCommand() *cobra.Command {
9493
command.Flags().StringArrayVar(&buildProperties, "build-property", []string{},
9594
tr("Override a build property with a custom value. Can be used multiple times for multiple properties."))
9695
command.Flags().StringVar(&warnings, "warnings", "none",
97-
fmt.Sprintf(tr(`Optional, can be "%[1]s", "%[2]s", "%[3]s" and "%[4]s". Defaults to "%[1]s". Used to tell gcc which warning level to use (-W flag).`), "none", "default", "more", "all"))
96+
tr(`Optional, can be: %s. Used to tell gcc which warning level to use (-W flag).`, "none, default, more, all"))
9897
command.Flags().BoolVarP(&verbose, "verbose", "v", false, tr("Optional, turns on verbose mode."))
9998
command.Flags().BoolVar(&quiet, "quiet", false, tr("Optional, suppresses almost every output."))
10099
command.Flags().BoolVarP(&uploadAfterCompile, "upload", "u", false, tr("Upload the binary after the compilation."))
@@ -215,7 +214,7 @@ func run(cmd *cobra.Command, args []string) {
215214

216215
fields := map[string]string{}
217216
if len(userFieldRes.UserFields) > 0 {
218-
feedback.Printf(tr("Uploading to specified board using %s protocol requires the following info:"), discoveryPort.Protocol)
217+
feedback.Print(tr("Uploading to specified board using %s protocol requires the following info:", discoveryPort.Protocol))
219218
fields = arguments.AskForUserFields(userFieldRes.UserFields)
220219
}
221220

cli/config/init.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package config
1717

1818
import (
19-
"fmt"
2019
"os"
2120

2221
"github.com/arduino/arduino-cli/cli/errorcodes"
@@ -107,7 +106,7 @@ func runInitCommand(cmd *cobra.Command, args []string) {
107106
os.Exit(errorcodes.ErrGeneric)
108107
}
109108

110-
msg := fmt.Sprintf(tr("Config file written to: %s"), configFileAbsPath.String())
109+
msg := tr("Config file written to: %s", configFileAbsPath.String())
111110
logrus.Info(msg)
112111
feedback.Print(msg)
113112
}

0 commit comments

Comments
 (0)