Skip to content

Commit ef229dc

Browse files
feat: 支持模板中动态渲染 GitLab endpoint、scheme 和 host
- 在 OutputConfig 结构体中添加 Endpoint、Scheme 和 Host 字段 - 新增 parseGitLabHostURL 函数,从 GitLabHost 解析出 endpoint、scheme 和 host - 更新模板示例,使用动态变量替代硬编码的服务器配置 - 允许模板根据不同的 GitLab 实例自动适配服务器信息
1 parent db58e5f commit ef229dc

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

internal/cli/cmd.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cli
22

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

67
"gitlab-cli-sdk/internal/config"
78
"gitlab-cli-sdk/internal/processor"
@@ -129,8 +130,14 @@ func runUserCreate(cfg *config.CLIConfig) error {
129130

130131
// 如果指定了输出文件,保存结果
131132
if cfg.OutputFile != "" {
133+
// 从 GitLabHost 解析 endpoint、scheme 和 host
134+
endpoint, scheme, host := parseGitLabHostURL(cfg.GitLabHost)
135+
132136
output := &types.OutputConfig{
133-
Users: userOutputs,
137+
Endpoint: endpoint,
138+
Scheme: scheme,
139+
Host: host,
140+
Users: userOutputs,
134141
}
135142

136143
// 如果指定了模板文件,使用模板渲染
@@ -205,3 +212,30 @@ func initializeClient(cfg *config.CLIConfig) (*client.GitLabClient, error) {
205212

206213
return gitlabClient, nil
207214
}
215+
216+
// parseGitLabHostURL 从 GitLab Host URL 解析出 endpoint、scheme 和 host
217+
func parseGitLabHostURL(gitlabHost string) (endpoint, scheme, host string) {
218+
// 默认值
219+
scheme = "https"
220+
endpoint = gitlabHost
221+
222+
// 去除尾部斜杠
223+
endpoint = strings.TrimSuffix(endpoint, "/")
224+
225+
// 检查是否包含 scheme
226+
if strings.HasPrefix(endpoint, "http://") {
227+
scheme = "http"
228+
host = strings.TrimPrefix(endpoint, "http://")
229+
} else if strings.HasPrefix(endpoint, "https://") {
230+
scheme = "https"
231+
host = strings.TrimPrefix(endpoint, "https://")
232+
} else {
233+
// 如果没有 scheme,则 host 就是原始的 endpoint
234+
host = endpoint
235+
}
236+
237+
// 重新添加 scheme 构造完整的 endpoint
238+
endpoint = scheme + "://" + host
239+
240+
return endpoint, scheme, host
241+
}

pkg/types/types.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ type ProjectSpec struct {
4343

4444
// OutputConfig 输出配置结构
4545
type OutputConfig struct {
46-
Users []UserOutput `yaml:"users"`
46+
Endpoint string `yaml:"endpoint"`
47+
Scheme string `yaml:"scheme"`
48+
Host string `yaml:"host"`
49+
Users []UserOutput `yaml:"users"`
4750
}
4851

4952
// UserOutput 用户输出结果

template-example.yaml

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

56
{{- range .Users }}
@@ -10,13 +11,10 @@
1011
toolchains:
1112
gitlab:
1213
# endpoint is the full endpoint of the server
13-
endpoint: https://devops-gitlab.alaudatech.net
14-
# host is the ip or the hostname of the server
15-
host: devops-gitlab.alaudatech.net
16-
# port is the port number that server is listening
17-
port: 443
14+
endpoint: {{ $.Endpoint }}
15+
host: {{ $.Host }}
1816
# scheme, http or https
19-
scheme: https
17+
scheme: {{ $.Scheme }}
2018
# username, the user name of the login account
2119
username: {{ .Username }}
2220
# email
@@ -30,7 +28,7 @@ toolchains:
3028
{{- end }}
3129
{{- if .Groups }}
3230
# Groups and Projects
33-
groups:
31+
TestGroups:
3432
{{- range .Groups }}
3533
- name: {{ .Name }}
3634
path: {{ .Path }}

0 commit comments

Comments
 (0)