-
Notifications
You must be signed in to change notification settings - Fork 138
/
plugin.go
70 lines (54 loc) · 1.32 KB
/
plugin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package plugin
import (
"context"
"os"
"os/signal"
"runtime/debug"
"strings"
"syscall"
"knative.dev/client/pkg/plugin"
"knative.dev/func/cmd"
)
func init() {
plugin.InternalPlugins = append(plugin.InternalPlugins, &funcPlugin{})
}
type funcPlugin struct{}
func (f *funcPlugin) Name() string {
return "kn-func"
}
func (f *funcPlugin) Execute(args []string) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
cancel()
}()
version := cmd.Version{}
info, _ := debug.ReadBuildInfo()
for _, dep := range info.Deps {
if strings.Contains(dep.Path, "knative.dev/func") {
version.Vers = dep.Version
version.Hash = dep.Sum
}
}
rootCmd := cmd.NewRootCmd(cmd.RootCommandConfig{Name: "kn func", Version: version})
oldArgs := os.Args
defer (func() {
os.Args = oldArgs
})()
os.Args = append([]string{"kn-func"}, args...)
return rootCmd.ExecuteContext(ctx)
}
// Description for function subcommand visible in 'kn --help'
func (f *funcPlugin) Description() (string, error) {
return "Function plugin", nil
}
func (f *funcPlugin) CommandParts() []string {
return []string{"func"}
}
// Path is empty because its an internal plugin
func (f *funcPlugin) Path() string {
return ""
}