1
1
package commands
2
2
3
3
import (
4
- "errors"
5
4
"fmt"
6
- "go/parser"
7
- "go/printer"
8
- "go/token"
9
- "os"
10
- "os/exec"
11
- "path/filepath"
12
- "runtime"
13
-
14
5
"github.com/project-flogo/cli/common"
15
- "github.com/project-flogo/cli/util"
16
6
"github.com/spf13/cobra"
17
- )
18
-
19
- const (
20
- fileImportsGo = "imports.go"
21
- add = false
22
- remove = true
7
+ "os"
23
8
)
24
9
25
10
func init () {
@@ -30,12 +15,6 @@ func init() {
30
15
rootCmd .AddCommand (pluginCmd )
31
16
}
32
17
33
- var (
34
- goPath = os .Getenv ("GOPATH" )
35
- cliPath = filepath .Join (goPath , filepath .Join ("src" , "github.com" , "project-flogo" , "cli" ))
36
- cliCmdPath = filepath .Join (cliPath , "cmd" , "flogo" )
37
- )
38
-
39
18
var pluginCmd = & cobra.Command {
40
19
Use : "plugin" ,
41
20
Short : "manage CLI plugins" ,
@@ -52,40 +31,17 @@ var pluginInstallCmd = &cobra.Command{
52
31
Args : cobra .ExactArgs (1 ),
53
32
Run : func (cmd * cobra.Command , args []string ) {
54
33
55
- err := useBuildGoMod ()
56
- if err != nil {
57
- fmt .Fprintf (os .Stderr , "Error: %v\n " , err )
58
- os .Exit (1 )
59
- }
60
-
61
- defer restoreGoMod ()
62
-
63
34
pluginPkg := args [0 ]
64
35
65
36
fmt .Printf ("Installing plugin: %s\n " , pluginPkg )
66
37
67
- added , err := updatePlugin (pluginPkg , add )
38
+ err := UpdateCLI (pluginPkg , UpdateOptAdd )
68
39
if err != nil {
69
40
fmt .Fprintf (os .Stderr , "Error adding plugin: %v\n " , err )
70
41
os .Exit (1 )
71
42
}
72
43
73
- if added {
74
- err = updateCLI ()
75
-
76
- if err != nil {
77
- fmt .Fprintf (os .Stderr , "Error updating CLI: %v\n " , err )
78
- //remove plugin import on failure
79
-
80
- modifyPluginImports (pluginPkg , true )
81
-
82
- os .Exit (1 )
83
- }
84
-
85
- fmt .Printf ("Installed plugin\n " )
86
- } else {
87
- fmt .Printf ("Plugin '%s' already installed\n " , pluginPkg )
88
- }
44
+ fmt .Printf ("Installed plugin: %s\n " , pluginPkg )
89
45
},
90
46
}
91
47
@@ -94,8 +50,9 @@ var pluginListCmd = &cobra.Command{
94
50
Short : "list installed plugins" ,
95
51
Long : "Lists installed CLI plugins" ,
96
52
Run : func (cmd * cobra.Command , args []string ) {
97
- for _ , cmd := range common .GetPlugins () {
98
- fmt .Println (cmd .Name ())
53
+
54
+ for _ , pluginPkg := range common .GetPluginPkgs () {
55
+ fmt .Println (pluginPkg )
99
56
}
100
57
},
101
58
}
@@ -106,24 +63,18 @@ var pluginRemoveCmd = &cobra.Command{
106
63
Long : "Remove installed CLI plugins" ,
107
64
Args : cobra .ExactArgs (1 ),
108
65
Run : func (cmd * cobra.Command , args []string ) {
66
+
109
67
pluginPkg := args [0 ]
110
- removed , err := updatePlugin (pluginPkg , remove )
111
68
69
+ fmt .Printf ("Removing plugin: %s\n " , pluginPkg )
70
+
71
+ err := UpdateCLI (pluginPkg , UpdateOptRemove )
112
72
if err != nil {
113
73
fmt .Fprintf (os .Stderr , "Error adding plugin: %v\n " , err )
114
74
os .Exit (1 )
115
75
}
116
76
117
- if removed {
118
- err = updateCLI ()
119
- if err != nil {
120
- fmt .Fprintf (os .Stderr , "Error updating CLI: %v\n " , err )
121
- //remove plugin import on failure
122
- os .Exit (1 )
123
- }
124
- fmt .Printf ("Removed plugin %v \n " , pluginPkg )
125
- }
126
-
77
+ fmt .Printf ("Removed plugin: %s\n " , pluginPkg )
127
78
},
128
79
}
129
80
@@ -134,179 +85,16 @@ var pluginUpdateCmd = &cobra.Command{
134
85
Args : cobra .ExactArgs (1 ),
135
86
Run : func (cmd * cobra.Command , args []string ) {
136
87
137
- err := useBuildGoMod ()
138
- if err != nil {
139
- fmt .Fprintf (os .Stderr , "Error: %v\n " , err )
140
- os .Exit (1 )
141
- }
142
-
143
- defer restoreGoMod ()
88
+ pluginPkg := args [0 ]
144
89
145
- plugin := args [0 ]
146
- fmt .Printf ("Updating plugin: %s\n " , plugin )
90
+ fmt .Printf ("Updating plugin: %s\n " , pluginPkg )
147
91
148
- err = util . ExecCmd ( exec . Command ( "go" , "get" , "-u" , plugin ), cliCmdPath )
92
+ err := UpdateCLI ( pluginPkg , UpdateOptUpdate )
149
93
if err != nil {
150
94
fmt .Fprintf (os .Stderr , "Error updating plugin: %v\n " , err )
151
95
os .Exit (1 )
152
96
}
153
97
154
- err = updateCLI ()
155
- if err != nil {
156
- fmt .Fprintf (os .Stderr , "Error updating CLI: %v\n " , err )
157
- os .Exit (1 )
158
- }
159
- fmt .Printf ("Updated plugin\n " )
98
+ fmt .Printf ("Updated plugin: %s\n " , pluginPkg )
160
99
},
161
100
}
162
-
163
- func useBuildGoMod () error {
164
-
165
- baseGoMod := filepath .Join (cliPath , "go.mod" )
166
- bakGoMod := filepath .Join (cliPath , "go.mod.bak" )
167
- buildGoMod := filepath .Join (cliPath , "go.mod.build" )
168
-
169
- if _ , err := os .Stat (buildGoMod ); err != nil {
170
-
171
- if verbose {
172
- fmt .Printf ("Creating plugin build go.mod" )
173
- }
174
-
175
- err := util .CopyFile (baseGoMod , buildGoMod )
176
- if err != nil {
177
- return err
178
- }
179
- }
180
-
181
- if verbose {
182
- fmt .Printf ("Switching to plugin build go.mod" )
183
- }
184
-
185
- err := os .Rename (baseGoMod , bakGoMod )
186
- if err != nil {
187
- return err
188
- }
189
-
190
- err = os .Rename (buildGoMod , baseGoMod )
191
- if err != nil {
192
- return err
193
- }
194
-
195
- return nil
196
- }
197
-
198
- func restoreGoMod () {
199
-
200
- if verbose {
201
- fmt .Printf ("Restoring default CLI go.mod" )
202
- }
203
- baseGoMod := filepath .Join (cliPath , "go.mod" )
204
- bakGoMod := filepath .Join (cliPath , "go.mod.bak" )
205
- buildGoMod := filepath .Join (cliPath , "go.mod.build" )
206
-
207
- err := os .Rename (baseGoMod , buildGoMod )
208
- if err != nil {
209
- fmt .Fprintf (os .Stderr , "Error: %v\n " , err )
210
- }
211
- err = os .Rename (bakGoMod , baseGoMod )
212
- if err != nil {
213
- fmt .Fprintf (os .Stderr , "Error: %v\n " , err )
214
- }
215
- }
216
-
217
- func updatePlugin (pluginPkg string , opt bool ) (bool , error ) {
218
-
219
- err := util .ExecCmd (exec .Command ("go" , "get" , pluginPkg ), cliCmdPath )
220
- if err != nil {
221
- return false , err
222
- }
223
-
224
- added , err := modifyPluginImports (pluginPkg , opt )
225
- if err != nil {
226
- return added , err
227
- }
228
-
229
- if added {
230
- //Download all the modules. This is just to ensure all packages are downloaded before go build.
231
- err := util .ExecCmd (exec .Command ("go" , "mod" , "download" ), cliCmdPath )
232
- if err != nil {
233
- modifyPluginImports (pluginPkg , true )
234
- return false , err
235
- }
236
- }
237
-
238
- return added , nil
239
- }
240
-
241
- func updateCLI () error {
242
-
243
- exe , err := os .Executable ()
244
- if err != nil {
245
- return err
246
- }
247
-
248
- backupExe := exe + ".bak"
249
- if _ , err := os .Stat (exe ); err == nil {
250
- err = os .Rename (exe , backupExe )
251
- if err != nil {
252
- return err
253
- }
254
- }
255
-
256
- err = util .ExecCmd (exec .Command ("go" , "build" ), cliCmdPath )
257
- if err != nil {
258
- osErr := os .Rename (backupExe , exe )
259
- fmt .Fprintf (os .Stderr , "Error: %v\n " , osErr )
260
- return err
261
- }
262
- cliExe := "flogo"
263
- if runtime .GOOS == "windows" || os .Getenv ("GOOS" ) == "windows" {
264
- cliExe = cliExe + ".exe"
265
- }
266
-
267
- err = os .Rename (filepath .Join (cliCmdPath , cliExe ), exe )
268
- if err != nil {
269
- return err
270
- }
271
-
272
- err = os .Remove (backupExe )
273
- if err != nil {
274
- return err
275
- }
276
-
277
- return nil
278
- }
279
-
280
- func modifyPluginImports (pkg string , remove bool ) (bool , error ) {
281
-
282
- importsFile := filepath .Join (cliCmdPath , fileImportsGo )
283
-
284
- fset := token .NewFileSet ()
285
- file , _ := parser .ParseFile (fset , importsFile , nil , parser .ImportsOnly )
286
-
287
- if file .Imports == nil {
288
- return false , errors .New ("No Imports found." )
289
- }
290
-
291
- successful := false
292
-
293
- if remove {
294
-
295
- successful = util .DeleteImport (fset , file , pkg )
296
- } else {
297
- successful = util .AddImport (fset , file , pkg )
298
- }
299
-
300
- if successful {
301
- f , err := os .Create (importsFile )
302
- if err != nil {
303
- return false , err
304
- }
305
- defer f .Close ()
306
- if err := printer .Fprint (f , fset , file ); err != nil {
307
- return false , err
308
- }
309
- }
310
-
311
- return successful , nil
312
- }
0 commit comments