diff --git a/internal/config/config.go b/internal/config/config.go index e8ec429..885b3db 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -8,21 +8,27 @@ import ( ) const ( - DefaultBaseURL = "https://www.gitlink.org.cn/api" - DefaultFormat = "table" + DefaultBaseURL = "https://www.gitlink.org.cn/api" + DefaultGatewayBaseURL = "https://gateway.gitlink.org.cn/api" + DefaultFormat = "table" + + // EnvGatewayBaseURL overrides GatewayBaseURL when set. + EnvGatewayBaseURL = "GITLINK_GATEWAY_URL" ) type Config struct { - BaseURL string `yaml:"base_url"` - Format string `yaml:"default_format"` - Editor string `yaml:"editor,omitempty"` - Pager string `yaml:"pager,omitempty"` + BaseURL string `yaml:"base_url"` + GatewayBaseURL string `yaml:"gateway_base_url,omitempty"` + Format string `yaml:"default_format"` + Editor string `yaml:"editor,omitempty"` + Pager string `yaml:"pager,omitempty"` } func DefaultConfig() *Config { return &Config{ - BaseURL: DefaultBaseURL, - Format: DefaultFormat, + BaseURL: DefaultBaseURL, + GatewayBaseURL: DefaultGatewayBaseURL, + Format: DefaultFormat, } } @@ -53,6 +59,12 @@ func Load() (*Config, error) { if cfg.BaseURL == "" { cfg.BaseURL = DefaultBaseURL } + if cfg.GatewayBaseURL == "" { + cfg.GatewayBaseURL = DefaultGatewayBaseURL + } + if v := os.Getenv(EnvGatewayBaseURL); v != "" { + cfg.GatewayBaseURL = v + } if cfg.Format == "" { cfg.Format = DefaultFormat } @@ -79,6 +91,8 @@ func Get(key string) (string, error) { switch key { case "base_url": return cfg.BaseURL, nil + case "gateway_base_url": + return cfg.GatewayBaseURL, nil case "default_format": return cfg.Format, nil case "editor": @@ -98,6 +112,8 @@ func Set(key, value string) error { switch key { case "base_url": cfg.BaseURL = value + case "gateway_base_url": + cfg.GatewayBaseURL = value case "default_format": cfg.Format = value case "editor": diff --git a/shortcuts/common/types.go b/shortcuts/common/types.go index 029c5cc..a838c32 100644 --- a/shortcuts/common/types.go +++ b/shortcuts/common/types.go @@ -10,6 +10,7 @@ import ( "github.com/gitlink-org/gitlink-cli/cmd/cmdutil" "github.com/gitlink-org/gitlink-cli/internal/client" + "github.com/gitlink-org/gitlink-cli/internal/config" "github.com/gitlink-org/gitlink-cli/internal/context" clierrors "github.com/gitlink-org/gitlink-cli/internal/errors" "github.com/gitlink-org/gitlink-cli/internal/output" @@ -21,7 +22,7 @@ type Shortcut struct { Description string Flags []Flag DryRun bool // 是否支持 dry-run - DryRunHint func(ctx *RuntimeContext) (string, error) // 返回预览描述 + DryRunHint func(ctx *RuntimeContext) (string, error) // 返回预览描述 Run func(ctx *RuntimeContext) error } @@ -37,12 +38,13 @@ type Flag struct { // RuntimeContext provides helpers for shortcut implementations. type RuntimeContext struct { - Client *client.Client - Owner string - Repo string - Format string - CommandName string - Args map[string]string + Client *client.Client + Owner string + Repo string + Format string + CommandName string + Args map[string]string + GatewayBaseURL string } // NewRuntimeContext creates a RuntimeContext with auto-resolved owner/repo. @@ -58,13 +60,19 @@ func NewRuntimeContext(args map[string]string, commandName string) (*RuntimeCont format = "json" } + gatewayBaseURL := config.DefaultGatewayBaseURL + if cfg, err := config.Load(); err == nil && cfg.GatewayBaseURL != "" { + gatewayBaseURL = cfg.GatewayBaseURL + } + return &RuntimeContext{ - Client: cli, - Owner: cmdutil.Owner, - Repo: cmdutil.Repo, - Format: format, - CommandName: commandName, - Args: args, + Client: cli, + Owner: cmdutil.Owner, + Repo: cmdutil.Repo, + Format: format, + CommandName: commandName, + Args: args, + GatewayBaseURL: gatewayBaseURL, }, nil } diff --git a/shortcuts/wiki/wiki.go b/shortcuts/wiki/wiki.go index 31777eb..11918bd 100644 --- a/shortcuts/wiki/wiki.go +++ b/shortcuts/wiki/wiki.go @@ -18,32 +18,30 @@ import ( "github.com/gitlink-org/gitlink-cli/shortcuts/common" ) -const gatewayBaseURL = "https://gateway.gitlink.org.cn/api" - -var ( - projectIDCache sync.Map - gatewayClient *client.Client - gatewayOnce sync.Once -) +var projectIDCache sync.Map func wikiPath(endpoint string) string { return "/wiki/open/" + endpoint } -func getGatewayClient() *client.Client { - gatewayOnce.Do(func() { - gatewayClient = &client.Client{ - HTTP: auth.NewHTTPClient(), - BaseURL: gatewayBaseURL, - SkipJSONSuffix: true, - } - }) - return gatewayClient +// getGatewayClient returns a client targeting the Wiki API gateway. +// The BaseURL is resolved from RuntimeContext.GatewayBaseURL, which in turn +// honours (in order): GITLINK_GATEWAY_URL env > config gateway_base_url > default. +func getGatewayClient(ctx *common.RuntimeContext) *client.Client { + baseURL := ctx.GatewayBaseURL + if baseURL == "" { + baseURL = "https://gateway.gitlink.org.cn/api" + } + return &client.Client{ + HTTP: auth.NewHTTPClient(), + BaseURL: baseURL, + SkipJSONSuffix: true, + Debug: ctx.Client.Debug, + } } func callWikiAPI(ctx *common.RuntimeContext, method, path string, body interface{}) (*output.Envelope, error) { - gc := getGatewayClient() - gc.Debug = ctx.Client.Debug + gc := getGatewayClient(ctx) env, err := gc.Do(method, path, body, nil) if err != nil { return nil, err @@ -52,8 +50,7 @@ func callWikiAPI(ctx *common.RuntimeContext, method, path string, body interface } func callWikiAPIWithQuery(ctx *common.RuntimeContext, method, path string, query url.Values) (*output.Envelope, error) { - gc := getGatewayClient() - gc.Debug = ctx.Client.Debug + gc := getGatewayClient(ctx) env, err := gc.Do(method, path, nil, query) if err != nil { return nil, err @@ -218,12 +215,12 @@ type LintIssue struct { } type LintSummary struct { - Repository string `json:"repository"` - TotalPages int `json:"total_pages"` - TotalIssues int `json:"total_issues"` - Errors int `json:"errors"` - Warnings int `json:"warnings"` - Results []LintIssue `json:"results"` + Repository string `json:"repository"` + TotalPages int `json:"total_pages"` + TotalIssues int `json:"total_issues"` + Errors int `json:"errors"` + Warnings int `json:"warnings"` + Results []LintIssue `json:"results"` } var (