feat(kn): Enable faas to be integrated as plugin to kn (#155)

Provides capabilities for the faas CLI to exist as a plugin in the upstream kn CLI.

Tested with kn 0.17
This commit is contained in:
David Simansky 2020-10-08 16:14:10 +02:00 committed by GitHub
parent 2c7c18dd9b
commit 85a5f475eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 1 deletions

View File

@ -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()

42
plugin/plugin.go Normal file
View File

@ -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 ""
}