-
-
Notifications
You must be signed in to change notification settings - Fork 400
/
Copy pathinstall.go
151 lines (140 loc) · 6.59 KB
/
install.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// This file is part of arduino-cli.
//
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to license@arduino.cc.
package lib
import (
"context"
"fmt"
"os"
"strings"
"github.com/arduino/arduino-cli/commands"
"github.com/arduino/arduino-cli/internal/cli/arguments"
"github.com/arduino/arduino-cli/internal/cli/feedback"
"github.com/arduino/arduino-cli/internal/cli/instance"
"github.com/arduino/arduino-cli/internal/i18n"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/arduino/arduino-cli/version"
"github.com/arduino/go-paths-helper"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
semver "go.bug.st/relaxed-semver"
)
func initInstallCommand(srv rpc.ArduinoCoreServiceServer, settings *rpc.Configuration) *cobra.Command {
var noDeps bool
var noOverwrite bool
var gitURL bool
var zipPath bool
var useBuiltinLibrariesDir bool
enableUnsafeInstall := settings.GetLibrary().GetEnableUnsafeInstall()
installCommand := &cobra.Command{
Use: fmt.Sprintf("install %s[@%s]...", i18n.Tr("LIBRARY"), i18n.Tr("VERSION_NUMBER")),
Short: i18n.Tr("Installs one or more specified libraries into the system."),
Long: i18n.Tr("Installs one or more specified libraries into the system."),
Example: "" +
" " + os.Args[0] + " lib install AudioZero # " + i18n.Tr("for the latest version.") + "\n" +
" " + os.Args[0] + " lib install AudioZero@1.0.0 # " + i18n.Tr("for the specific version.") + "\n" +
" " + os.Args[0] + " lib install --git-url https://github.com/arduino-libraries/WiFi101.git https://github.com/arduino-libraries/ArduinoBLE.git\n" +
" " + os.Args[0] + " lib install --git-url https://github.com/arduino-libraries/WiFi101.git#0.16.0 # " + i18n.Tr("for the specific version.") + "\n" +
" " + os.Args[0] + " lib install --zip-path /path/to/WiFi101.zip /path/to/ArduinoBLE.zip\n",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
runInstallCommand(cmd.Context(), srv, args, noDeps, noOverwrite, gitURL, zipPath, useBuiltinLibrariesDir, enableUnsafeInstall)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return arguments.GetInstallableLibs(cmd.Context(), srv), cobra.ShellCompDirectiveDefault
},
}
installCommand.Flags().BoolVar(&noDeps, "no-deps", false, i18n.Tr("Do not install dependencies."))
installCommand.Flags().BoolVar(&noOverwrite, "no-overwrite", false, i18n.Tr("Do not overwrite already installed libraries."))
installCommand.Flags().BoolVar(&gitURL, "git-url", false, i18n.Tr("Enter git url for libraries hosted on repositories"))
installCommand.Flags().BoolVar(&zipPath, "zip-path", false, i18n.Tr("Enter a path to zip file"))
installCommand.Flags().BoolVar(&useBuiltinLibrariesDir, "install-in-builtin-dir", false, i18n.Tr("Install libraries in the IDE-Builtin directory"))
return installCommand
}
func runInstallCommand(ctx context.Context, srv rpc.ArduinoCoreServiceServer, args []string, noDeps bool, noOverwrite bool, gitURL bool, zipPath bool, useBuiltinLibrariesDir bool, enableUnsafeInstall bool) {
instance := instance.CreateAndInit(ctx, srv)
logrus.Info("Executing `arduino-cli lib install`")
if zipPath || gitURL {
if !enableUnsafeInstall {
documentationURL := "https://arduino.github.io/arduino-cli/latest/configuration/#configuration-keys"
_, err := semver.Parse(version.VersionInfo.VersionString)
if err == nil {
split := strings.Split(version.VersionInfo.VersionString, ".")
documentationURL = fmt.Sprintf("https://arduino.github.io/arduino-cli/%s.%s/configuration/#configuration-keys", split[0], split[1])
}
feedback.Fatal(i18n.Tr("--git-url and --zip-path are disabled by default, for more information see: %v", documentationURL), feedback.ErrGeneric)
}
feedback.Print(i18n.Tr("--git-url and --zip-path flags allow installing untrusted files, use it at your own risk."))
if useBuiltinLibrariesDir {
feedback.Fatal(i18n.Tr("--git-url or --zip-path can't be used with --install-in-builtin-dir"), feedback.ErrGeneric)
}
}
if zipPath {
for _, path := range args {
req := &rpc.ZipLibraryInstallRequest{
Instance: instance,
Path: path,
Overwrite: !noOverwrite,
}
stream := commands.ZipLibraryInstallStreamResponseToCallbackFunction(ctx, feedback.TaskProgress())
if err := srv.ZipLibraryInstall(req, stream); err != nil {
feedback.Fatal(i18n.Tr("Error installing Zip Library: %v", err), feedback.ErrGeneric)
}
}
return
}
if gitURL {
for _, url := range args {
if url == "." {
wd, err := paths.Getwd()
if err != nil {
feedback.Fatal(i18n.Tr("Couldn't get current working directory: %v", err), feedback.ErrGeneric)
}
url = wd.String()
}
req := &rpc.GitLibraryInstallRequest{
Instance: instance,
Url: url,
Overwrite: !noOverwrite,
}
stream := commands.GitLibraryInstallStreamResponseToCallbackFunction(ctx, feedback.TaskProgress())
if err := srv.GitLibraryInstall(req, stream); err != nil {
feedback.Fatal(i18n.Tr("Error installing Git Library: %v", err), feedback.ErrGeneric)
}
}
return
}
libRefs, err := ParseLibraryReferenceArgsAndAdjustCase(ctx, srv, instance, args)
if err != nil {
feedback.Fatal(i18n.Tr("Arguments error: %v", err), feedback.ErrBadArgument)
}
for _, libRef := range libRefs {
installLocation := rpc.LibraryInstallLocation_LIBRARY_INSTALL_LOCATION_USER
if useBuiltinLibrariesDir {
installLocation = rpc.LibraryInstallLocation_LIBRARY_INSTALL_LOCATION_BUILTIN
}
libraryInstallRequest := &rpc.LibraryInstallRequest{
Instance: instance,
Name: libRef.Name,
Version: libRef.Version,
NoDeps: noDeps,
NoOverwrite: noOverwrite,
InstallLocation: installLocation,
}
stream := commands.LibraryInstallStreamResponseToCallbackFunction(ctx, feedback.ProgressBar(), feedback.TaskProgress())
if err := srv.LibraryInstall(libraryInstallRequest, stream); err != nil {
feedback.Fatal(i18n.Tr("Error installing %s: %v", libRef.Name, err), feedback.ErrGeneric)
}
}
}