go20/devops/agent/script/response.go
2026-03-15 16:24:01 +08:00

59 lines
1.3 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package script
import (
"fmt"
"os"
"strings"
)
// ParseOutputParams 解析脚本输出参数文件output.env
// 支持标准的 key=value 格式,自动处理注释、空行、引号等
func ParseOutputParams(filePath string) (map[string]string, error) {
data, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
params := make(map[string]string)
lines := strings.Split(string(data), "\n")
for lineNum, line := range lines {
// 去除首尾空白
line = strings.TrimSpace(line)
// 跳过空行和注释
if line == "" || strings.HasPrefix(line, "#") {
continue
}
// 查找第一个 = 号
parts := strings.SplitN(line, "=", 2)
if len(parts) != 2 {
// 格式不正确,记录警告但继续处理
fmt.Printf("[WARN] output.env line %d: invalid format, skipping: %s\n", lineNum+1, line)
continue
}
key := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])
// 移除值两端的引号(支持单引号和双引号)
if len(value) >= 2 {
if (value[0] == '"' && value[len(value)-1] == '"') ||
(value[0] == '\'' && value[len(value)-1] == '\'') {
value = value[1 : len(value)-1]
}
}
// 验证 key 不为空
if key == "" {
fmt.Printf("[WARN] output.env line %d: empty key, skipping\n", lineNum+1)
continue
}
params[key] = value
}
return params, nil
}