blob: ae0e63def03c03d382ecfcfce0a6b90cfce4a60e [file] [log] [blame]
package main
import (
"fmt"
"os"
"github.com/alecthomas/kong"
kongyaml "github.com/alecthomas/kong-yaml"
"github.com/korap/korap-mcp/config"
"github.com/korap/korap-mcp/logger"
"github.com/rs/zerolog"
)
// Constants for server identification
const (
ServerName = "KorAP MCP Server"
ServerVersion = "0.1.0"
)
// CLI represents the command line interface
type CLI struct {
// Configuration file path
Config string `short:"c" type:"path" help:"Configuration file path"`
// OAuth2 authentication configuration
OAuth config.OAuthConfig `embed:"" prefix:"oauth-"`
// KorAP API configuration
KorAP config.KorAPConfig `embed:"" prefix:"korap-"`
// Cache configuration
Cache config.CacheConfig `embed:"" prefix:"cache-"`
// Logging configuration
Logging config.LoggingConfig `embed:"" prefix:"log-"`
// Version flag
Version bool `short:"v" help:"Show version information"`
}
// SetupCLI initializes the CLI parser with configuration
func SetupCLI() (*CLI, *kong.Kong, error) {
// Initialize default configuration
cfg := config.DefaultConfig()
cli := CLI{
OAuth: cfg.OAuth,
KorAP: cfg.KorAP,
Cache: cfg.Cache,
Logging: cfg.Logging,
}
// Setup kong with conditional YAML configuration
var parser *kong.Kong
var err error
if len(os.Args) > 1 {
// Check if config file is specified in arguments
for i, arg := range os.Args {
if (arg == "--config" || arg == "-c") && i+1 < len(os.Args) {
parser, err = kong.New(&cli,
kong.Name("korap-mcp"),
kong.Description("A Model Context Protocol server for KorAP corpus analysis platform."),
kong.Configuration(kongyaml.Loader, os.Args[i+1]),
kong.HelpOptions(kong.HelpOptions{Compact: true}),
)
break
}
}
}
// Fallback to parser without config file
if parser == nil {
parser, err = kong.New(&cli,
kong.Name("korap-mcp"),
kong.Description("A Model Context Protocol server for KorAP corpus analysis platform."),
kong.HelpOptions(kong.HelpOptions{Compact: true}),
)
}
if err != nil {
return nil, nil, fmt.Errorf("failed to create CLI parser: %w", err)
}
return &cli, parser, nil
}
// ValidateAndSetupLogging validates the configuration and sets up logging
func (c *CLI) ValidateAndSetupLogging() (zerolog.Logger, error) {
// Create config struct for validation with constants
fullConfig := config.Config{
Server: config.ServerConfig{
Name: ServerName,
Version: ServerVersion,
ConfigFile: c.Config,
},
OAuth: c.OAuth,
KorAP: c.KorAP,
Cache: c.Cache,
Logging: c.Logging,
}
// Validate configuration
if err := fullConfig.Validate(); err != nil {
return zerolog.Logger{}, fmt.Errorf("configuration validation failed: %w", err)
}
// Setup zerolog logging
logger, err := logger.SetupLogger(&c.Logging)
if err != nil {
return zerolog.Logger{}, fmt.Errorf("failed to setup logging: %w", err)
}
return logger, nil
}
// GetConfig returns the full configuration
func (c *CLI) GetConfig() *config.Config {
return &config.Config{
Server: config.ServerConfig{
Name: ServerName,
Version: ServerVersion,
ConfigFile: c.Config,
},
OAuth: c.OAuth,
KorAP: c.KorAP,
Cache: c.Cache,
Logging: c.Logging,
}
}
// GetServerName returns the server name constant
func (c *CLI) GetServerName() string {
return ServerName
}
// GetServerVersion returns the server version constant
func (c *CLI) GetServerVersion() string {
return ServerVersion
}