opencode-provider-switch/internal/cli/serve.go
apale7 cf3dcec399 refactor(cli): rename tool to ocswitch
Avoid collision with the existing Opswitch product while keeping the repository name as opencode-provider-switch.

This aligns the CLI, provider key, config paths, examples, and Trellis history under one public-facing name.
2026-04-17 21:43:21 +08:00

45 lines
1.3 KiB
Go

package cli
import (
"os/signal"
"syscall"
"github.com/spf13/cobra"
"github.com/Apale7/opencode-provider-switch/internal/proxy"
)
func newServeCmd() *cobra.Command {
return &cobra.Command{
Use: "serve",
Short: "Run the local ocswitch proxy (alias -> failover upstream)",
Long: `serve starts the long-running local ocswitch proxy using the current local config.
It reads local alias/provider configuration, validates that config, and then
accepts OpenAI Responses traffic at the configured local base URL. With default
settings the proxy listens on http://127.0.0.1:9982/v1 and expects the local API
key ocswitch-local.
serve does not rewrite config files. Run doctor and opencode sync first so
OpenCode can see the same aliases that the proxy can route.`,
Example: ` ocswitch serve
ocswitch --config /path/to/config.json serve`,
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := loadCfg()
if err != nil {
return err
}
if errs := cfg.Validate(); len(errs) > 0 {
for _, e := range errs {
cmd.PrintErrln("config error:", e)
}
return errs[0]
}
srv := proxy.New(cfg)
ctx, stop := signal.NotifyContext(cmd.Context(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
return srv.ListenAndServe(ctx)
},
}
}