-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime.go
More file actions
132 lines (114 loc) · 3.43 KB
/
Copy pathruntime.go
File metadata and controls
132 lines (114 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"context"
"errors"
"fmt"
"os"
"strings"
"time"
"github.com/spf13/cobra"
)
type outputFormat string
const (
outputFormatJSON outputFormat = "json"
outputFormatTable outputFormat = "table"
)
type runtimeConfig struct {
Profile string
BaseURL string
ProjectID string
APIKey string
Token string
Model string
Output outputFormat
Timeout time.Duration
AllowAll bool
Allow []string
Trace bool
}
type runtimeConfigKey struct{}
func withRuntimeConfig(ctx context.Context, cfg runtimeConfig) context.Context {
return context.WithValue(ctx, runtimeConfigKey{}, cfg)
}
func runtimeConfigFrom(cmd *cobra.Command) (runtimeConfig, error) {
val := cmd.Context().Value(runtimeConfigKey{})
if val == nil {
return runtimeConfig{}, errors.New("runtime config unavailable")
}
cfg, ok := val.(runtimeConfig)
if !ok {
return runtimeConfig{}, errors.New("runtime config invalid")
}
return cfg, nil
}
func resolveRuntimeConfig(cmd *cobra.Command, cfgFile cliConfig) (runtimeConfig, error) {
profileFlag, _ := cmd.Flags().GetString("profile")
profileName := resolveProfileName(profileFlag, cfgFile)
profile := profileFor(cfgFile, profileName)
baseFlag, _ := cmd.Flags().GetString("base-url")
projectFlag, _ := cmd.Flags().GetString("project")
apiKeyFlag, _ := cmd.Flags().GetString("api-key")
tokenFlag, _ := cmd.Flags().GetString("token")
jsonFlag, _ := cmd.Flags().GetBool("json")
timeoutFlag, _ := cmd.Flags().GetDuration("timeout")
baseURL := firstNonEmpty(baseFlag, os.Getenv("MODELRELAY_API_BASE_URL"), profile.BaseURL, defaultAPIBaseURL)
if strings.TrimSpace(baseURL) == "" {
return runtimeConfig{}, errors.New("base URL is required")
}
projectID := firstNonEmpty(projectFlag, os.Getenv("MODELRELAY_PROJECT_ID"), profile.ProjectID)
apiKey := firstNonEmpty(apiKeyFlag, os.Getenv("MODELRELAY_API_KEY"), os.Getenv("MODELRELAY_SECRET_KEY"), profile.APIKey)
token := firstNonEmpty(tokenFlag, os.Getenv("MODELRELAY_TOKEN"), profile.Token)
model := firstNonEmpty(os.Getenv("MODELRELAY_MODEL"), profile.Model)
output, err := resolveOutputFormat(jsonFlag, profile.Output)
if err != nil {
return runtimeConfig{}, err
}
timeout := 30 * time.Second
if timeoutFlag > 0 {
timeout = timeoutFlag
}
return runtimeConfig{
Profile: profileName,
BaseURL: strings.TrimSpace(baseURL),
ProjectID: strings.TrimSpace(projectID),
APIKey: strings.TrimSpace(apiKey),
Token: strings.TrimSpace(token),
Model: strings.TrimSpace(model),
Output: output,
Timeout: timeout,
AllowAll: profile.AllowAll,
Allow: profile.Allow,
Trace: profile.Trace,
}, nil
}
func resolveOutputFormat(jsonFlag bool, profileOutput string) (outputFormat, error) {
if jsonFlag {
return outputFormatJSON, nil
}
raw := strings.TrimSpace(profileOutput)
if raw == "" {
return outputFormatTable, nil
}
switch strings.ToLower(raw) {
case string(outputFormatJSON):
return outputFormatJSON, nil
case string(outputFormatTable):
return outputFormatTable, nil
default:
return "", fmt.Errorf("invalid output format: %s", raw)
}
}
func contextWithTimeout(timeout time.Duration) (context.Context, context.CancelFunc) {
if timeout <= 0 {
return context.WithCancel(context.Background())
}
return context.WithTimeout(context.Background(), timeout)
}
func firstNonEmpty(values ...string) string {
for _, val := range values {
if strings.TrimSpace(val) != "" {
return val
}
}
return ""
}