forked from intelsdi-x/snap
/
plugin.go
195 lines (171 loc) · 5.24 KB
/
plugin.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015 Intel Corporation
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 (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"text/tabwriter"
"time"
"github.com/codegangsta/cli"
)
func loadPlugin(ctx *cli.Context) error {
pAsc := ctx.String("plugin-asc")
var paths []string
if len(ctx.Args()) != 1 {
return newUsageError("Incorrect usage:", ctx)
}
paths = append(paths, ctx.Args().First())
if pAsc != "" {
if !strings.Contains(pAsc, ".asc") {
return newUsageError("Must be a .asc file for the -a flag", ctx)
}
paths = append(paths, pAsc)
}
r := pClient.LoadPlugin(paths)
if r.Err != nil {
if r.Err.Fields()["error"] != nil {
return fmt.Errorf("Error loading plugin:\n%v\n%v\n", r.Err.Error(), r.Err.Fields()["error"])
}
return fmt.Errorf("Error loading plugin:\n%v\n", r.Err.Error())
}
for _, p := range r.LoadedPlugins {
fmt.Println("Plugin loaded")
fmt.Printf("Name: %s\n", p.Name)
fmt.Printf("Version: %d\n", p.Version)
fmt.Printf("Type: %s\n", p.Type)
fmt.Printf("Signed: %v\n", p.Signed)
fmt.Printf("Loaded Time: %s\n\n", p.LoadedTime().Format(timeFormat))
}
return nil
}
func unloadPlugin(ctx *cli.Context) error {
pDetails := filepath.SplitList(ctx.Args().First())
var pType, pName string
var pVer int
var err error
if len(pDetails) == 3 {
pType = pDetails[0]
pName = pDetails[1]
pVer, err = strconv.Atoi(pDetails[2])
if err != nil {
return newUsageError("Can't convert version string to integer", ctx)
}
} else {
pType = ctx.String("plugin-type")
pName = ctx.String("plugin-name")
pVer = ctx.Int("plugin-version")
}
if pType == "" {
return newUsageError("Must provide plugin type", ctx)
}
if pName == "" {
return newUsageError("Must provide plugin name", ctx)
}
if pVer < 1 {
return newUsageError("Must provide plugin version", ctx)
}
r := pClient.UnloadPlugin(pType, pName, pVer)
if r.Err != nil {
return fmt.Errorf("Error unloading plugin:\n%v\n", r.Err.Error())
}
fmt.Println("Plugin unloaded")
fmt.Printf("Name: %s\n", r.Name)
fmt.Printf("Version: %d\n", r.Version)
fmt.Printf("Type: %s\n", r.Type)
return nil
}
func swapPlugins(ctx *cli.Context) error {
// plugin to load
pAsc := ctx.String("plugin-asc")
var paths []string
if len(ctx.Args()) < 1 || len(ctx.Args()) > 2 {
return newUsageError("Incorrect usage:", ctx)
}
paths = append(paths, ctx.Args().First())
if pAsc != "" {
if !strings.Contains(pAsc, ".asc") {
return newUsageError("Must be a .asc file for the -a flag", ctx)
}
paths = append(paths, pAsc)
}
// plugin to unload
var pDetails []string
var pType, pName string
var pVer int
var err error
if len(ctx.Args()) == 2 {
pDetails = filepath.SplitList(ctx.Args()[1])
if len(pDetails) == 3 {
pType = pDetails[0]
pName = pDetails[1]
pVer, err = strconv.Atoi(pDetails[2])
if err != nil {
return newUsageError("Can't convert version string to integer", ctx)
}
} else {
return newUsageError("Missing type, name, or version", ctx)
}
} else {
pType = ctx.String("plugin-type")
pName = ctx.String("plugin-name")
pVer = ctx.Int("plugin-version")
}
if pType == "" {
return newUsageError("Must provide plugin type", ctx)
}
if pName == "" {
return newUsageError("Must provide plugin name", ctx)
}
if pVer < 1 {
return newUsageError("Must provide plugin version", ctx)
}
r := pClient.SwapPlugin(paths, pType, pName, pVer)
if r.Err != nil {
return fmt.Errorf("Error swapping plugins:\n%v\n", r.Err.Error())
}
fmt.Println("Plugin loaded")
fmt.Printf("Name: %s\n", r.LoadedPlugin.Name)
fmt.Printf("Version: %d\n", r.LoadedPlugin.Version)
fmt.Printf("Type: %s\n", r.LoadedPlugin.Type)
fmt.Printf("Signed: %v\n", r.LoadedPlugin.Signed)
fmt.Printf("Loaded Time: %s\n\n", r.LoadedPlugin.LoadedTime().Format(timeFormat))
fmt.Println("\nPlugin unloaded")
fmt.Printf("Name: %s\n", r.UnloadedPlugin.Name)
fmt.Printf("Version: %d\n", r.UnloadedPlugin.Version)
fmt.Printf("Type: %s\n", r.UnloadedPlugin.Type)
return nil
}
func listPlugins(ctx *cli.Context) error {
plugins := pClient.GetPlugins(ctx.Bool("running"))
if plugins.Err != nil {
return fmt.Errorf("Error: %v\n", plugins.Err)
}
w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
if ctx.Bool("running") {
printFields(w, false, 0, "NAME", "HIT COUNT", "LAST HIT", "TYPE")
for _, rp := range plugins.AvailablePlugins {
printFields(w, false, 0, rp.Name, rp.HitCount, time.Unix(rp.LastHitTimestamp, 0).Format(timeFormat), rp.Type)
}
} else {
printFields(w, false, 0, "NAME", "VERSION", "TYPE", "SIGNED", "STATUS", "LOADED TIME")
for _, lp := range plugins.LoadedPlugins {
printFields(w, false, 0, lp.Name, lp.Version, lp.Type, lp.Signed, lp.Status, lp.LoadedTime().Format(timeFormat))
}
}
w.Flush()
return nil
}