Skip to content

Commit 89d1780

Browse files
committed
Porting of command lib/download
1 parent 1711a26 commit 89d1780

19 files changed

+854
-122
lines changed

cli/lib/download.go

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* This file is part of arduino-cli.
3+
*
4+
* Copyright 2018 ARDUINO SA (http://www.arduino.cc/)
5+
*
6+
* This software is released under the GNU General Public License version 3,
7+
* which covers the main part of arduino-cli.
8+
* The terms of this license can be found at:
9+
* https://www.gnu.org/licenses/gpl-3.0.en.html
10+
*
11+
* You can be released from the requirements of the above licenses by purchasing
12+
* a commercial license. Buying such a license is mandatory if you want to modify or
13+
* otherwise use the software for commercial activities involving the Arduino
14+
* software without disclosing the source code of your own applications. To purchase
15+
* a commercial license, send an email to license@arduino.cc.
16+
*/
17+
18+
package lib
19+
20+
import (
21+
"context"
22+
23+
"github.com/arduino/arduino-cli/common/formatter"
24+
25+
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
26+
"github.com/arduino/arduino-cli/cli"
27+
"github.com/arduino/arduino-cli/commands/lib"
28+
"github.com/arduino/arduino-cli/output"
29+
"github.com/arduino/arduino-cli/rpc"
30+
"github.com/spf13/cobra"
31+
)
32+
33+
func initDownloadCommand() *cobra.Command {
34+
downloadCommand := &cobra.Command{
35+
Use: "download [LIBRARY_NAME(S)]",
36+
Short: "Downloads one or more libraries without installing them.",
37+
Long: "Downloads one or more libraries without installing them.",
38+
Example: "" +
39+
" " + cli.AppName + " lib download AudioZero # for the latest version.\n" +
40+
" " + cli.AppName + " lib download AudioZero@1.0.0 # for a specific version.",
41+
Args: cobra.MinimumNArgs(1),
42+
Run: runDownloadCommand,
43+
}
44+
return downloadCommand
45+
}
46+
47+
func runDownloadCommand(cmd *cobra.Command, args []string) {
48+
instance := cli.CreateInstance()
49+
pairs, err := librariesindex.ParseArgs(args)
50+
if err != nil {
51+
formatter.PrintError(err, "Error in oparsing arguments")
52+
}
53+
for _, library := range pairs {
54+
_, err := lib.LibraryDownload(context.Background(), &rpc.LibraryDownloadReq{
55+
Instance: instance,
56+
Name: library.Name,
57+
Version: library.Version.String(),
58+
}, output.DownloadProgressBar())
59+
if err != nil {
60+
formatter.PrintError(err, "Error downloading "+library.String())
61+
}
62+
}
63+
}

cli/lib/install.go

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* This file is part of arduino-cli.
3+
*
4+
* Copyright 2018 ARDUINO SA (http://www.arduino.cc/)
5+
*
6+
* This software is released under the GNU General Public License version 3,
7+
* which covers the main part of arduino-cli.
8+
* The terms of this license can be found at:
9+
* https://www.gnu.org/licenses/gpl-3.0.en.html
10+
*
11+
* You can be released from the requirements of the above licenses by purchasing
12+
* a commercial license. Buying such a license is mandatory if you want to modify or
13+
* otherwise use the software for commercial activities involving the Arduino
14+
* software without disclosing the source code of your own applications. To purchase
15+
* a commercial license, send an email to license@arduino.cc.
16+
*/
17+
18+
package lib
19+
20+
import (
21+
"os"
22+
23+
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
24+
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
25+
"github.com/arduino/arduino-cli/cli"
26+
"github.com/arduino/arduino-cli/common/formatter"
27+
"github.com/sirupsen/logrus"
28+
"github.com/spf13/cobra"
29+
)
30+
31+
func initInstallCommand() *cobra.Command {
32+
installCommand := &cobra.Command{
33+
Use: "install LIBRARY[@VERSION_NUMBER](S)",
34+
Short: "Installs one of more specified libraries into the system.",
35+
Long: "Installs one or more specified libraries into the system.",
36+
Example: "" +
37+
" " + cli.AppName + " lib install AudioZero # for the latest version.\n" +
38+
" " + cli.AppName + " lib install AudioZero@1.0.0 # for the specific version.",
39+
Args: cobra.MinimumNArgs(1),
40+
Run: runInstallCommand,
41+
}
42+
return installCommand
43+
}
44+
45+
func runInstallCommand(cmd *cobra.Command, args []string) {
46+
logrus.Info("Executing `arduino lib install`")
47+
lm := cli.InitLibraryManager(cli.Config)
48+
49+
refs, err := librariesindex.ParseArgs(args)
50+
if err != nil {
51+
formatter.PrintError(err, "Arguments error")
52+
os.Exit(cli.ErrBadArgument)
53+
}
54+
//downloadLibrariesFromReferences(lm, refs)
55+
installLibrariesFromReferences(lm, refs)
56+
}
57+
58+
func installLibrariesFromReferences(lm *librariesmanager.LibrariesManager, refs []*librariesindex.Reference) {
59+
libReleases := []*librariesindex.Release{}
60+
for _, ref := range refs {
61+
rel := lm.Index.FindRelease(ref)
62+
if rel == nil {
63+
formatter.PrintErrorMessage("Error: library " + ref.String() + " not found")
64+
os.Exit(cli.ErrBadCall)
65+
}
66+
libReleases = append(libReleases, rel)
67+
}
68+
installLibraries(lm, libReleases)
69+
}
70+
71+
func installLibraries(lm *librariesmanager.LibrariesManager, libReleases []*librariesindex.Release) {
72+
for _, libRelease := range libReleases {
73+
logrus.WithField("library", libRelease).Info("Installing library")
74+
75+
if _, err := lm.Install(libRelease); err != nil {
76+
logrus.WithError(err).Warn("Error installing library ", libRelease)
77+
formatter.PrintError(err, "Error installing library: "+libRelease.String())
78+
os.Exit(cli.ErrGeneric)
79+
}
80+
81+
formatter.Print("Installed " + libRelease.String())
82+
}
83+
}
File renamed without changes.

cli/lib/list.go

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* This file is part of arduino-cli.
3+
*
4+
* Copyright 2018 ARDUINO SA (http://www.arduino.cc/)
5+
*
6+
* This software is released under the GNU General Public License version 3,
7+
* which covers the main part of arduino-cli.
8+
* The terms of this license can be found at:
9+
* https://www.gnu.org/licenses/gpl-3.0.en.html
10+
*
11+
* You can be released from the requirements of the above licenses by purchasing
12+
* a commercial license. Buying such a license is mandatory if you want to modify or
13+
* otherwise use the software for commercial activities involving the Arduino
14+
* software without disclosing the source code of your own applications. To purchase
15+
* a commercial license, send an email to license@arduino.cc.
16+
*/
17+
18+
package lib
19+
20+
import (
21+
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
22+
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
23+
"github.com/arduino/arduino-cli/cli"
24+
"github.com/arduino/arduino-cli/common/formatter"
25+
"github.com/arduino/arduino-cli/common/formatter/output"
26+
"github.com/sirupsen/logrus"
27+
"github.com/spf13/cobra"
28+
)
29+
30+
func initListCommand() *cobra.Command {
31+
listCommand := &cobra.Command{
32+
Use: "list",
33+
Short: "Shows a list of all installed libraries.",
34+
Long: "Shows a list of all installed libraries.",
35+
Example: " " + cli.AppName + " lib list",
36+
Args: cobra.NoArgs,
37+
Run: runListCommand,
38+
}
39+
listCommand.Flags().BoolVar(&listFlags.all, "all", false, "Include built-in libraries (from platforms and IDE) in listing.")
40+
listCommand.Flags().BoolVar(&listFlags.updatable, "updatable", false, "List updatable libraries.")
41+
return listCommand
42+
}
43+
44+
var listFlags struct {
45+
all bool
46+
updatable bool
47+
}
48+
49+
func runListCommand(cmd *cobra.Command, args []string) {
50+
logrus.Info("Listing")
51+
52+
var lm *librariesmanager.LibrariesManager
53+
if listFlags.all {
54+
_, lm = cli.InitPackageAndLibraryManager()
55+
} else {
56+
lm = cli.InitLibraryManager(cli.Config)
57+
}
58+
59+
res := ListLibraries(lm, listFlags.updatable)
60+
if len(res.Libraries) > 0 {
61+
formatter.Print(res)
62+
}
63+
logrus.Info("Done")
64+
}
65+
66+
// ListLibraries returns the list of installed libraries. If updatable is true it
67+
// returns only the libraries that may be updated.
68+
func ListLibraries(lm *librariesmanager.LibrariesManager, updatable bool) *output.InstalledLibraries {
69+
res := &output.InstalledLibraries{}
70+
for _, libAlternatives := range lm.Libraries {
71+
for _, lib := range libAlternatives.Alternatives {
72+
var available *librariesindex.Release
73+
if updatable {
74+
available = lm.Index.FindLibraryUpdate(lib)
75+
if available == nil {
76+
continue
77+
}
78+
}
79+
res.Libraries = append(res.Libraries, &output.InstalledLibary{
80+
Library: lib,
81+
Available: available,
82+
})
83+
}
84+
}
85+
return res
86+
}

cli/lib/search.go

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* This file is part of arduino-cli.
3+
*
4+
* Copyright 2018 ARDUINO SA (http://www.arduino.cc/)
5+
*
6+
* This software is released under the GNU General Public License version 3,
7+
* which covers the main part of arduino-cli.
8+
* The terms of this license can be found at:
9+
* https://www.gnu.org/licenses/gpl-3.0.en.html
10+
*
11+
* You can be released from the requirements of the above licenses by purchasing
12+
* a commercial license. Buying such a license is mandatory if you want to modify or
13+
* otherwise use the software for commercial activities involving the Arduino
14+
* software without disclosing the source code of your own applications. To purchase
15+
* a commercial license, send an email to license@arduino.cc.
16+
*/
17+
18+
package lib
19+
20+
import (
21+
"fmt"
22+
"strings"
23+
24+
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
25+
"github.com/arduino/arduino-cli/cli"
26+
"github.com/arduino/arduino-cli/common/formatter"
27+
"github.com/arduino/arduino-cli/common/formatter/output"
28+
"github.com/sirupsen/logrus"
29+
"github.com/spf13/cobra"
30+
)
31+
32+
func initSearchCommand() *cobra.Command {
33+
searchCommand := &cobra.Command{
34+
Use: "search [LIBRARY_NAME]",
35+
Short: "Searchs for one or more libraries data.",
36+
Long: "Search for one or more libraries data (case insensitive search).",
37+
Example: " " + cli.AppName + " lib search audio",
38+
Args: cobra.ArbitraryArgs,
39+
Run: runSearchCommand,
40+
}
41+
searchCommand.Flags().BoolVar(&searchFlags.names, "names", false, "Show library names only.")
42+
return searchCommand
43+
}
44+
45+
var searchFlags struct {
46+
names bool // if true outputs lib names only.
47+
}
48+
49+
func runSearchCommand(cmd *cobra.Command, args []string) {
50+
logrus.Info("Executing `arduino lib search`")
51+
query := strings.ToLower(strings.Join(args, " "))
52+
53+
lm := cli.InitLibraryManager(cli.Config)
54+
55+
res := output.LibSearchResults{
56+
Libraries: []*librariesindex.Library{},
57+
}
58+
for _, lib := range lm.Index.Libraries {
59+
if strings.Contains(strings.ToLower(lib.Name), query) {
60+
res.Libraries = append(res.Libraries, lib)
61+
}
62+
}
63+
64+
if searchFlags.names {
65+
for _, lib := range res.Libraries {
66+
formatter.Print(lib.Name)
67+
}
68+
} else {
69+
if len(res.Libraries) == 0 {
70+
formatter.Print(fmt.Sprintf("No library found matching `%s` search query", query))
71+
} else {
72+
formatter.Print(res)
73+
}
74+
}
75+
logrus.Info("Done")
76+
}

cli/lib/uninstall.go

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* This file is part of arduino-cli.
3+
*
4+
* Copyright 2018 ARDUINO SA (http://www.arduino.cc/)
5+
*
6+
* This software is released under the GNU General Public License version 3,
7+
* which covers the main part of arduino-cli.
8+
* The terms of this license can be found at:
9+
* https://www.gnu.org/licenses/gpl-3.0.en.html
10+
*
11+
* You can be released from the requirements of the above licenses by purchasing
12+
* a commercial license. Buying such a license is mandatory if you want to modify or
13+
* otherwise use the software for commercial activities involving the Arduino
14+
* software without disclosing the source code of your own applications. To purchase
15+
* a commercial license, send an email to license@arduino.cc.
16+
*/
17+
18+
package lib
19+
20+
import (
21+
"os"
22+
23+
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
24+
"github.com/arduino/arduino-cli/cli"
25+
"github.com/arduino/arduino-cli/common/formatter"
26+
"github.com/sirupsen/logrus"
27+
"github.com/spf13/cobra"
28+
)
29+
30+
func initUninstallCommand() *cobra.Command {
31+
uninstallCommand := &cobra.Command{
32+
Use: "uninstall LIBRARY_NAME(S)",
33+
Short: "Uninstalls one or more libraries.",
34+
Long: "Uninstalls one or more libraries.",
35+
Example: " " + cli.AppName + " lib uninstall AudioZero",
36+
Args: cobra.MinimumNArgs(1),
37+
Run: runUninstallCommand,
38+
}
39+
return uninstallCommand
40+
}
41+
42+
func runUninstallCommand(cmd *cobra.Command, args []string) {
43+
logrus.Info("Executing `arduino lib uninstall`")
44+
45+
lm := cli.InitLibraryManager(cli.Config)
46+
libRefs, err := librariesindex.ParseArgs(args)
47+
if err != nil {
48+
formatter.PrintError(err, "Arguments error")
49+
os.Exit(cli.ErrBadArgument)
50+
}
51+
for _, libRef := range libRefs {
52+
lib := lm.FindByReference(libRef)
53+
if lib == nil {
54+
formatter.PrintErrorMessage("Library not installed: " + libRef.String())
55+
os.Exit(cli.ErrGeneric)
56+
} else {
57+
formatter.Print("Uninstalling " + lib.String())
58+
lm.Uninstall(lib)
59+
}
60+
}
61+
62+
logrus.Info("Done")
63+
}

0 commit comments

Comments
 (0)