Skip to content

Commit 7340430

Browse files
committed
apply suggestions from code review
1 parent 72463d5 commit 7340430

File tree

7 files changed

+57
-44
lines changed

7 files changed

+57
-44
lines changed

Diff for: cli/arguments/completion.go

+32-21
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
// It returns a list of fqbn
1717
// it's taken from cli/board/listall.go
1818
func GetInstalledBoards() []string {
19-
inst := instance.CreateAndInit() // TODO optimize this: it does not make sense to create an instance everytime
19+
inst := instance.CreateAndInit()
2020

2121
list, _ := board.ListAll(context.Background(), &rpc.BoardListAllRequest{
2222
Instance: inst,
@@ -34,24 +34,25 @@ func GetInstalledBoards() []string {
3434
// GetInstalledProtocols is an helper function useful to autocomplete.
3535
// It returns a list of protocols available based on the installed boards
3636
func GetInstalledProtocols() []string {
37-
inst := instance.CreateAndInit() // TODO optimize this: it does not make sense to create an instance everytime
37+
inst := instance.CreateAndInit()
3838
pm := commands.GetPackageManager(inst.Id)
3939
boards := pm.InstalledBoards()
4040

41-
// use this strange map because it should be more optimized
42-
// we use only the key and not the value because we do not need it
43-
intalledProtocolsMap := make(map[string]struct{})
41+
installedProtocols := make(map[string]struct{})
4442
for _, board := range boards {
45-
// we filter and elaborate a bit the informations present in Properties
4643
for _, protocol := range board.Properties.SubTree("upload.tool").FirstLevelKeys() {
47-
if protocol != "default" { // remove this value since it's the default one
48-
intalledProtocolsMap[protocol] = struct{}{}
44+
if protocol == "default" {
45+
// default is used as fallback when trying to upload to a board
46+
// using a protocol not defined for it, it's useless showing it
47+
// in autocompletion
48+
continue
4949
}
50+
installedProtocols[protocol] = struct{}{}
5051
}
5152
}
52-
res := make([]string, len(intalledProtocolsMap))
53+
res := make([]string, len(installedProtocols))
5354
i := 0
54-
for k := range intalledProtocolsMap {
55+
for k := range installedProtocols {
5556
res[i] = k
5657
i++
5758
}
@@ -61,7 +62,7 @@ func GetInstalledProtocols() []string {
6162
// GetInstalledProgrammers is an helper function useful to autocomplete.
6263
// It returns a list of programmers available based on the installed boards
6364
func GetInstalledProgrammers() []string {
64-
inst := instance.CreateAndInit() // TODO optimize this: it does not make sense to create an instance everytime
65+
inst := instance.CreateAndInit()
6566
pm := commands.GetPackageManager(inst.Id)
6667

6768
// we need the list of the available fqbn in order to get the list of the programmers
@@ -72,8 +73,8 @@ func GetInstalledProgrammers() []string {
7273
})
7374

7475
installedProgrammers := make(map[string]string)
75-
for _, i := range list.Boards {
76-
fqbn, _ := cores.ParseFQBN(i.Fqbn)
76+
for _, board := range list.Boards {
77+
fqbn, _ := cores.ParseFQBN(board.Fqbn)
7778
_, boardPlatform, _, _, _, _ := pm.ResolveFQBN(fqbn)
7879
for programmerID, programmer := range boardPlatform.Programmers {
7980
installedProgrammers[programmerID] = programmer.Name
@@ -92,7 +93,7 @@ func GetInstalledProgrammers() []string {
9293
// GetUninstallableCores is an helper function useful to autocomplete.
9394
// It returns a list of cores which can be uninstalled
9495
func GetUninstallableCores() []string {
95-
inst := instance.CreateAndInit() // TODO optimize this: it does not make sense to create an instance everytime
96+
inst := instance.CreateAndInit()
9697

9798
platforms, _ := core.GetPlatforms(&rpc.PlatformListRequest{
9899
Instance: inst,
@@ -110,7 +111,7 @@ func GetUninstallableCores() []string {
110111
// GetInstallableCores is an helper function useful to autocomplete.
111112
// It returns a list of cores which can be installed/downloaded
112113
func GetInstallableCores() []string {
113-
inst := instance.CreateAndInit() // TODO optimize this: it does not make sense to create an instance everytime
114+
inst := instance.CreateAndInit()
114115

115116
platforms, _ := core.PlatformSearch(&rpc.PlatformSearchRequest{
116117
Instance: inst,
@@ -125,13 +126,23 @@ func GetInstallableCores() []string {
125126
return res
126127
}
127128

128-
// GetUninstallableLibs is an helper function useful to autocomplete.
129+
// GetInstalledLibraries is an helper function useful to autocomplete.
130+
// It returns a list of libs which are currently installed, including the builtin ones
131+
func GetInstalledLibraries() []string {
132+
return getLibraries(true)
133+
}
134+
135+
// GetUninstallableLibraries is an helper function useful to autocomplete.
129136
// It returns a list of libs which can be uninstalled
130-
func GetUninstallableLibs() []string {
131-
inst := instance.CreateAndInit() // TODO optimize this: it does not make sense to create an instance everytime
137+
func GetUninstallableLibraries() []string {
138+
return getLibraries(false)
139+
}
140+
141+
func getLibraries(all bool) []string {
142+
inst := instance.CreateAndInit()
132143
libs, _ := lib.LibraryList(context.Background(), &rpc.LibraryListRequest{
133144
Instance: inst,
134-
All: false,
145+
All: all,
135146
Updatable: false,
136147
Name: "",
137148
Fqbn: "",
@@ -147,7 +158,7 @@ func GetUninstallableLibs() []string {
147158
// GetInstallableLibs is an helper function useful to autocomplete.
148159
// It returns a list of libs which can be installed/downloaded
149160
func GetInstallableLibs() []string {
150-
inst := instance.CreateAndInit() // TODO optimize this: it does not make sense to create an instance everytime
161+
inst := instance.CreateAndInit()
151162

152163
libs, _ := lib.LibrarySearch(context.Background(), &rpc.LibrarySearchRequest{
153164
Instance: inst,
@@ -165,7 +176,7 @@ func GetInstallableLibs() []string {
165176
// It returns a list of boards which are currently connected
166177
// Obviously it does not suggests network ports because of the timeout
167178
func GetConnectedBoards() []string {
168-
inst := instance.CreateAndInit() // TODO optimize this: it does not make sense to create an instance everytime
179+
inst := instance.CreateAndInit()
169180

170181
list, _ := board.List(&rpc.BoardListRequest{
171182
Instance: inst,

Diff for: cli/cli.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,20 @@ func createCliCommandTree(cmd *cobra.Command) {
105105
cmd.AddCommand(version.NewCommand())
106106

107107
cmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, tr("Print the logs on the standard output."))
108-
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"))
108+
validLogLevels := []string{"trace", "debug", "info", "warn", "error", "fatal", "panic"}
109+
cmd.PersistentFlags().String("log-level", "", tr("Messages with this level and above will be logged. Valid levels are: %s", strings.Join(validLogLevels, ", ")))
109110
cmd.RegisterFlagCompletionFunc("log-level", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
110-
return []string{"trace", "debug", "info", "warn", "error", "fatal", "panic"}, cobra.ShellCompDirectiveDefault
111+
return validLogLevels, cobra.ShellCompDirectiveDefault
111112
})
112113
cmd.PersistentFlags().String("log-file", "", tr("Path to the file where logs will be written."))
113-
cmd.PersistentFlags().String("log-format", "", tr("The output format for the logs, can be: %s", "text, json"))
114+
validFormats := []string{"text", "json"}
115+
cmd.PersistentFlags().String("log-format", "", tr("The output format for the logs, can be: %s", strings.Join(validFormats, ", ")))
114116
cmd.RegisterFlagCompletionFunc("log-format", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
115-
return []string{"text", "json"}, cobra.ShellCompDirectiveDefault
117+
return validFormats, cobra.ShellCompDirectiveDefault
116118
})
117-
cmd.PersistentFlags().StringVar(&outputFormat, "format", "text", tr("The output format for the logs, can be: %s", "text, json"))
119+
cmd.PersistentFlags().StringVar(&outputFormat, "format", "text", tr("The output format for the logs, can be: %s", strings.Join(validFormats, ", ")))
118120
cmd.RegisterFlagCompletionFunc("format", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
119-
return []string{"text", "json"}, cobra.ShellCompDirectiveDefault
121+
return validFormats, cobra.ShellCompDirectiveDefault
120122
})
121123
cmd.PersistentFlags().StringVar(&configFile, "config-file", "", tr("The custom config file (if not specified the default will be used)."))
122124
cmd.PersistentFlags().StringSlice("additional-urls", []string{}, tr("Comma-separated list of additional URLs for the Boards Manager."))

Diff for: cli/lib/check_deps.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func initDepsCommand() *cobra.Command {
4141
Args: cobra.ExactArgs(1),
4242
Run: runDepsCommand,
4343
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
44-
return arguments.GetUninstallableLibs(), cobra.ShellCompDirectiveDefault
44+
return arguments.GetInstalledLibraries(), cobra.ShellCompDirectiveDefault
4545
},
4646
}
4747
return depsCommand

Diff for: cli/lib/examples.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func initExamplesCommand() *cobra.Command {
4343
Args: cobra.MaximumNArgs(1),
4444
Run: runExamplesCommand,
4545
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
46-
return arguments.GetUninstallableLibs(), cobra.ShellCompDirectiveDefault
46+
return arguments.GetInstalledLibraries(), cobra.ShellCompDirectiveDefault
4747
},
4848
}
4949
examplesCommand.Flags().StringVarP(&examplesFlags.fqbn, "fqbn", "b", "", tr("Show libraries for the specified board FQBN."))

Diff for: cli/lib/uninstall.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func initUninstallCommand() *cobra.Command {
4040
Args: cobra.MinimumNArgs(1),
4141
Run: runUninstallCommand,
4242
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
43-
return arguments.GetUninstallableLibs(), cobra.ShellCompDirectiveDefault
43+
return arguments.GetUninstallableLibraries(), cobra.ShellCompDirectiveDefault
4444
},
4545
}
4646
return uninstallCommand

Diff for: i18n/data/en.po

+10-10
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ msgstr "Checksum:"
365365
msgid "Clean caches."
366366
msgstr "Clean caches."
367367

368-
#: cli/cli.go:122
368+
#: cli/cli.go:124
369369
msgid "Comma-separated list of additional URLs for the Boards Manager."
370370
msgstr "Comma-separated list of additional URLs for the Boards Manager."
371371

@@ -1214,7 +1214,7 @@ msgstr "Internal error in cache"
12141214
msgid "Invalid '%[1]s' property: %[2]s"
12151215
msgstr "Invalid '%[1]s' property: %[2]s"
12161216

1217-
#: cli/cli.go:263
1217+
#: cli/cli.go:265
12181218
msgid "Invalid Call : should show Help, but it is available only in TEXT mode."
12191219
msgstr "Invalid Call : should show Help, but it is available only in TEXT mode."
12201220

@@ -1271,11 +1271,11 @@ msgstr "Invalid library"
12711271
msgid "Invalid network.proxy '%[1]s': %[2]s"
12721272
msgstr "Invalid network.proxy '%[1]s': %[2]s"
12731273

1274-
#: cli/cli.go:224
1274+
#: cli/cli.go:226
12751275
msgid "Invalid option for --log-level: %s"
12761276
msgstr "Invalid option for --log-level: %s"
12771277

1278-
#: cli/cli.go:241
1278+
#: cli/cli.go:243
12791279
msgid "Invalid output format: %s"
12801280
msgstr "Invalid output format: %s"
12811281

@@ -1444,7 +1444,7 @@ msgstr "Maintainer: %s"
14441444
msgid "Max time to wait for port discovery, e.g.: 30s, 1m"
14451445
msgstr "Max time to wait for port discovery, e.g.: 30s, 1m"
14461446

1447-
#: cli/cli.go:108
1447+
#: cli/cli.go:109
14481448
msgid "Messages with this level and above will be logged. Valid levels are: %s"
14491449
msgstr "Messages with this level and above will be logged. Valid levels are: %s"
14501450

@@ -1667,7 +1667,7 @@ msgstr "Package website:"
16671667
msgid "Paragraph: %s"
16681668
msgstr "Paragraph: %s"
16691669

1670-
#: cli/cli.go:112
1670+
#: cli/cli.go:113
16711671
msgid "Path to the file where logs will be written."
16721672
msgstr "Path to the file where logs will be written."
16731673

@@ -2044,7 +2044,7 @@ msgstr "The connected devices search timeout, raise it if your board doesn't sho
20442044
msgid "The connected devices search timeout, raise it if your board doesn't show up e.g.: 10s"
20452045
msgstr "The connected devices search timeout, raise it if your board doesn't show up e.g.: 10s"
20462046

2047-
#: cli/cli.go:121
2047+
#: cli/cli.go:123
20482048
msgid "The custom config file (if not specified the default will be used)."
20492049
msgstr "The custom config file (if not specified the default will be used)."
20502050

@@ -2064,8 +2064,8 @@ msgid "The key '%[1]v' is not a list of items, can't remove from it.\n"
20642064
msgstr "The key '%[1]v' is not a list of items, can't remove from it.\n"
20652065
"Maybe use '%[2]s'?"
20662066

2067-
#: cli/cli.go:113
2068-
#: cli/cli.go:117
2067+
#: cli/cli.go:115
2068+
#: cli/cli.go:119
20692069
msgid "The output format for the logs, can be: %s"
20702070
msgstr "The output format for the logs, can be: %s"
20712071

@@ -2147,7 +2147,7 @@ msgstr "Unable to get Local App Data Folder: %v"
21472147
msgid "Unable to get user home dir: %v"
21482148
msgstr "Unable to get user home dir: %v"
21492149

2150-
#: cli/cli.go:210
2150+
#: cli/cli.go:212
21512151
msgid "Unable to open file for logging: %s"
21522152
msgstr "Unable to open file for logging: %s"
21532153

Diff for: i18n/rice-box.go

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)