This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubernetes.go
68 lines (58 loc) · 1.75 KB
/
kubernetes.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
package resource
import (
"context"
"io"
"strings"
"github.com/spf13/cobra"
"github.com/trivigy/migrate/v2/global"
"github.com/trivigy/migrate/v2/require"
"github.com/trivigy/migrate/v2/types"
)
// Kubernetes represents a kubernetes root command.
type Kubernetes map[string]types.Resource
var _ interface {
types.Resource
types.Command
} = new(Kubernetes)
// NewCommand returns a new cobra.Command object.
func (r Kubernetes) NewCommand(ctx context.Context, name string) *cobra.Command {
cmd := &cobra.Command{
Use: name[strings.LastIndex(name, ".")+1:] + " COMMAND",
Short: "Kubernetes cluster release and deployment controller.",
Long: "Kubernetes cluster release and deployment controller",
Args: require.Args(r.validation),
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
SilenceErrors: true,
SilenceUsage: true,
}
cmd.SetUsageTemplate(global.DefaultUsageTemplate)
cmd.SetHelpCommand(&cobra.Command{Hidden: true})
for key, resource := range r {
cmd.AddCommand(resource.NewCommand(ctx, name+"."+key))
}
flags := cmd.Flags()
flags.SortFlags = false
flags.Bool("help", false, "Show help information.")
return cmd
}
// Execute runs the command.
func (r Kubernetes) Execute(name string, out io.Writer, args []string) error {
wrap := types.Executor{Name: name, Command: r}
ctx := context.WithValue(context.Background(), global.RefRoot, wrap)
cmd := r.NewCommand(ctx, name)
cmd.SetOut(out)
cmd.SetArgs(args)
if err := cmd.Execute(); err != nil {
return err
}
return nil
}
// validation represents a sequence of positional argument validation steps.
func (r Kubernetes) validation(cmd *cobra.Command, args []string) error {
if err := require.ExactArgs(args, 1); err != nil {
return err
}
return nil
}