-
Notifications
You must be signed in to change notification settings - Fork 10
/
plugin.go
111 lines (91 loc) · 2.89 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* Copyright (C) 2022 Appvia Ltd <info@appvia.io>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package kubectl
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"github.com/spf13/cobra"
"github.com/appvia/terranetes-controller/pkg/cmd"
"github.com/appvia/terranetes-controller/pkg/utils/template"
)
var longPluginHelp = `
This command is used to integrate the tnctl command as a kubectl
plugin. It effectively generates a series of shortcuts that are
called from kubectl. You need to ensure the scripts this command
generates are included your $PATH, long with the location of the
tnctl command.
Create the kubectl plugins (defaults to ${HOME}/bin)
$ tnctl kubectl plugin
Place the plugins scripts in another directory
$ tnctl kubectl plugin -d ${GOPATH}/bin
`
var pluginTemplate = `#!/bin/env sh
tnctl {{ .Command }} $@
`
// PluginCommand returns the cobra command for the "build" sub-command.
type PluginCommand struct {
cmd.Factory
// Directory is the location to place the kubectl plugins
Directory string
}
// NewPluginCommand returns a new instance of the get command
func NewPluginCommand(factory cmd.Factory) *cobra.Command {
o := &PluginCommand{Factory: factory}
c := &cobra.Command{
Use: "plugin [OPTIONS]",
Short: "Generates the kubectl plugin integration",
Long: longPluginHelp,
RunE: func(cmd *cobra.Command, args []string) error {
return o.Run(cmd.Context())
},
}
flags := c.Flags()
flags.StringVarP(&o.Directory, "directory", "d", filepath.Join(os.Getenv("HOME"), "bin"), "Directory to place the kubectl plugins shortcuts")
return c
}
// Run implements the command action
func (o *PluginCommand) Run(ctx context.Context) error {
if o.Directory == "" {
return errors.New("no directory specified for kubectl plugins")
}
commands := []string{
"approve",
"config",
"describe",
"logs",
"search",
}
// @step: ensure the directory exists
if err := os.MkdirAll(o.Directory, 0755); err != nil {
return err
}
for _, name := range commands {
path := filepath.Join(o.Directory, fmt.Sprintf("kubectl-tnctl-%s", name))
content, err := template.New(pluginTemplate, map[string]interface{}{"Command": name})
if err != nil {
return err
}
//nolint:gosec
if err := os.WriteFile(path, []byte(content), 0755); err != nil {
return err
}
}
return nil
}