@@ -23,41 +23,40 @@ import (
23
23
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
24
24
"github.com/arduino/arduino-cli/commands"
25
25
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
26
- "google.golang.org/grpc/codes"
27
- "google.golang.org/grpc/status"
28
26
)
29
27
30
28
// PlatformInstall FIXMEDOC
31
29
func PlatformInstall (ctx context.Context , req * rpc.PlatformInstallRequest ,
32
- downloadCB commands.DownloadProgressCB , taskCB commands.TaskProgressCB ) (* rpc.PlatformInstallResponse , * status. Status ) {
30
+ downloadCB commands.DownloadProgressCB , taskCB commands.TaskProgressCB ) (* rpc.PlatformInstallResponse , error ) {
33
31
34
32
pm := commands .GetPackageManager (req .GetInstance ().GetId ())
35
33
if pm == nil {
36
- return nil , status . New ( codes . InvalidArgument , tr ( "Invalid instance" ))
34
+ return nil , & commands. InvalidInstanceError {}
37
35
}
38
36
39
37
version , err := commands .ParseVersion (req )
40
38
if err != nil {
41
- return nil , status . Newf ( codes . InvalidArgument , tr ( "Invalid version: %s" ), err )
39
+ return nil , & commands. InvalidVersionError { Cause : err }
42
40
}
43
41
44
- platform , tools , err := pm . FindPlatformReleaseDependencies ( & packagemanager.PlatformReference {
42
+ ref := & packagemanager.PlatformReference {
45
43
Package : req .PlatformPackage ,
46
44
PlatformArchitecture : req .Architecture ,
47
45
PlatformVersion : version ,
48
- })
46
+ }
47
+ platform , tools , err := pm .FindPlatformReleaseDependencies (ref )
49
48
if err != nil {
50
- return nil , status . Newf ( codes . InvalidArgument , tr ( "Error finding platform dependencies: %s" ), err )
49
+ return nil , & commands. PlatformNotFound { Platform : ref . String ( ), Cause : err }
51
50
}
52
51
53
52
err = installPlatform (pm , platform , tools , downloadCB , taskCB , req .GetSkipPostInstall ())
54
53
if err != nil {
55
- return nil , status . Convert ( err )
54
+ return nil , err
56
55
}
57
56
58
57
status := commands .Init (& rpc.InitRequest {Instance : req .Instance }, nil )
59
58
if status != nil {
60
- return nil , status
59
+ return nil , status . Err ()
61
60
}
62
61
63
62
return & rpc.PlatformInstallResponse {}, nil
@@ -92,16 +91,14 @@ func installPlatform(pm *packagemanager.PackageManager,
92
91
return err
93
92
}
94
93
}
95
- err := downloadPlatform (pm , platformRelease , downloadCB )
96
- if err != nil {
94
+ if err := downloadPlatform (pm , platformRelease , downloadCB ); err != nil {
97
95
return err
98
96
}
99
97
taskCB (& rpc.TaskProgress {Completed : true })
100
98
101
99
// Install tools first
102
100
for _ , tool := range toolsToInstall {
103
- err := commands .InstallToolRelease (pm , tool , taskCB )
104
- if err != nil {
101
+ if err := commands .InstallToolRelease (pm , tool , taskCB ); err != nil {
105
102
return err
106
103
}
107
104
}
@@ -111,11 +108,11 @@ func installPlatform(pm *packagemanager.PackageManager,
111
108
if installed == nil {
112
109
// No version of this platform is installed
113
110
log .Info ("Installing platform" )
114
- taskCB (& rpc.TaskProgress {Name : fmt .Sprintf (tr ("Installing %s" ), platformRelease )})
111
+ taskCB (& rpc.TaskProgress {Name : fmt .Sprintf (tr ("Installing platform %s" ), platformRelease )})
115
112
} else {
116
113
// A platform with a different version is already installed
117
114
log .Info ("Upgrading platform " + installed .String ())
118
- taskCB (& rpc.TaskProgress {Name : fmt .Sprintf (tr ("Upgrading %[1]s with %[2]s" ), installed , platformRelease )})
115
+ taskCB (& rpc.TaskProgress {Name : fmt .Sprintf (tr ("Upgrading platform %[1]s with %[2]s" ), installed , platformRelease )})
119
116
platformRef := & packagemanager.PlatformReference {
120
117
Package : platformRelease .Platform .Package .Name ,
121
118
PlatformArchitecture : platformRelease .Platform .Architecture ,
@@ -128,33 +125,32 @@ func installPlatform(pm *packagemanager.PackageManager,
128
125
var err error
129
126
_ , installedTools , err = pm .FindPlatformReleaseDependencies (platformRef )
130
127
if err != nil {
131
- return fmt . Errorf ( tr ("can 't find dependencies for platform %[1]s: %[2]w" ) , platformRef , err )
128
+ return & commands. NotFoundError { Message : tr ("Can 't find dependencies for platform %s" , platformRef ), Cause : err }
132
129
}
133
130
}
134
131
135
132
// Install
136
- err = pm .InstallPlatform (platformRelease )
137
- if err != nil {
133
+ if err := pm .InstallPlatform (platformRelease ); err != nil {
138
134
log .WithError (err ).Error ("Cannot install platform" )
139
- return err
135
+ return & commands. FailedInstallError { Message : tr ( "Cannot install platform" ), Cause : err }
140
136
}
141
137
142
138
// If upgrading remove previous release
143
139
if installed != nil {
144
- errUn := pm .UninstallPlatform (installed )
140
+ uninstallErr := pm .UninstallPlatform (installed )
145
141
146
142
// In case of error try to rollback
147
- if errUn != nil {
148
- log .WithError (errUn ).Error ("Error upgrading platform." )
149
- taskCB (& rpc.TaskProgress {Message : fmt .Sprintf (tr ("Error upgrading platform: %s" ), errUn )})
143
+ if uninstallErr != nil {
144
+ log .WithError (uninstallErr ).Error ("Error upgrading platform." )
145
+ taskCB (& rpc.TaskProgress {Message : fmt .Sprintf (tr ("Error upgrading platform: %s" ), uninstallErr )})
150
146
151
147
// Rollback
152
148
if err := pm .UninstallPlatform (platformRelease ); err != nil {
153
149
log .WithError (err ).Error ("Error rolling-back changes." )
154
150
taskCB (& rpc.TaskProgress {Message : fmt .Sprintf (tr ("Error rolling-back changes: %s" ), err )})
155
151
}
156
152
157
- return fmt . Errorf ( tr ("upgrading platform: %s " ), errUn )
153
+ return & commands. FailedInstallError { Message : tr ("Cannot upgrade platform " ), Cause : uninstallErr }
158
154
}
159
155
160
156
// Uninstall unused tools
@@ -169,16 +165,16 @@ func installPlatform(pm *packagemanager.PackageManager,
169
165
// Perform post install
170
166
if ! skipPostInstall {
171
167
log .Info ("Running post_install script" )
172
- taskCB (& rpc.TaskProgress {Message : tr ("Configuring platform" )})
168
+ taskCB (& rpc.TaskProgress {Message : tr ("Configuring platform. " )})
173
169
if err := pm .RunPostInstallScript (platformRelease ); err != nil {
174
- taskCB (& rpc.TaskProgress {Message : fmt .Sprintf (tr ("WARNING: cannot run post install : %s" ), err )})
170
+ taskCB (& rpc.TaskProgress {Message : fmt .Sprintf (tr ("WARNING cannot configure platform : %s" ), err )})
175
171
}
176
172
} else {
177
- log .Info ("Skipping platform configuration (post_install run) ." )
178
- taskCB (& rpc.TaskProgress {Message : tr ("Skipping platform configuration" )})
173
+ log .Info ("Skipping platform configuration." )
174
+ taskCB (& rpc.TaskProgress {Message : tr ("Skipping platform configuration. " )})
179
175
}
180
176
181
177
log .Info ("Platform installed" )
182
- taskCB (& rpc.TaskProgress {Message : fmt .Sprintf (tr ("%s installed" ), platformRelease ), Completed : true })
178
+ taskCB (& rpc.TaskProgress {Message : fmt .Sprintf (tr ("Platform %s installed" ), platformRelease ), Completed : true })
183
179
return nil
184
180
}
0 commit comments