Commit 0f259643 authored by Umberto Baldi's avatar Umberto Baldi

refactor lib command and use code from arguments, remove structs

parent 4428a46d
......@@ -34,6 +34,10 @@ import (
"github.com/spf13/cobra"
)
var (
fqbn arguments.Fqbn
)
func initExamplesCommand() *cobra.Command {
examplesCommand := &cobra.Command{
Use: fmt.Sprintf("examples [%s]", tr("LIBRARY_NAME")),
......@@ -46,17 +50,10 @@ func initExamplesCommand() *cobra.Command {
return arguments.GetInstalledLibraries(), cobra.ShellCompDirectiveDefault
},
}
examplesCommand.Flags().StringVarP(&examplesFlags.fqbn, "fqbn", "b", "", tr("Show libraries for the specified board FQBN."))
examplesCommand.RegisterFlagCompletionFunc("fqbn", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return arguments.GetInstalledBoards(), cobra.ShellCompDirectiveDefault
})
fqbn.AddToCommand(examplesCommand)
return examplesCommand
}
var examplesFlags struct {
fqbn string
}
func runExamplesCommand(cmd *cobra.Command, args []string) {
instance := instance.CreateAndInit()
logrus.Info("Show examples for library")
......@@ -70,7 +67,7 @@ func runExamplesCommand(cmd *cobra.Command, args []string) {
Instance: instance,
All: true,
Name: name,
Fqbn: examplesFlags.fqbn,
Fqbn: fqbn.String(),
})
if err != nil {
feedback.Errorf(tr("Error getting libraries info: %v"), err)
......
......@@ -35,6 +35,12 @@ import (
semver "go.bug.st/relaxed-semver"
)
var (
noDeps bool
gitURL bool
zipPath bool
)
func initInstallCommand() *cobra.Command {
installCommand := &cobra.Command{
Use: fmt.Sprintf("install %s[@%s]...", tr("LIBRARY"), tr("VERSION_NUMBER")),
......@@ -51,22 +57,16 @@ func initInstallCommand() *cobra.Command {
return arguments.GetInstallableLibs(), cobra.ShellCompDirectiveDefault
},
}
installCommand.Flags().BoolVar(&installFlags.noDeps, "no-deps", false, tr("Do not install dependencies."))
installCommand.Flags().BoolVar(&installFlags.gitURL, "git-url", false, tr("Enter git url for libraries hosted on repositories"))
installCommand.Flags().BoolVar(&installFlags.zipPath, "zip-path", false, tr("Enter a path to zip file"))
installCommand.Flags().BoolVar(&noDeps, "no-deps", false, tr("Do not install dependencies."))
installCommand.Flags().BoolVar(&gitURL, "git-url", false, tr("Enter git url for libraries hosted on repositories"))
installCommand.Flags().BoolVar(&zipPath, "zip-path", false, tr("Enter a path to zip file"))
return installCommand
}
var installFlags struct {
noDeps bool
gitURL bool
zipPath bool
}
func runInstallCommand(cmd *cobra.Command, args []string) {
instance := instance.CreateAndInit()
if installFlags.zipPath || installFlags.gitURL {
if zipPath || gitURL {
if !configuration.Settings.GetBool("library.enable_unsafe_install") {
documentationURL := "https://arduino.github.io/arduino-cli/latest/configuration/#configuration-keys"
_, err := semver.Parse(globals.VersionInfo.VersionString)
......@@ -80,7 +80,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
feedback.Print(tr("--git-url and --zip-path flags allow installing untrusted files, use it at your own risk."))
}
if installFlags.zipPath {
if zipPath {
for _, path := range args {
err := lib.ZipLibraryInstall(context.Background(), &rpc.ZipLibraryInstallRequest{
Instance: instance,
......@@ -95,7 +95,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
return
}
if installFlags.gitURL {
if gitURL {
for _, url := range args {
if url == "." {
wd, err := paths.Getwd()
......@@ -129,7 +129,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
Instance: instance,
Name: libRef.Name,
Version: libRef.Version,
NoDeps: installFlags.noDeps,
NoDeps: noDeps,
}
err := lib.LibraryInstall(context.Background(), libraryInstallRequest, output.ProgressBar(), output.TaskProgress())
if err != nil {
......
......@@ -32,6 +32,11 @@ import (
"github.com/spf13/cobra"
)
var (
all bool
updatable bool
)
func initListCommand() *cobra.Command {
listCommand := &cobra.Command{
Use: fmt.Sprintf("list [%s]", tr("LIBNAME")),
......@@ -45,18 +50,12 @@ not listed, they can be listed by adding the --all flag.`),
Args: cobra.MaximumNArgs(1),
Run: runListCommand,
}
listCommand.Flags().BoolVar(&listFlags.all, "all", false, tr("Include built-in libraries (from platforms and IDE) in listing."))
listCommand.Flags().StringVarP(&listFlags.fqbn, "fqbn", "b", "", tr("Show libraries for the specified board FQBN."))
listCommand.Flags().BoolVar(&listFlags.updatable, "updatable", false, tr("List updatable libraries."))
listCommand.Flags().BoolVar(&all, "all", false, tr("Include built-in libraries (from platforms and IDE) in listing."))
fqbn.AddToCommand(listCommand)
listCommand.Flags().BoolVar(&updatable, "updatable", false, tr("List updatable libraries."))
return listCommand
}
var listFlags struct {
all bool
updatable bool
fqbn string
}
func runListCommand(cmd *cobra.Command, args []string) {
instance := instance.CreateAndInit()
logrus.Info("Listing")
......@@ -68,10 +67,10 @@ func runListCommand(cmd *cobra.Command, args []string) {
res, err := lib.LibraryList(context.Background(), &rpc.LibraryListRequest{
Instance: instance,
All: listFlags.all,
Updatable: listFlags.updatable,
All: all,
Updatable: updatable,
Name: name,
Fqbn: listFlags.fqbn,
Fqbn: fqbn.String(),
})
if err != nil {
feedback.Errorf(tr("Error listing Libraries: %v"), err)
......@@ -79,11 +78,11 @@ func runListCommand(cmd *cobra.Command, args []string) {
}
libs := []*rpc.InstalledLibrary{}
if listFlags.fqbn == "" {
if fqbn.String() == "" {
libs = res.GetInstalledLibraries()
} else {
for _, lib := range res.GetInstalledLibraries() {
if lib.Library.CompatibleWith[listFlags.fqbn] {
if lib.Library.CompatibleWith[fqbn.String()] {
libs = append(libs, lib)
}
}
......@@ -111,7 +110,7 @@ func (ir installedResult) Data() interface{} {
func (ir installedResult) String() string {
if ir.installedLibs == nil || len(ir.installedLibs) == 0 {
if listFlags.updatable {
if updatable {
return tr("No updates available.")
}
return tr("No libraries installed.")
......
......@@ -34,6 +34,10 @@ import (
semver "go.bug.st/relaxed-semver"
)
var (
namesOnly bool // if true outputs lib names only.
)
func initSearchCommand() *cobra.Command {
searchCommand := &cobra.Command{
Use: fmt.Sprintf("search [%s]", tr("LIBRARY_NAME")),
......@@ -43,14 +47,10 @@ func initSearchCommand() *cobra.Command {
Args: cobra.ArbitraryArgs,
Run: runSearchCommand,
}
searchCommand.Flags().BoolVar(&searchFlags.namesOnly, "names", false, tr("Show library names only."))
searchCommand.Flags().BoolVar(&namesOnly, "names", false, tr("Show library names only."))
return searchCommand
}
var searchFlags struct {
namesOnly bool // if true outputs lib names only.
}
func runSearchCommand(cmd *cobra.Command, args []string) {
inst, status := instance.Create()
if status != nil {
......@@ -83,7 +83,7 @@ func runSearchCommand(cmd *cobra.Command, args []string) {
feedback.PrintResult(result{
results: searchResp,
namesOnly: searchFlags.namesOnly,
namesOnly: namesOnly,
})
logrus.Info("Done")
......
......@@ -28,7 +28,7 @@ import (
)
func initUpgradeCommand() *cobra.Command {
listCommand := &cobra.Command{
upgradeCommand := &cobra.Command{
Use: "upgrade",
Short: tr("Upgrades installed libraries."),
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 {
Args: cobra.ArbitraryArgs,
Run: runUpgradeCommand,
}
return listCommand
return upgradeCommand
}
func runUpgradeCommand(cmd *cobra.Command, args []string) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment