Skip to content

Commit af34582

Browse files
authored
Merge pull request #87 from fm-tibco/master
Update CLI plugin support
2 parents 22cf3e3 + b0c2e58 commit af34582

File tree

10 files changed

+368
-292
lines changed

10 files changed

+368
-292
lines changed

cmd/flogo/gen/version.go

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@
33
package main
44

55
import (
6-
"log"
7-
"os"
8-
"text/template"
9-
"time"
10-
116
"github.com/project-flogo/cli/util"
7+
"os"
128
)
139

1410
// This Go program is aimed at being called by go:generate from "cmd/flogo/main.go" to create a "currentversion.go" file
@@ -20,29 +16,12 @@ import (
2016
// Users getting the CLI with a classic "go get" command will still have the version retrieved from the directory
2117
// $GOPATH/src/github.com/project-flogo/cli
2218
func main() {
23-
currentVersion := util.GetVersion(false)
2419

25-
f, err := os.Create("./currentversion.go")
20+
_, currentVersion, _ := util.GetCLIInfo()
21+
wd, err := os.Getwd()
2622
if err != nil {
27-
log.Fatal(err)
2823
return
2924
}
30-
defer f.Close()
31-
32-
packageTemplate.Execute(f, struct {
33-
Timestamp time.Time
34-
Version string
35-
}{
36-
Timestamp: time.Now(),
37-
Version: currentVersion,
38-
})
39-
}
40-
41-
var packageTemplate = template.Must(template.New("").Parse(`// Code generated by go generate; DO NOT EDIT.
42-
// {{ .Timestamp }}
43-
package main
4425

45-
func init() {
46-
Version = "{{ .Version }}"
26+
util.CreateVersionFile(wd, currentVersion)
4727
}
48-
`))

cmd/flogo/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var Version string = ""
1313
//go:generate go run gen/version.go
1414
func main() {
1515
//Initialize the commands
16-
os.Setenv("GO111MODULE", "on")
16+
_ = os.Setenv("GO111MODULE", "on")
1717
commands.Initialize(Version)
1818
commands.Execute()
1919
}

commands/plugin.go

Lines changed: 15 additions & 227 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,10 @@
11
package commands
22

33
import (
4-
"errors"
54
"fmt"
6-
"go/parser"
7-
"go/printer"
8-
"go/token"
9-
"os"
10-
"os/exec"
11-
"path/filepath"
12-
"runtime"
13-
145
"github.com/project-flogo/cli/common"
15-
"github.com/project-flogo/cli/util"
166
"github.com/spf13/cobra"
17-
)
18-
19-
const (
20-
fileImportsGo = "imports.go"
21-
add = false
22-
remove = true
7+
"os"
238
)
249

2510
func init() {
@@ -30,12 +15,6 @@ func init() {
3015
rootCmd.AddCommand(pluginCmd)
3116
}
3217

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-
3918
var pluginCmd = &cobra.Command{
4019
Use: "plugin",
4120
Short: "manage CLI plugins",
@@ -52,40 +31,17 @@ var pluginInstallCmd = &cobra.Command{
5231
Args: cobra.ExactArgs(1),
5332
Run: func(cmd *cobra.Command, args []string) {
5433

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-
6334
pluginPkg := args[0]
6435

6536
fmt.Printf("Installing plugin: %s\n", pluginPkg)
6637

67-
added, err := updatePlugin(pluginPkg, add)
38+
err := UpdateCLI(pluginPkg, UpdateOptAdd)
6839
if err != nil {
6940
fmt.Fprintf(os.Stderr, "Error adding plugin: %v\n", err)
7041
os.Exit(1)
7142
}
7243

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)
8945
},
9046
}
9147

@@ -94,8 +50,9 @@ var pluginListCmd = &cobra.Command{
9450
Short: "list installed plugins",
9551
Long: "Lists installed CLI plugins",
9652
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)
9956
}
10057
},
10158
}
@@ -106,24 +63,18 @@ var pluginRemoveCmd = &cobra.Command{
10663
Long: "Remove installed CLI plugins",
10764
Args: cobra.ExactArgs(1),
10865
Run: func(cmd *cobra.Command, args []string) {
66+
10967
pluginPkg := args[0]
110-
removed, err := updatePlugin(pluginPkg, remove)
11168

69+
fmt.Printf("Removing plugin: %s\n", pluginPkg)
70+
71+
err := UpdateCLI(pluginPkg, UpdateOptRemove)
11272
if err != nil {
11373
fmt.Fprintf(os.Stderr, "Error adding plugin: %v\n", err)
11474
os.Exit(1)
11575
}
11676

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)
12778
},
12879
}
12980

@@ -134,179 +85,16 @@ var pluginUpdateCmd = &cobra.Command{
13485
Args: cobra.ExactArgs(1),
13586
Run: func(cmd *cobra.Command, args []string) {
13687

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]
14489

145-
plugin := args[0]
146-
fmt.Printf("Updating plugin: %s\n", plugin)
90+
fmt.Printf("Updating plugin: %s\n", pluginPkg)
14791

148-
err = util.ExecCmd(exec.Command("go", "get", "-u", plugin), cliCmdPath)
92+
err := UpdateCLI(pluginPkg, UpdateOptUpdate)
14993
if err != nil {
15094
fmt.Fprintf(os.Stderr, "Error updating plugin: %v\n", err)
15195
os.Exit(1)
15296
}
15397

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)
16099
},
161100
}
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

Comments
 (0)