MCPcopy Create free account
hub / github.com/0xUnixIO/pulse / upsertEnvFile

Function upsertEnvFile

internal/nodeenroll/cmd.go:139–166  ·  view source on GitHub ↗

upsertEnvFile 原地更新 env 文件中的指定变量,未出现的变量追加到末尾。

(path string, vars map[string]string)

Source from the content-addressed store, hash-verified

137
138// upsertEnvFile 原地更新 env 文件中的指定变量,未出现的变量追加到末尾。
139func upsertEnvFile(path string, vars map[string]string) error {
140 existing, err := os.ReadFile(path)
141 if err != nil && !os.IsNotExist(err) {
142 return err
143 }
144
145 lines := strings.Split(string(existing), "\n")
146 updated := make(map[string]bool)
147 for i, line := range lines {
148 for k, v := range vars {
149 if strings.HasPrefix(line, k+"=") {
150 lines[i] = k + "=" + v
151 updated[k] = true
152 }
153 }
154 }
155 for k, v := range vars {
156 if !updated[k] {
157 lines = append(lines, k+"="+v)
158 }
159 }
160
161 content := strings.Join(lines, "\n")
162 if !strings.HasSuffix(content, "\n") {
163 content += "\n"
164 }
165 return os.WriteFile(path, []byte(content), 0o644)
166}
167
168func readToken(path string) (string, error) {
169 if path == "-" {

Callers 1

writeEnvResultFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected