Skip to content

Commit 3d99dbf

Browse files
feat: 模板支持 Port 参数输出
- 在 OutputConfig 中添加 Port 字段 - parseGitLabHostURL 函数新增 port 解析逻辑 - 自动识别 http (80) 和 https (443) 默认端口 - 支持从 URL 中解析自定义端口号 - 非默认端口会自动添加到 endpoint 中 - 更新模板示例文档,添加 Port 使用说明
1 parent 27af106 commit 3d99dbf

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

examples/template-example.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# GitLab CLI 输出模板示例
22
# 支持 Go template 语法,可以访问 OutputConfig 结构中的所有数据
3-
# 数据结构: .Endpoint (完整 URL), .Scheme (http/https), .Host (主机名) 是 GitLab 服务器配置
3+
# 数据结构: .Endpoint (完整 URL), .Scheme (http/https), .Host (主机名), .Port (端口号) 是 GitLab 服务器配置
44
# 数据结构: .Users[0] 包含 Username, Email, Name, UserID, Password, Token, Groups, Projects
55

66
{{- range .Users }}
@@ -13,6 +13,8 @@ toolchains:
1313
# endpoint is the full endpoint of the server
1414
endpoint: {{ $.Endpoint }}
1515
host: {{ $.Host }}
16+
# port number
17+
port: {{ $.Port }}
1618
# scheme, http or https
1719
scheme: {{ $.Scheme }}
1820
# username, the user name of the login account

internal/cli/cmd.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cli
22

33
import (
44
"log"
5+
"strconv"
56
"strings"
67

78
"gitlab-cli-sdk/internal/config"
@@ -130,13 +131,14 @@ func runUserCreate(cfg *config.CLIConfig) error {
130131

131132
// 如果指定了输出文件,保存结果
132133
if cfg.OutputFile != "" {
133-
// 从 GitLabHost 解析 endpoint、scheme 和 host
134-
endpoint, scheme, host := parseGitLabHostURL(cfg.GitLabHost)
134+
// 从 GitLabHost 解析 endpoint、scheme、hostport
135+
endpoint, scheme, host, port := parseGitLabHostURL(cfg.GitLabHost)
135136

136137
output := &types.OutputConfig{
137138
Endpoint: endpoint,
138139
Scheme: scheme,
139140
Host: host,
141+
Port: port,
140142
Users: userOutputs,
141143
}
142144

@@ -214,9 +216,10 @@ func initializeClient(cfg *config.CLIConfig) (*client.GitLabClient, error) {
214216
}
215217

216218
// parseGitLabHostURL 从 GitLab Host URL 解析出 endpoint、scheme 和 host
217-
func parseGitLabHostURL(gitlabHost string) (endpoint, scheme, host string) {
219+
func parseGitLabHostURL(gitlabHost string) (endpoint, scheme, host string, port int) {
218220
// 默认值
219221
scheme = "https"
222+
port = 443
220223
endpoint = gitlabHost
221224

222225
// 去除尾部斜杠
@@ -225,17 +228,37 @@ func parseGitLabHostURL(gitlabHost string) (endpoint, scheme, host string) {
225228
// 检查是否包含 scheme
226229
if strings.HasPrefix(endpoint, "http://") {
227230
scheme = "http"
231+
port = 80
228232
host = strings.TrimPrefix(endpoint, "http://")
229233
} else if strings.HasPrefix(endpoint, "https://") {
230234
scheme = "https"
235+
port = 443
231236
host = strings.TrimPrefix(endpoint, "https://")
232237
} else {
233238
// 如果没有 scheme,则 host 就是原始的 endpoint
234239
host = endpoint
235240
}
236241

242+
// 检查 host 中是否包含端口号
243+
if strings.Contains(host, ":") {
244+
parts := strings.Split(host, ":")
245+
if len(parts) == 2 {
246+
host = parts[0]
247+
// 尝试解析端口号
248+
if parsedPort, err := strconv.Atoi(parts[1]); err == nil {
249+
port = parsedPort
250+
}
251+
}
252+
}
253+
237254
// 重新添加 scheme 构造完整的 endpoint
238-
endpoint = scheme + "://" + host
255+
if (scheme == "http" && port == 80) || (scheme == "https" && port == 443) {
256+
// 默认端口,不需要在 endpoint 中显示
257+
endpoint = scheme + "://" + host
258+
} else {
259+
// 非默认端口,需要在 endpoint 中显示
260+
endpoint = scheme + "://" + host + ":" + strconv.Itoa(port)
261+
}
239262

240-
return endpoint, scheme, host
263+
return endpoint, scheme, host, port
241264
}

pkg/types/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type OutputConfig struct {
5050
Endpoint string `yaml:"endpoint"`
5151
Scheme string `yaml:"scheme"`
5252
Host string `yaml:"host"`
53+
Port int `yaml:"port"`
5354
Users []UserOutput `yaml:"users"`
5455
}
5556

0 commit comments

Comments
 (0)