Support --plugin parameter in Fission CLI (#1111)

* Added support for --plugin parameter in Fission CLI

This fixes the issue that renaming fission  to fission-XYZ would cause a fork bomb, due to the CLI recursively calling itself to discover plugins.
This commit is contained in:
Erwin van Eyk
2019-03-20 00:15:29 +08:00
committed by Ta-Ching Chen
parent cdcc6fb3ab
commit adceee1be0
2 changed files with 34 additions and 2 deletions
+30 -1
View File
@@ -17,6 +17,7 @@ limitations under the License.
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
@@ -69,6 +70,7 @@ func newCliApp() *cli.App {
app.Flags = []cli.Flag{
cli.StringFlag{Name: "server", Value: "", Usage: "Fission server URL"},
cli.IntFlag{Name: "verbosity", Value: 1, Usage: "CLI verbosity (0 is quiet, 1 is the default, 2 is verbose.)"},
cli.BoolFlag{Name: "plugin", Hidden: true},
}
// all resource create commands accept --spec
@@ -322,10 +324,34 @@ func newCliApp() *cli.App {
}
app.Before = cliHook
app.CommandNotFound = handleCommandNotFound
app.Action = handleNoCommand
return app
}
func handleNoCommand(ctx *cli.Context) error {
if ctx.GlobalBool("version") {
versionPrinter(ctx)
return nil
}
if ctx.GlobalBool("plugin") {
bs, err := json.Marshal(plugin.Metadata{
Version: fission.Version,
Usage: ctx.App.Usage,
})
if err != nil {
log.Fatal(fmt.Sprintf("Failed to marshal plugin metadata to JSON: %v", err))
}
fmt.Println(string(bs))
return nil
}
if len(ctx.Args()) > 0 {
handleCommandNotFound(ctx, ctx.Args().First())
return nil
}
return cli.ShowAppHelp(ctx)
}
func handleCommandNotFound(ctx *cli.Context, subCommand string) {
pmd, err := plugin.Find(subCommand)
if err != nil {
@@ -351,6 +377,9 @@ To install it for your local Fission CLI:
// Rebuild global arguments string (urfave/cli does not have an option to get the raw input of the global flags)
var globalArgs []string
for _, globalFlagName := range ctx.GlobalFlagNames() {
if globalFlagName == "plugin" {
continue
}
val := fmt.Sprintf("%v", ctx.GlobalGeneric(globalFlagName))
if len(val) > 0 {
globalArgs = append(globalArgs, fmt.Sprintf("--%v", globalFlagName), val)
+4 -1
View File
@@ -163,11 +163,14 @@ func fetchPluginMetadata(pluginPath string) (*Metadata, error) {
if err != nil {
return nil, err
}
// Parse metadata if possible
pluginName := strings.TrimPrefix(path.Base(pluginPath), Prefix)
md := &Metadata{}
err = json.Unmarshal(buf.Bytes(), md)
if err != nil {
// If metadata could not be retrieved, or if no name was provided, use the filename of the binary
if err != nil || len(md.Name) == 0 {
md.Name = pluginName
}
md.Path = pluginPath