Skip to content

Commit f6760b3

Browse files
committed
refactor lib command and use code from arguments, remove structs
1 parent 7a2659d commit f6760b3

File tree

6 files changed

+66
-74
lines changed

6 files changed

+66
-74
lines changed

cli/lib/examples.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ import (
3434
"github.com/spf13/cobra"
3535
)
3636

37+
var (
38+
fqbn arguments.Fqbn
39+
)
40+
3741
func initExamplesCommand() *cobra.Command {
3842
examplesCommand := &cobra.Command{
3943
Use: fmt.Sprintf("examples [%s]", tr("LIBRARY_NAME")),
@@ -46,17 +50,10 @@ func initExamplesCommand() *cobra.Command {
4650
return arguments.GetInstalledLibraries(), cobra.ShellCompDirectiveDefault
4751
},
4852
}
49-
examplesCommand.Flags().StringVarP(&examplesFlags.fqbn, "fqbn", "b", "", tr("Show libraries for the specified board FQBN."))
50-
examplesCommand.RegisterFlagCompletionFunc("fqbn", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
51-
return arguments.GetInstalledBoards(), cobra.ShellCompDirectiveDefault
52-
})
53+
fqbn.AddToCommand(examplesCommand)
5354
return examplesCommand
5455
}
5556

56-
var examplesFlags struct {
57-
fqbn string
58-
}
59-
6057
func runExamplesCommand(cmd *cobra.Command, args []string) {
6158
instance := instance.CreateAndInit()
6259
logrus.Info("Show examples for library")
@@ -70,7 +67,7 @@ func runExamplesCommand(cmd *cobra.Command, args []string) {
7067
Instance: instance,
7168
All: true,
7269
Name: name,
73-
Fqbn: examplesFlags.fqbn,
70+
Fqbn: fqbn.GetFQBN(),
7471
})
7572
if err != nil {
7673
feedback.Errorf(tr("Error getting libraries info: %v"), err)

cli/lib/install.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ import (
3535
semver "go.bug.st/relaxed-semver"
3636
)
3737

38+
var (
39+
noDeps bool
40+
gitURL bool
41+
zipPath bool
42+
)
43+
3844
func initInstallCommand() *cobra.Command {
3945
installCommand := &cobra.Command{
4046
Use: fmt.Sprintf("install %s[@%s]...", tr("LIBRARY"), tr("VERSION_NUMBER")),
@@ -51,22 +57,16 @@ func initInstallCommand() *cobra.Command {
5157
return arguments.GetInstallableLibs(), cobra.ShellCompDirectiveDefault
5258
},
5359
}
54-
installCommand.Flags().BoolVar(&installFlags.noDeps, "no-deps", false, tr("Do not install dependencies."))
55-
installCommand.Flags().BoolVar(&installFlags.gitURL, "git-url", false, tr("Enter git url for libraries hosted on repositories"))
56-
installCommand.Flags().BoolVar(&installFlags.zipPath, "zip-path", false, tr("Enter a path to zip file"))
60+
installCommand.Flags().BoolVar(&noDeps, "no-deps", false, tr("Do not install dependencies."))
61+
installCommand.Flags().BoolVar(&gitURL, "git-url", false, tr("Enter git url for libraries hosted on repositories"))
62+
installCommand.Flags().BoolVar(&zipPath, "zip-path", false, tr("Enter a path to zip file"))
5763
return installCommand
5864
}
5965

60-
var installFlags struct {
61-
noDeps bool
62-
gitURL bool
63-
zipPath bool
64-
}
65-
6666
func runInstallCommand(cmd *cobra.Command, args []string) {
6767
instance := instance.CreateAndInit()
6868

69-
if installFlags.zipPath || installFlags.gitURL {
69+
if zipPath || gitURL {
7070
if !configuration.Settings.GetBool("library.enable_unsafe_install") {
7171
documentationURL := "https://arduino.github.io/arduino-cli/latest/configuration/#configuration-keys"
7272
_, err := semver.Parse(globals.VersionInfo.VersionString)
@@ -80,7 +80,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
8080
feedback.Print(tr("--git-url and --zip-path flags allow installing untrusted files, use it at your own risk."))
8181
}
8282

83-
if installFlags.zipPath {
83+
if zipPath {
8484
for _, path := range args {
8585
err := lib.ZipLibraryInstall(context.Background(), &rpc.ZipLibraryInstallRequest{
8686
Instance: instance,
@@ -95,7 +95,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
9595
return
9696
}
9797

98-
if installFlags.gitURL {
98+
if gitURL {
9999
for _, url := range args {
100100
if url == "." {
101101
wd, err := paths.Getwd()
@@ -129,7 +129,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
129129
Instance: instance,
130130
Name: libRef.Name,
131131
Version: libRef.Version,
132-
NoDeps: installFlags.noDeps,
132+
NoDeps: noDeps,
133133
}
134134
err := lib.LibraryInstall(context.Background(), libraryInstallRequest, output.ProgressBar(), output.TaskProgress())
135135
if err != nil {

cli/lib/list.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ import (
3232
"github.com/spf13/cobra"
3333
)
3434

35+
var (
36+
all bool
37+
updatable bool
38+
// fqbn arguments.Fqbn //already defined in examples.go
39+
)
40+
3541
func initListCommand() *cobra.Command {
3642
listCommand := &cobra.Command{
3743
Use: fmt.Sprintf("list [%s]", tr("LIBNAME")),
@@ -45,18 +51,12 @@ not listed, they can be listed by adding the --all flag.`),
4551
Args: cobra.MaximumNArgs(1),
4652
Run: runListCommand,
4753
}
48-
listCommand.Flags().BoolVar(&listFlags.all, "all", false, tr("Include built-in libraries (from platforms and IDE) in listing."))
49-
listCommand.Flags().StringVarP(&listFlags.fqbn, "fqbn", "b", "", tr("Show libraries for the specified board FQBN."))
50-
listCommand.Flags().BoolVar(&listFlags.updatable, "updatable", false, tr("List updatable libraries."))
54+
listCommand.Flags().BoolVar(&all, "all", false, tr("Include built-in libraries (from platforms and IDE) in listing."))
55+
fqbn.AddToCommand(listCommand)
56+
listCommand.Flags().BoolVar(&updatable, "updatable", false, tr("List updatable libraries."))
5157
return listCommand
5258
}
5359

54-
var listFlags struct {
55-
all bool
56-
updatable bool
57-
fqbn string
58-
}
59-
6060
func runListCommand(cmd *cobra.Command, args []string) {
6161
instance := instance.CreateAndInit()
6262
logrus.Info("Listing")
@@ -68,22 +68,22 @@ func runListCommand(cmd *cobra.Command, args []string) {
6868

6969
res, err := lib.LibraryList(context.Background(), &rpc.LibraryListRequest{
7070
Instance: instance,
71-
All: listFlags.all,
72-
Updatable: listFlags.updatable,
71+
All: all,
72+
Updatable: updatable,
7373
Name: name,
74-
Fqbn: listFlags.fqbn,
74+
Fqbn: fqbn.GetFQBN(),
7575
})
7676
if err != nil {
7777
feedback.Errorf(tr("Error listing Libraries: %v"), err)
7878
os.Exit(errorcodes.ErrGeneric)
7979
}
8080

8181
libs := []*rpc.InstalledLibrary{}
82-
if listFlags.fqbn == "" {
82+
if fqbn.GetFQBN() == "" {
8383
libs = res.GetInstalledLibraries()
8484
} else {
8585
for _, lib := range res.GetInstalledLibraries() {
86-
if lib.Library.CompatibleWith[listFlags.fqbn] {
86+
if lib.Library.CompatibleWith[fqbn.GetFQBN()] {
8787
libs = append(libs, lib)
8888
}
8989
}
@@ -111,7 +111,7 @@ func (ir installedResult) Data() interface{} {
111111

112112
func (ir installedResult) String() string {
113113
if ir.installedLibs == nil || len(ir.installedLibs) == 0 {
114-
if listFlags.updatable {
114+
if updatable {
115115
return tr("No updates available.")
116116
}
117117
return tr("No libraries installed.")

cli/lib/search.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ import (
3434
semver "go.bug.st/relaxed-semver"
3535
)
3636

37+
var (
38+
namesOnly bool // if true outputs lib names only.
39+
)
40+
3741
func initSearchCommand() *cobra.Command {
3842
searchCommand := &cobra.Command{
3943
Use: fmt.Sprintf("search [%s]", tr("LIBRARY_NAME")),
@@ -43,14 +47,10 @@ func initSearchCommand() *cobra.Command {
4347
Args: cobra.ArbitraryArgs,
4448
Run: runSearchCommand,
4549
}
46-
searchCommand.Flags().BoolVar(&searchFlags.namesOnly, "names", false, tr("Show library names only."))
50+
searchCommand.Flags().BoolVar(&namesOnly, "names", false, tr("Show library names only."))
4751
return searchCommand
4852
}
4953

50-
var searchFlags struct {
51-
namesOnly bool // if true outputs lib names only.
52-
}
53-
5454
func runSearchCommand(cmd *cobra.Command, args []string) {
5555
inst, status := instance.Create()
5656
if status != nil {
@@ -83,7 +83,7 @@ func runSearchCommand(cmd *cobra.Command, args []string) {
8383

8484
feedback.PrintResult(result{
8585
results: searchResp,
86-
namesOnly: searchFlags.namesOnly,
86+
namesOnly: namesOnly,
8787
})
8888

8989
logrus.Info("Done")

cli/lib/upgrade.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
)
2929

3030
func initUpgradeCommand() *cobra.Command {
31-
listCommand := &cobra.Command{
31+
upgradeCommand := &cobra.Command{
3232
Use: "upgrade",
3333
Short: tr("Upgrades installed libraries."),
3434
Long: tr("This command upgrades an installed library to the latest available version. Multiple libraries can be passed separated by a space. If no arguments are provided, the command will upgrade all the installed libraries where an update is available."),
@@ -38,7 +38,7 @@ func initUpgradeCommand() *cobra.Command {
3838
Args: cobra.ArbitraryArgs,
3939
Run: runUpgradeCommand,
4040
}
41-
return listCommand
41+
return upgradeCommand
4242
}
4343

4444
func runUpgradeCommand(cmd *cobra.Command, args []string) {

0 commit comments

Comments
 (0)