-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun.go
105 lines (86 loc) · 2.68 KB
/
run.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
/*
* Copyright (c) 2023 Maple Wu <[email protected]>
* National Electronics and Computer Technology Center, Thailand
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
zcore "github.com/go-zing/gozz-core"
)
var (
run = &cobra.Command{
Use: "run",
Short: "run annotations analysis and use plugins to do awesome things",
Example: zcore.ExecName + ` run -p "api" [ -p "plugin:options" ...] ./`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if err := Run(args); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(2)
}
},
}
runPlugins = make([]string, 0)
)
func init() {
flags := run.Flags()
flags.StringArrayVarP(&runPlugins, "plugin", "p", nil, "plugins to run")
}
func Run(args []string) (err error) {
if err = loadPlugins(); err != nil {
return
}
// get analysis path absolute
filename, err := filepath.Abs(args[0])
if err != nil {
return errors.New("get annotation analysis path absolute error: " + err.Error())
}
// validate plugins
if len(runPlugins) == 0 {
return errors.New("invalid plugins list. use -p to specify plugins")
}
// parse plugin entity with key-value options
plugins := make(zcore.PluginEntities, 0, len(runPlugins))
registry := zcore.PluginRegistry()
for i, plugin := range runPlugins {
// split plugin name and options string
// options would add to each comments annotation options
// Example: name:option1=value1:option2=value2
commands := strings.Split(zcore.EscapeAnnotation(plugin), ":")
name := commands[0]
// get registry plugin entity
entity, ok := registry[name]
if !ok {
return fmt.Errorf(`unregistered plugin name: %s. use "%s list" to get registered plugins`, name, zcore.ExecName)
}
// append entities
plugins = append(plugins, zcore.PluginEntity{
Plugin: entity,
Options: make(map[string]string, len(commands)-1),
})
option := plugins[i].Options
// parse entity options
zcore.SplitKVSlice2Map(commands[1:], "=", option)
for k, v := range option {
option[k] = zcore.UnescapeAnnotation(v)
}
}
return plugins.Run(filename)
}