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:
committed by
Ta-Ching Chen
parent
cdcc6fb3ab
commit
adceee1be0
+30
-1
@@ -17,6 +17,7 @@ limitations under the License.
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -69,6 +70,7 @@ func newCliApp() *cli.App {
|
|||||||
app.Flags = []cli.Flag{
|
app.Flags = []cli.Flag{
|
||||||
cli.StringFlag{Name: "server", Value: "", Usage: "Fission server URL"},
|
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.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
|
// all resource create commands accept --spec
|
||||||
@@ -322,10 +324,34 @@ func newCliApp() *cli.App {
|
|||||||
}
|
}
|
||||||
|
|
||||||
app.Before = cliHook
|
app.Before = cliHook
|
||||||
app.CommandNotFound = handleCommandNotFound
|
app.Action = handleNoCommand
|
||||||
return app
|
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) {
|
func handleCommandNotFound(ctx *cli.Context, subCommand string) {
|
||||||
pmd, err := plugin.Find(subCommand)
|
pmd, err := plugin.Find(subCommand)
|
||||||
if err != nil {
|
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)
|
// Rebuild global arguments string (urfave/cli does not have an option to get the raw input of the global flags)
|
||||||
var globalArgs []string
|
var globalArgs []string
|
||||||
for _, globalFlagName := range ctx.GlobalFlagNames() {
|
for _, globalFlagName := range ctx.GlobalFlagNames() {
|
||||||
|
if globalFlagName == "plugin" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
val := fmt.Sprintf("%v", ctx.GlobalGeneric(globalFlagName))
|
val := fmt.Sprintf("%v", ctx.GlobalGeneric(globalFlagName))
|
||||||
if len(val) > 0 {
|
if len(val) > 0 {
|
||||||
globalArgs = append(globalArgs, fmt.Sprintf("--%v", globalFlagName), val)
|
globalArgs = append(globalArgs, fmt.Sprintf("--%v", globalFlagName), val)
|
||||||
|
|||||||
@@ -163,11 +163,14 @@ func fetchPluginMetadata(pluginPath string) (*Metadata, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse metadata if possible
|
// Parse metadata if possible
|
||||||
pluginName := strings.TrimPrefix(path.Base(pluginPath), Prefix)
|
pluginName := strings.TrimPrefix(path.Base(pluginPath), Prefix)
|
||||||
md := &Metadata{}
|
md := &Metadata{}
|
||||||
err = json.Unmarshal(buf.Bytes(), md)
|
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.Name = pluginName
|
||||||
}
|
}
|
||||||
md.Path = pluginPath
|
md.Path = pluginPath
|
||||||
|
|||||||
Reference in New Issue
Block a user