posh-providers/jlesquembre/kubeprompt/command.go
2023-05-25 10:03:43 +02:00

99 lines
2.8 KiB
Go

package kubeprompt
import (
"context"
"github.com/foomo/posh-providers/kubernets/kubectl"
"github.com/foomo/posh/pkg/command/tree"
"github.com/foomo/posh/pkg/log"
"github.com/foomo/posh/pkg/prompt/goprompt"
"github.com/foomo/posh/pkg/readline"
"github.com/foomo/posh/pkg/shell"
"github.com/foomo/posh/pkg/util/suggests"
)
type Command struct {
l log.Logger
kubectl *kubectl.Kubectl
commandTree tree.Root
}
// ------------------------------------------------------------------------------------------------
// ~ Constructor
// ------------------------------------------------------------------------------------------------
func NewCommand(l log.Logger, kubectl *kubectl.Kubectl) *Command {
inst := &Command{
l: l.Named("kubeprompt"),
kubectl: kubectl,
}
inst.commandTree = tree.New(&tree.Node{
Name: "kubeprompt",
Description: "Open the kubectl prompt",
Args: tree.Args{
{
Name: "cluster",
Suggest: inst.completeClusters,
},
},
Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error {
if r.Args().HasIndex(0) {
fs.Internal().String("profile", "", "Profile to use.")
if err := fs.Internal().SetValues("profile", inst.kubectl.Cluster(r.Args().At(0)).Profiles(ctx)...); err != nil {
return err
}
}
return nil
},
Execute: inst.execute,
})
return inst
}
// ------------------------------------------------------------------------------------------------
// ~ Public methods
// ------------------------------------------------------------------------------------------------
func (c *Command) Name() string {
return c.commandTree.Node().Name
}
func (c *Command) Description() string {
return c.commandTree.Node().Description
}
func (c *Command) Complete(ctx context.Context, r *readline.Readline) []goprompt.Suggest {
return c.commandTree.Complete(ctx, r)
}
func (c *Command) Execute(ctx context.Context, r *readline.Readline) error {
return c.commandTree.Execute(ctx, r)
}
func (c *Command) Help(ctx context.Context, r *readline.Readline) string {
return c.commandTree.Help(ctx, r)
}
// ------------------------------------------------------------------------------------------------
// ~ Private methods
// ------------------------------------------------------------------------------------------------
func (c *Command) execute(ctx context.Context, r *readline.Readline) error {
ifs := r.FlagSets().Internal()
profile, err := ifs.GetString("profile")
if err != nil {
return err
}
return shell.New(ctx, c.l, "kube-prompt").
Args(r.AdditionalArgs()...).
Env(c.kubectl.Cluster(r.Args().At(0)).Env(profile)).
Run()
}
func (c *Command) completeClusters(ctx context.Context, t tree.Root, r *readline.Readline) []goprompt.Suggest {
return suggests.List(c.kubectl.Clusters())
}