Merge Go frontend (apiserver + xcecli) into meson build

This commit is contained in:
Rongsong Shen 2026-04-14 11:20:17 +08:00
parent 43dc12a36c
commit c9dc8c4676
205 changed files with 30793 additions and 328 deletions

View file

@ -0,0 +1,116 @@
package cmd
import (
"fmt"
apiserver "xcecli/cmd/auth"
"xcecli/cmd/clusters"
"xcecli/cmd/hosts"
"xcecli/cmd/jobs"
"xcecli/cmd/permissions"
"xcecli/cmd/queues"
"xcecli/cmd/resources"
"xcecli/cmd/roles"
"xcecli/cmd/users"
"xcecli/common/request"
"xcecli/config"
"github.com/spf13/cobra"
)
var (
rootCmd = &cobra.Command{
Use: "xce",
Short: "XCE CLI tool for the API Server operations",
}
configManager *config.ConfigManager
)
// execute the root command
func Execute(cm *config.ConfigManager) error {
if cm == nil {
return fmt.Errorf("the config manager is not initialized")
}
configManager = cm
rootCmd.CompletionOptions.DisableDefaultCmd = true
rootCmd.SilenceErrors = true
rootCmd.SilenceUsage = true
// initialize the subcommands
initCommands()
return rootCmd.Execute()
}
// withAuth wraps a command with authentication check
// This function creates a middleware that checks authentication before executing any command
func withAuth(cmd *cobra.Command) *cobra.Command {
// Store original handlers
originalRunE := cmd.RunE
originalRun := cmd.Run
// Create new RunE that checks authentication first
cmd.RunE = func(cmd *cobra.Command, args []string) error {
// Check authentication before executing the command
_, err := configManager.IsAuthenticated()
if err != nil {
return err
}
// Execute the original command logic
if originalRunE != nil {
return originalRunE(cmd, args)
}
if originalRun != nil {
originalRun(cmd, args)
return nil
}
return nil
}
return cmd
}
// addCommandWithAuth adds a command and all its subcommands with authentication check
func addCommandWithAuth(parent *cobra.Command, cmd *cobra.Command) {
// Apply auth check to the command itself
withAuth(cmd)
// Apply auth check to all subcommands recursively
for _, subCmd := range cmd.Commands() {
withAuth(subCmd)
}
parent.AddCommand(cmd)
}
// initialize all the subcommands
func initCommands() {
cobra.EnableCommandSorting = false
rootCmd.AddGroup(
&cobra.Group{
ID: "api",
Title: "API Server Operations",
},
&cobra.Group{
ID: "query",
Title: "Query Operations",
},
)
// Add authentication commands (no auth required)
rootCmd.AddCommand(apiserver.NewLogonCmd(configManager))
rootCmd.AddCommand(apiserver.NewLogoutCmd(configManager))
// Add commands that require authentication
// These commands will automatically check authentication before execution
addCommandWithAuth(rootCmd, apiserver.NewListCmd(configManager))
addCommandWithAuth(rootCmd, permissions.NewPermsCmd(configManager))
addCommandWithAuth(rootCmd, roles.NewRolesCmd(configManager))
addCommandWithAuth(rootCmd, users.NewUsersCmd(configManager))
addCommandWithAuth(rootCmd, jobs.NewJobsCmd(configManager))
addCommandWithAuth(rootCmd, hosts.NewHostsCmd(configManager))
addCommandWithAuth(rootCmd, queues.NewQueuesCmd(configManager))
addCommandWithAuth(rootCmd, clusters.NewClustersCmd(configManager))
addCommandWithAuth(rootCmd, resources.NewResourcesCmd(configManager))
rootCmd.SetFlagErrorFunc(request.FlagErrorHandler)
}