refactor(cmd): use cobra to align with kubernetes (123)

Use cobra to align with kubernetes
---
Update unit tests based on new Cobra
---
Add help test back
This commit is contained in:
Arda Güçlü
2025-06-17 19:56:32 +03:00
committed by Marc Nuri
parent b07cd04d60
commit 2c18ca0822
5 changed files with 223 additions and 166 deletions

View File

@@ -1,7 +1,20 @@
package main
import "github.com/manusa/kubernetes-mcp-server/pkg/kubernetes-mcp-server/cmd"
import (
"os"
"github.com/spf13/pflag"
"k8s.io/cli-runtime/pkg/genericiooptions"
"github.com/manusa/kubernetes-mcp-server/pkg/kubernetes-mcp-server/cmd"
)
func main() {
cmd.Execute()
flags := pflag.NewFlagSet("kubernetes-mcp-server", pflag.ExitOnError)
pflag.CommandLine = flags
root := cmd.NewMCPServer(genericiooptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr})
if err := root.Execute(); err != nil {
os.Exit(1)
}
}

11
go.mod
View File

@@ -8,8 +8,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/spf13/afero v1.14.0
github.com/spf13/cobra v1.9.1
github.com/spf13/viper v1.20.1
golang.org/x/net v0.41.0
github.com/spf13/pflag v1.0.6
golang.org/x/sync v0.15.0
helm.sh/helm/v3 v3.18.2
k8s.io/api v0.33.1
@@ -56,7 +55,6 @@ require (
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/btree v1.1.3 // indirect
@@ -95,25 +93,20 @@ require (
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.1 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/rubenv/sql-migrate v1.8.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sagikazarmark/locafero v0.7.0 // indirect
github.com/shopspring/decimal v1.4.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/cast v1.7.1 // indirect
github.com/spf13/pflag v1.0.6 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
github.com/yosida95/uritemplate/v3 v3.0.2 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.39.0 // indirect
golang.org/x/net v0.41.0 // indirect
golang.org/x/oauth2 v0.28.0 // indirect
golang.org/x/sys v0.33.0 // indirect
golang.org/x/term v0.32.0 // indirect

12
go.sum
View File

@@ -110,8 +110,6 @@ github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpv
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI=
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss=
github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
@@ -230,8 +228,6 @@ github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.1.1 h1:y0fUlFfIZhPF1W537XOLg0/fcx6zcHCJwooC2xJA040=
github.com/opencontainers/image-spec v1.1.1/go.mod h1:qpqAh3Dmcf36wStyyWU+kCeDgrGnAve2nCC8+7h8Q0M=
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+vxiaj6gdUUzhl4XmI=
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5 h1:Ii+DKncOVM8Cu1Hc+ETb5K+23HdAMvESYE3ZJ5b5cMI=
@@ -263,16 +259,12 @@ github.com/rubenv/sql-migrate v1.8.0 h1:dXnYiJk9k3wetp7GfQbKJcPHjVJL6YK19tKj8t2N
github.com/rubenv/sql-migrate v1.8.0/go.mod h1:F2bGFBwCU+pnmbtNYDeKvSuvL6lBVtXDXUUv5t+u1qw=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sagikazarmark/locafero v0.7.0 h1:5MqpDsTGNDhY8sGp0Aowyf0qKsPrhewaLSsFaodPcyo=
github.com/sagikazarmark/locafero v0.7.0/go.mod h1:2za3Cg5rMaTMoG/2Ulr9AwtFaIppKXTRYnozin4aB5k=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/spf13/afero v1.14.0 h1:9tH6MapGnn/j0eb0yIXiLjERO8RB6xIVZRDCX7PtqWA=
github.com/spf13/afero v1.14.0/go.mod h1:acJQ8t0ohCGuMN3O+Pv0V0hgMxNYDlvdk+VTfyZmbYo=
github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y=
@@ -281,8 +273,6 @@ github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.20.1 h1:ZMi+z/lvLyPSCoNtFCpqjy0S4kPbirhpTMwl8BkW9X4=
github.com/spf13/viper v1.20.1/go.mod h1:P9Mdzt1zoHIG8m2eZQinpiBjo6kCmZSKBClNNqjJvu4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
@@ -297,8 +287,6 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=

View File

@@ -1,140 +1,180 @@
package cmd
import (
"context"
"errors"
"flag"
"fmt"
"strconv"
"strings"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericiooptions"
"k8s.io/klog/v2"
"k8s.io/klog/v2/textlogger"
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"
"github.com/manusa/kubernetes-mcp-server/pkg/mcp"
"github.com/manusa/kubernetes-mcp-server/pkg/output"
"github.com/manusa/kubernetes-mcp-server/pkg/version"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/net/context"
"k8s.io/klog/v2"
"k8s.io/klog/v2/textlogger"
"os"
"strconv"
"strings"
)
var rootCmd = &cobra.Command{
Use: "kubernetes-mcp-server [command] [options]",
Short: "Kubernetes Model Context Protocol (MCP) server",
Long: `
Kubernetes Model Context Protocol (MCP) server
var (
long = templates.LongDesc(i18n.T("Kubernetes Model Context Protocol (MCP) server"))
examples = templates.Examples(i18n.T(`
# show this help
kubernetes-mcp-server -h
# show this help
kubernetes-mcp-server -h
# shows version information
kubernetes-mcp-server --version
# shows version information
kubernetes-mcp-server --version
# start STDIO server
kubernetes-mcp-server
# start STDIO server
kubernetes-mcp-server
# start a SSE server on port 8080
kubernetes-mcp-server --sse-port 8080
# start a SSE server on port 8080
kubernetes-mcp-server --sse-port 8080
# start a SSE server on port 8443 with a public HTTPS host of example.com
kubernetes-mcp-server --sse-port 8443 --sse-base-url https://example.com:8443
`))
)
# start a SSE server on port 8443 with a public HTTPS host of example.com
kubernetes-mcp-server --sse-port 8443 --sse-base-url https://example.com:8443
type MCPServerOptions struct {
Version bool
LogLevel int
SSEPort int
HttpPort int
SSEBaseUrl string
Kubeconfig string
Profile string
ListOutput string
ReadOnly bool
DisableDestructive bool
# TODO: add more examples`,
Run: func(cmd *cobra.Command, args []string) {
initLogging()
profile := mcp.ProfileFromString(viper.GetString("profile"))
if profile == nil {
fmt.Printf("Invalid profile name: %s, valid names are: %s\n", viper.GetString("profile"), strings.Join(mcp.ProfileNames, ", "))
os.Exit(1)
}
listOutput := output.FromString(viper.GetString("list-output"))
if listOutput == nil {
fmt.Printf("Invalid output name: %s, valid names are: %s\n", viper.GetString("list-output"), strings.Join(output.Names, ", "))
os.Exit(1)
}
klog.V(1).Info("Starting kubernetes-mcp-server")
klog.V(1).Infof(" - Profile: %s", profile.GetName())
klog.V(1).Infof(" - ListOutput: %s", listOutput.GetName())
klog.V(1).Infof(" - Read-only mode: %t", viper.GetBool("read-only"))
klog.V(1).Infof(" - Disable destructive tools: %t", viper.GetBool("disable-destructive"))
if viper.GetBool("version") {
fmt.Println(version.Version)
return
}
mcpServer, err := mcp.NewSever(mcp.Configuration{
Profile: profile,
ListOutput: listOutput,
ReadOnly: viper.GetBool("read-only"),
DisableDestructive: viper.GetBool("disable-destructive"),
Kubeconfig: viper.GetString("kubeconfig"),
})
if err != nil {
fmt.Printf("Failed to initialize MCP server: %v\n", err)
os.Exit(1)
}
defer mcpServer.Close()
ssePort := viper.GetInt("sse-port")
if ssePort > 0 {
sseServer := mcpServer.ServeSse(viper.GetString("sse-base-url"))
defer func() { _ = sseServer.Shutdown(cmd.Context()) }()
klog.V(0).Infof("SSE server starting on port %d and path /sse", ssePort)
if err := sseServer.Start(fmt.Sprintf(":%d", ssePort)); err != nil {
klog.Errorf("Failed to start SSE server: %s", err)
return
}
}
httpPort := viper.GetInt("http-port")
if httpPort > 0 {
httpServer := mcpServer.ServeHTTP()
klog.V(0).Infof("Streaming HTTP server starting on port %d and path /mcp", httpPort)
if err := httpServer.Start(fmt.Sprintf(":%d", httpPort)); err != nil {
klog.Errorf("Failed to start streaming HTTP server: %s", err)
return
}
}
if err := mcpServer.ServeStdio(); err != nil && !errors.Is(err, context.Canceled) {
panic(err)
}
},
genericiooptions.IOStreams
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
klog.Errorf("Failed to execute command: %s", err)
os.Exit(1)
func NewMCPServerOptions(streams genericiooptions.IOStreams) *MCPServerOptions {
return &MCPServerOptions{
IOStreams: streams,
Profile: "full",
ListOutput: "table",
}
}
func initLogging() {
flagSet := flag.NewFlagSet("kubernetes-mcp-server", flag.ContinueOnError)
func NewMCPServer(streams genericiooptions.IOStreams) *cobra.Command {
o := NewMCPServerOptions(streams)
cmd := &cobra.Command{
Use: "kubernetes-mcp-server [command] [options]",
Short: "Kubernetes Model Context Protocol (MCP) server",
Long: long,
Example: examples,
RunE: func(c *cobra.Command, args []string) error {
if err := o.Complete(); err != nil {
return err
}
if err := o.Validate(); err != nil {
return err
}
if err := o.Run(); err != nil {
return err
}
return nil
},
}
cmd.Flags().BoolVar(&o.Version, "version", o.Version, "Print version information and quit")
cmd.Flags().IntVar(&o.LogLevel, "log-level", o.LogLevel, "Set the log level (from 0 to 9)")
cmd.Flags().IntVar(&o.SSEPort, "sse-port", o.SSEPort, "Start a SSE server on the specified port")
cmd.Flags().IntVar(&o.HttpPort, "http-port", o.HttpPort, "Start a streamable HTTP server on the specified port")
cmd.Flags().StringVar(&o.SSEBaseUrl, "sse-base-url", o.SSEBaseUrl, "SSE public base URL to use when sending the endpoint message (e.g. https://example.com)")
cmd.Flags().StringVar(&o.Kubeconfig, "kubeconfig", o.Kubeconfig, "Path to the kubeconfig file to use for authentication")
cmd.Flags().StringVar(&o.Profile, "profile", o.Profile, "MCP profile to use (one of: "+strings.Join(mcp.ProfileNames, ", ")+")")
cmd.Flags().StringVar(&o.ListOutput, "list-output", o.ListOutput, "Output format for resource list operations (one of: "+strings.Join(output.Names, ", ")+")")
cmd.Flags().BoolVar(&o.ReadOnly, "read-only", o.ReadOnly, "If true, only tools annotated with readOnlyHint=true are exposed")
cmd.Flags().BoolVar(&o.DisableDestructive, "disable-destructive", o.DisableDestructive, "If true, tools annotated with destructiveHint=true are disabled")
return cmd
}
func (m *MCPServerOptions) Complete() error {
m.initializeLogging()
return nil
}
func (m *MCPServerOptions) initializeLogging() {
flagSet := flag.NewFlagSet("klog", flag.ContinueOnError)
klog.InitFlags(flagSet)
loggerOptions := []textlogger.ConfigOption{textlogger.Output(os.Stdout)}
if logLevel := viper.GetInt("log-level"); logLevel >= 0 {
loggerOptions = append(loggerOptions, textlogger.Verbosity(logLevel))
_ = flagSet.Parse([]string{"--v", strconv.Itoa(logLevel)})
loggerOptions := []textlogger.ConfigOption{textlogger.Output(m.Out)}
if m.LogLevel >= 0 {
loggerOptions = append(loggerOptions, textlogger.Verbosity(m.LogLevel))
_ = flagSet.Parse([]string{"--v", strconv.Itoa(m.LogLevel)})
}
logger := textlogger.NewLogger(textlogger.NewConfig(loggerOptions...))
klog.SetLoggerWithOptions(logger)
}
// flagInit initializes the flags for the root command.
// Exposed for testing purposes.
func flagInit() {
rootCmd.Flags().BoolP("version", "v", false, "Print version information and quit")
rootCmd.Flags().IntP("log-level", "", 0, "Set the log level (from 0 to 9)")
rootCmd.Flags().IntP("sse-port", "", 0, "Start a SSE server on the specified port")
rootCmd.Flags().IntP("http-port", "", 0, "Start a streamable HTTP server on the specified port")
rootCmd.Flags().StringP("sse-base-url", "", "", "SSE public base URL to use when sending the endpoint message (e.g. https://example.com)")
rootCmd.Flags().StringP("kubeconfig", "", "", "Path to the kubeconfig file to use for authentication")
rootCmd.Flags().String("profile", "full", "MCP profile to use (one of: "+strings.Join(mcp.ProfileNames, ", ")+")")
rootCmd.Flags().String("list-output", "table", "Output format for resource list operations (one of: "+strings.Join(output.Names, ", ")+")")
rootCmd.Flags().Bool("read-only", false, "If true, only tools annotated with readOnlyHint=true are exposed")
rootCmd.Flags().Bool("disable-destructive", false, "If true, tools annotated with destructiveHint=true are disabled")
_ = viper.BindPFlags(rootCmd.Flags())
func (m *MCPServerOptions) Validate() error {
return nil
}
func init() {
flagInit()
func (m *MCPServerOptions) Run() error {
profile := mcp.ProfileFromString(m.Profile)
if profile == nil {
return fmt.Errorf("Invalid profile name: %s, valid names are: %s\n", m.Profile, strings.Join(mcp.ProfileNames, ", "))
}
listOutput := output.FromString(m.ListOutput)
if listOutput == nil {
return fmt.Errorf("Invalid output name: %s, valid names are: %s\n", m.ListOutput, strings.Join(output.Names, ", "))
}
klog.V(1).Info("Starting kubernetes-mcp-server")
klog.V(1).Infof(" - Profile: %s", profile.GetName())
klog.V(1).Infof(" - ListOutput: %s", listOutput.GetName())
klog.V(1).Infof(" - Read-only mode: %t", m.ReadOnly)
klog.V(1).Infof(" - Disable destructive tools: %t", m.DisableDestructive)
if m.Version {
fmt.Fprintf(m.Out, "%s\n", version.Version)
return nil
}
mcpServer, err := mcp.NewSever(mcp.Configuration{
Profile: profile,
ListOutput: listOutput,
ReadOnly: m.ReadOnly,
DisableDestructive: m.DisableDestructive,
Kubeconfig: m.Kubeconfig,
})
if err != nil {
return fmt.Errorf("Failed to initialize MCP server: %w\n", err)
}
defer mcpServer.Close()
ctx := context.Background()
if m.SSEPort > 0 {
sseServer := mcpServer.ServeSse(m.SSEBaseUrl)
defer func() { _ = sseServer.Shutdown(ctx) }()
klog.V(0).Infof("SSE server starting on port %d and path /sse", m.SSEPort)
if err := sseServer.Start(fmt.Sprintf(":%d", m.SSEPort)); err != nil {
return fmt.Errorf("failed to start SSE server: %w\n", err)
}
}
if m.HttpPort > 0 {
httpServer := mcpServer.ServeHTTP()
klog.V(0).Infof("Streaming HTTP server starting on port %d and path /mcp", m.HttpPort)
if err := httpServer.Start(fmt.Sprintf(":%d", m.HttpPort)); err != nil {
return fmt.Errorf("failed to start streaming HTTP server: %w\n", err)
}
}
if err := mcpServer.ServeStdio(); err != nil && !errors.Is(err, context.Canceled) {
return err
}
return nil
}

View File

@@ -1,10 +1,13 @@
package cmd
import (
"bytes"
"io"
"os"
"strings"
"testing"
"k8s.io/cli-runtime/pkg/genericiooptions"
)
func captureOutput(f func() error) (string, error) {
@@ -21,64 +24,84 @@ func captureOutput(f func() error) (string, error) {
}
func TestVersion(t *testing.T) {
rootCmd.SetArgs([]string{"--version"})
rootCmd.ResetFlags()
flagInit()
version, err := captureOutput(rootCmd.Execute)
if version != "0.0.0\n" {
t.Fatalf("Expected version 0.0.0, got %s %v", version, err)
in := &bytes.Buffer{}
out := &bytes.Buffer{}
errOut := io.Discard
rootCmd := NewMCPServerOptions(genericiooptions.IOStreams{In: in, Out: out, ErrOut: errOut})
rootCmd.Version = true
rootCmd.Run()
if out.String() != "0.0.0\n" {
t.Fatalf("Expected version 0.0.0, got %s", out.String())
}
}
func TestProfile(t *testing.T) {
t.Run("default", func(t *testing.T) {
rootCmd.SetArgs([]string{"--version", "--log-level=1"})
rootCmd.ResetFlags()
flagInit()
out, err := captureOutput(rootCmd.Execute)
if !strings.Contains(out, "- Profile: full") {
t.Fatalf("Expected profile 'full', got %s %v", out, err)
in := &bytes.Buffer{}
out := &bytes.Buffer{}
errOut := io.Discard
rootCmd := NewMCPServerOptions(genericiooptions.IOStreams{In: in, Out: out, ErrOut: errOut})
rootCmd.Version = true
rootCmd.LogLevel = 1
rootCmd.Complete()
rootCmd.Run()
if !strings.Contains(out.String(), "- Profile: full") {
t.Fatalf("Expected profile 'full', got %s", out)
}
})
}
func TestListOutput(t *testing.T) {
t.Run("available", func(t *testing.T) {
in := &bytes.Buffer{}
out := io.Discard
errOut := io.Discard
rootCmd := NewMCPServer(genericiooptions.IOStreams{In: in, Out: out, ErrOut: errOut})
rootCmd.SetArgs([]string{"--help"})
rootCmd.ResetFlags()
flagInit()
out, err := captureOutput(rootCmd.Execute)
if !strings.Contains(out, "Output format for resource list operations (one of: yaml, table)") {
o, err := captureOutput(rootCmd.Execute)
if !strings.Contains(o, "Output format for resource list operations (one of: yaml, table)") {
t.Fatalf("Expected all available outputs, got %s %v", out, err)
}
})
t.Run("defaults to table", func(t *testing.T) {
rootCmd.SetArgs([]string{"--version", "--log-level=1"})
rootCmd.ResetFlags()
flagInit()
out, err := captureOutput(rootCmd.Execute)
if !strings.Contains(out, "- ListOutput: table") {
t.Fatalf("Expected list-output 'table', got %s %v", out, err)
in := &bytes.Buffer{}
out := &bytes.Buffer{}
errOut := io.Discard
rootCmd := NewMCPServerOptions(genericiooptions.IOStreams{In: in, Out: out, ErrOut: errOut})
rootCmd.Version = true
rootCmd.LogLevel = 1
rootCmd.Complete()
rootCmd.Run()
if !strings.Contains(out.String(), "- ListOutput: table") {
t.Fatalf("Expected list-output 'table', got %s", out)
}
})
}
func TestDefaultReadOnly(t *testing.T) {
rootCmd.SetArgs([]string{"--version", "--log-level=1"})
rootCmd.ResetFlags()
flagInit()
out, err := captureOutput(rootCmd.Execute)
if !strings.Contains(out, " - Read-only mode: false") {
t.Fatalf("Expected read-only mode false, got %s %v", out, err)
in := &bytes.Buffer{}
out := &bytes.Buffer{}
errOut := io.Discard
rootCmd := NewMCPServerOptions(genericiooptions.IOStreams{In: in, Out: out, ErrOut: errOut})
rootCmd.Version = true
rootCmd.LogLevel = 1
rootCmd.Complete()
rootCmd.Run()
if !strings.Contains(out.String(), " - Read-only mode: false") {
t.Fatalf("Expected read-only mode false, got %s", out)
}
}
func TestDefaultDisableDestructive(t *testing.T) {
rootCmd.SetArgs([]string{"--version", "--log-level=1"})
rootCmd.ResetFlags()
flagInit()
out, err := captureOutput(rootCmd.Execute)
if !strings.Contains(out, " - Disable destructive tools: false") {
t.Fatalf("Expected disable destructive false, got %s %v", out, err)
in := &bytes.Buffer{}
out := &bytes.Buffer{}
errOut := io.Discard
rootCmd := NewMCPServerOptions(genericiooptions.IOStreams{In: in, Out: out, ErrOut: errOut})
rootCmd.Version = true
rootCmd.LogLevel = 1
rootCmd.Complete()
rootCmd.Run()
if !strings.Contains(out.String(), " - Disable destructive tools: false") {
t.Fatalf("Expected disable destructive false, got %s", out)
}
}