blob: ae0e63def03c03d382ecfcfce0a6b90cfce4a60e [file] [log] [blame]
Akron90f65212025-06-12 14:32:55 +02001package main
2
3import (
4 "fmt"
5 "os"
6
7 "github.com/alecthomas/kong"
8 kongyaml "github.com/alecthomas/kong-yaml"
9
10 "github.com/korap/korap-mcp/config"
11 "github.com/korap/korap-mcp/logger"
12 "github.com/rs/zerolog"
13)
14
15// Constants for server identification
16const (
17 ServerName = "KorAP MCP Server"
18 ServerVersion = "0.1.0"
19)
20
21// CLI represents the command line interface
22type CLI struct {
23 // Configuration file path
24 Config string `short:"c" type:"path" help:"Configuration file path"`
25
26 // OAuth2 authentication configuration
27 OAuth config.OAuthConfig `embed:"" prefix:"oauth-"`
28
29 // KorAP API configuration
30 KorAP config.KorAPConfig `embed:"" prefix:"korap-"`
31
Akrone73bc912025-06-17 16:36:45 +020032 // Cache configuration
33 Cache config.CacheConfig `embed:"" prefix:"cache-"`
34
Akron90f65212025-06-12 14:32:55 +020035 // Logging configuration
36 Logging config.LoggingConfig `embed:"" prefix:"log-"`
37
38 // Version flag
39 Version bool `short:"v" help:"Show version information"`
40}
41
42// SetupCLI initializes the CLI parser with configuration
43func SetupCLI() (*CLI, *kong.Kong, error) {
44 // Initialize default configuration
45 cfg := config.DefaultConfig()
46
47 cli := CLI{
48 OAuth: cfg.OAuth,
49 KorAP: cfg.KorAP,
Akrone73bc912025-06-17 16:36:45 +020050 Cache: cfg.Cache,
Akron90f65212025-06-12 14:32:55 +020051 Logging: cfg.Logging,
52 }
53
54 // Setup kong with conditional YAML configuration
55 var parser *kong.Kong
56 var err error
57
58 if len(os.Args) > 1 {
59 // Check if config file is specified in arguments
60 for i, arg := range os.Args {
61 if (arg == "--config" || arg == "-c") && i+1 < len(os.Args) {
62 parser, err = kong.New(&cli,
63 kong.Name("korap-mcp"),
64 kong.Description("A Model Context Protocol server for KorAP corpus analysis platform."),
65 kong.Configuration(kongyaml.Loader, os.Args[i+1]),
66 kong.HelpOptions(kong.HelpOptions{Compact: true}),
67 )
68 break
69 }
70 }
71 }
72
73 // Fallback to parser without config file
74 if parser == nil {
75 parser, err = kong.New(&cli,
76 kong.Name("korap-mcp"),
77 kong.Description("A Model Context Protocol server for KorAP corpus analysis platform."),
78 kong.HelpOptions(kong.HelpOptions{Compact: true}),
79 )
80 }
81 if err != nil {
82 return nil, nil, fmt.Errorf("failed to create CLI parser: %w", err)
83 }
84
85 return &cli, parser, nil
86}
87
88// ValidateAndSetupLogging validates the configuration and sets up logging
89func (c *CLI) ValidateAndSetupLogging() (zerolog.Logger, error) {
90 // Create config struct for validation with constants
91 fullConfig := config.Config{
92 Server: config.ServerConfig{
93 Name: ServerName,
94 Version: ServerVersion,
95 ConfigFile: c.Config,
96 },
97 OAuth: c.OAuth,
98 KorAP: c.KorAP,
Akrone73bc912025-06-17 16:36:45 +020099 Cache: c.Cache,
Akron90f65212025-06-12 14:32:55 +0200100 Logging: c.Logging,
101 }
102
103 // Validate configuration
104 if err := fullConfig.Validate(); err != nil {
105 return zerolog.Logger{}, fmt.Errorf("configuration validation failed: %w", err)
106 }
107
108 // Setup zerolog logging
109 logger, err := logger.SetupLogger(&c.Logging)
110 if err != nil {
111 return zerolog.Logger{}, fmt.Errorf("failed to setup logging: %w", err)
112 }
113
114 return logger, nil
115}
116
117// GetConfig returns the full configuration
118func (c *CLI) GetConfig() *config.Config {
119 return &config.Config{
120 Server: config.ServerConfig{
121 Name: ServerName,
122 Version: ServerVersion,
123 ConfigFile: c.Config,
124 },
125 OAuth: c.OAuth,
126 KorAP: c.KorAP,
Akrone73bc912025-06-17 16:36:45 +0200127 Cache: c.Cache,
Akron90f65212025-06-12 14:32:55 +0200128 Logging: c.Logging,
129 }
130}
131
132// GetServerName returns the server name constant
133func (c *CLI) GetServerName() string {
134 return ServerName
135}
136
137// GetServerVersion returns the server version constant
138func (c *CLI) GetServerVersion() string {
139 return ServerVersion
140}