From df1819b4585461e1692eb8863c6306f06dd319c3 Mon Sep 17 00:00:00 2001 From: David Simansky Date: Tue, 6 Oct 2020 19:39:25 +0200 Subject: [PATCH] feat(kn): Enable faas to be integrated as plugin to kn --- cmd/root.go | 7 ++++++- plugin/plugin.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 plugin/plugin.go diff --git a/cmd/root.go b/cmd/root.go index da221e4cf0..55ef4999f6 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -26,10 +26,15 @@ var root = &cobra.Command{ Create and run Functions as a Service.`, } +// NewRootCmd is used to initialize faas as kn plugin +func NewRootCmd() *cobra.Command { + return root +} + // When the code is loaded into memory upon invocation, the cobra/viper packages // are invoked to gather system context. This includes reading the configuration // file, environment variables, and parsing the command flags. -func init() { +func Init() { // read in environment variables that match viper.AutomaticEnv() diff --git a/plugin/plugin.go b/plugin/plugin.go new file mode 100644 index 0000000000..9dee48c47f --- /dev/null +++ b/plugin/plugin.go @@ -0,0 +1,42 @@ +package plugin + +import ( + "github.com/boson-project/faas/cmd" + "knative.dev/client/pkg/kn/plugin" + "os" +) + +func init() { + plugin.InternalPlugins = append(plugin.InternalPlugins, &faasPlugin{}) +} + +type faasPlugin struct {} + +func (f *faasPlugin) Name() string { + return "kn-faas" +} + +func (f *faasPlugin) Execute(args []string) error { + rootCmd := cmd.NewRootCmd() + cmd.Init() + oldArgs := os.Args + defer (func() { + os.Args = oldArgs + })() + os.Args = append([]string { "kn-faas" }, args...) + return rootCmd.Execute() +} + +// Description for faas subcommand visible in 'kn --help' +func (f *faasPlugin) Description() (string, error) { + return "Function as a Service plugin", nil +} + +func (f *faasPlugin) CommandParts() []string { + return []string{ "faas"} +} + +// Path is empty because its an internal plugins +func (f *faasPlugin) Path() string { + return "" +}