-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.go
77 lines (68 loc) · 1.85 KB
/
install.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
71
72
73
74
75
76
77
package main
import (
"bytes"
"os"
"path/filepath"
"strings"
"github.com/appscode/go/ioutil"
"github.com/appscode/go/log"
"github.com/ghodss/yaml"
"github.com/kardianos/osext"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/client-go/util/homedir"
"k8s.io/kubernetes/pkg/kubectl/plugins"
)
func NewCmdInstall(rootCmd *cobra.Command) *cobra.Command {
cmd := &cobra.Command{
Use: "install",
Short: "Install as kubectl plugin",
DisableAutoGenTag: true,
Run: func(cmd *cobra.Command, args []string) {
dir := filepath.Join(homedir.HomeDir(), ".kube", "plugins", rootCmd.Name())
os.MkdirAll(dir, 0755)
p, err := osext.Executable()
if err != nil {
log.Fatal(err)
}
p = filepath.Clean(p)
ioutil.CopyFile(filepath.Join(dir, filepath.Base(p)), p, 0755)
var traverse func(cmd *cobra.Command, p *plugins.Plugin)
traverse = func(cmd *cobra.Command, p *plugins.Plugin) {
p.Name = cmd.Name()
p.ShortDesc = cmd.Short
p.LongDesc = cmd.Long
p.Example = cmd.Example
if len(cmd.Commands()) == 0 {
p.Command = "./" + strings.TrimSpace(cmd.CommandPath())
fn := func(flag *pflag.Flag) {
if flag.Hidden {
return
}
p.Flags = append(p.Flags, plugins.Flag{
Name: flag.Name,
Shorthand: flag.Shorthand,
Desc: flag.Usage,
DefValue: flag.DefValue,
})
}
cmd.NonInheritedFlags().VisitAll(fn)
cmd.InheritedFlags().VisitAll(fn)
}
for _, cc := range cmd.Commands() {
cp := &plugins.Plugin{}
traverse(cc, cp)
p.Tree = append(p.Tree, cp)
}
}
plugin := &plugins.Plugin{}
traverse(rootCmd, plugin)
data, err := yaml.Marshal(plugin)
if err != nil {
log.Fatal(err)
}
ioutil.WriteFile(filepath.Join(dir, "plugin.yaml"), bytes.NewBuffer(data), 0755)
},
}
return cmd
}