Skip to content

Commit 85a5f47

Browse files
authored
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
1 parent 2c7c18d commit 85a5f47

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

cmd/root.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,15 @@ var root = &cobra.Command{
2626
Create and run Functions as a Service.`,
2727
}
2828

29+
// NewRootCmd is used to initialize faas as kn plugin
30+
func NewRootCmd() *cobra.Command {
31+
return root
32+
}
33+
2934
// When the code is loaded into memory upon invocation, the cobra/viper packages
3035
// are invoked to gather system context. This includes reading the configuration
3136
// file, environment variables, and parsing the command flags.
32-
func init() {
37+
func Init() {
3338
// read in environment variables that match
3439
viper.AutomaticEnv()
3540

plugin/plugin.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package plugin
2+
3+
import (
4+
"github.com/boson-project/faas/cmd"
5+
"knative.dev/client/pkg/kn/plugin"
6+
"os"
7+
)
8+
9+
func init() {
10+
plugin.InternalPlugins = append(plugin.InternalPlugins, &faasPlugin{})
11+
}
12+
13+
type faasPlugin struct {}
14+
15+
func (f *faasPlugin) Name() string {
16+
return "kn-faas"
17+
}
18+
19+
func (f *faasPlugin) Execute(args []string) error {
20+
rootCmd := cmd.NewRootCmd()
21+
cmd.Init()
22+
oldArgs := os.Args
23+
defer (func() {
24+
os.Args = oldArgs
25+
})()
26+
os.Args = append([]string { "kn-faas" }, args...)
27+
return rootCmd.Execute()
28+
}
29+
30+
// Description for faas subcommand visible in 'kn --help'
31+
func (f *faasPlugin) Description() (string, error) {
32+
return "Function as a Service plugin", nil
33+
}
34+
35+
func (f *faasPlugin) CommandParts() []string {
36+
return []string{ "faas"}
37+
}
38+
39+
// Path is empty because its an internal plugins
40+
func (f *faasPlugin) Path() string {
41+
return ""
42+
}

0 commit comments

Comments
 (0)