Files
2026-04-12 00:41:33 +08:00

213 lines
7.4 KiB
Go

package vecps
import (
"context"
"fmt"
"net/url"
"os"
"os/user"
"path/filepath"
"strings"
"github.com/fatih/color"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"code.byted.org/infcp/vecps-cli/cmd/vecps/cert"
"code.byted.org/infcp/vecps-cli/cmd/vecps/compose"
"code.byted.org/infcp/vecps-cli/cmd/vecps/es8"
"code.byted.org/infcp/vecps-cli/cmd/vecps/milvus"
"code.byted.org/infcp/vecps-cli/cmd/vecps/oldexpose"
"code.byted.org/infcp/vecps-cli/cmd/vecps/opensearch"
"code.byted.org/infcp/vecps-cli/cmd/vecps/operate"
"code.byted.org/infcp/vecps-cli/cmd/vecps/pack"
"code.byted.org/infcp/vecps-cli/cmd/vecps/sh"
"code.byted.org/infcp/vecps-cli/cmd/vecps/temporal"
"code.byted.org/infcp/vecps-cli/cmd/vecps/vinit"
"code.byted.org/infcp/vecps-cli/internal/addons"
"code.byted.org/infcp/vecps-cli/internal/option"
"code.byted.org/infcp/vecps-cli/pkg/version"
)
// Root root
var Root = New()
// New cobra command entry
func New() (root *cobra.Command) {
var verbose = false
var kubeconfig string
var namespace string
root = &cobra.Command{
Use: "vecps",
Short: "vecps is a tool for managing vecps platform",
RunE: func(cmd *cobra.Command, _ []string) error { return cmd.Usage() },
DisableAutoGenTag: true,
SilenceUsage: true,
SilenceErrors: true,
PersistentPreRunE: func(_ *cobra.Command, _ []string) (err error) {
var opt = option.Get()
if verbose {
logrus.SetLevel(logrus.DebugLevel)
logrus.SetFormatter(&logrus.TextFormatter{
DisableColors: true,
FullTimestamp: true,
ForceColors: true,
DisableQuote: true,
})
logrus.SetReportCaller(true)
opt.Verbose = verbose
}
opt.Namespace = namespace
if kubeconfig != "" {
opt.Kubeconfig = kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
return err
}
var clientset *kubernetes.Clientset
if clientset, err = kubernetes.NewForConfig(config); err != nil {
return err
}
opt.Client = clientset
opt.Config = config
} else if conf, err := config.GetConfig(); err == nil {
opt.Config = conf
if len(kubeconfig) != 0 {
opt.Kubeconfig = kubeconfig
}
if len(namespace) != 0 {
opt.Namespace = namespace
}
if len(opt.Kubeconfig) == 0 {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
if _, ok := os.LookupEnv("HOME"); !ok {
if u, err := user.Current(); err == nil {
loadingRules.Precedence = append(loadingRules.Precedence, filepath.Join(u.HomeDir, clientcmd.RecommendedHomeDir, clientcmd.RecommendedFileName))
}
}
if len(namespace) == 0 {
clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, nil)
opt.Namespace, _, _ = clientConfig.Namespace()
}
}
var u *url.URL
if u, err = url.Parse(opt.Config.Host); err != nil {
return err
}
var address = u.Host
if strings.Contains(u.Host, ":") {
address = strings.Split(u.Host, ":")[0]
}
opt.Address = address
var clientset *kubernetes.Clientset
if clientset, err = kubernetes.NewForConfig(conf); err != nil {
return err
}
opt.Client = clientset
}
if err = addons.InitialAddons(); err != nil {
return
}
return
},
}
var ctx = context.Background()
var commands = []*cobra.Command{
oldexpose.NewCmdExpose(ctx),
cert.NewCmdCert(ctx),
compose.NewCmdK9s(ctx),
compose.NewCmdNeofetch(ctx),
compose.NewCmdBtop(ctx),
vinit.NewCmdInit(ctx),
operate.NewCmdOperate(ctx),
sh.NewCmdSH(ctx),
sh.NewCmdNU(ctx),
sh.NewCmdFish(ctx),
NewCmdVersion(ctx),
pack.NewCmdPack(ctx),
pack.NewCmdFetch(ctx),
NewCmdVersion(ctx),
milvus.NewCmdMilvus(ctx),
es8.NewCmdEs8(ctx),
opensearch.NewCmdOpensearch(ctx),
temporal.NewCmdTemporal(ctx),
}
root.AddCommand(commands...)
root.SetUsageTemplate(usage)
root.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "enable debug logs")
root.PersistentFlags().StringVarP(&kubeconfig, "kubeconfig", "", "", "path to the kubeconfig file")
root.PersistentFlags().StringVarP(&namespace, "namespace", "n", "vke-system", "namespace setting")
return
}
const banner = `vvvvvvv vvvvvvv eeeeeeeeeeee ccccccccccccccccppppp ppppppppp ssssssssss
v:::::v v:::::vee::::::::::::ee cc:::::::::::::::cp::::ppp:::::::::p ss::::::::::s
v:::::v v:::::ve::::::eeeee:::::ee c:::::::::::::::::cp:::::::::::::::::p ss:::::::::::::s
v:::::v v:::::ve::::::e e:::::ec:::::::cccccc:::::cpp::::::ppppp::::::ps::::::ssss:::::s
v:::::v v:::::v e:::::::eeeee::::::ec::::::c ccccccc p:::::p p:::::p s:::::s ssssss
v:::::v v:::::v e:::::::::::::::::e c:::::c p:::::p p:::::p s::::::s
v:::::v:::::v e::::::eeeeeeeeeee c:::::c p:::::p p:::::p s::::::s
v:::::::::v e:::::::e c::::::c ccccccc p:::::p p::::::pssssss s:::::s
v:::::::v e::::::::e c:::::::cccccc:::::c p:::::ppppp:::::::ps:::::ssss::::::s
v:::::v e::::::::eeeeeeee c:::::::::::::::::c p::::::::::::::::p s::::::::::::::s
v:::v ee:::::::::::::e cc:::::::::::::::c p::::::::::::::pp s:::::::::::ss
vvv eeeeeeeeeeeeee cccccccccccccccc p::::::pppppppp sssssssssss
p:::::p
p:::::p
p:::::::p
p:::::::p
p:::::::p
ppppppppp`
func NewCmdVersion(ctx context.Context) *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Print the version number of vecps-cli",
Run: func(_ *cobra.Command, _ []string) {
color.Cyan(banner)
info := version.Get()
fmt.Printf("Version: %s\n", info.Version)
fmt.Printf("GoVersion: %s\n", info.GoVersion)
fmt.Printf("Platform: %s\n", info.Platform)
fmt.Printf("BuildDate: %s\n", info.BuildDate)
fmt.Printf("GitCommit: %s\n", info.GitCommit)
},
}
}
const usage = `Usage:{{if .Runnable}}
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}
Aliases:
{{.NameAndAliases}}{{end}}{{if .HasExample}}
Examples:
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}
Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}{{ $command := (printf "%s" .Name) }}
{{range $alias := .Aliases}}{{ $command = printf "%s, %s" $command $alias }}{{end}}{{ rpad $command 18 }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
Flags:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
Global Flags:
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
`