Agent not work yet. Need to build Part B first

This commit is contained in:
2026-03-30 22:30:42 +08:00
parent bdbc29649c
commit 3bbd0a00a8
4 changed files with 80 additions and 56 deletions

View File

@@ -0,0 +1,57 @@
package cmd
import (
"bufio"
"fmt"
"os"
"strings"
)
func LoadEnvFile(path string) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
scanner := bufio.NewScanner(f)
lineNum := 0
for scanner.Scan() {
lineNum++
line := strings.TrimSpace(scanner.Text())
if line == "" || strings.HasPrefix(line, "#") {
continue
}
key, val, ok := strings.Cut(line, "=")
if !ok {
return fmt.Errorf("line %d: expected KEY=VALUE", lineNum)
}
key = strings.TrimSpace(key)
val = strings.TrimSpace(val)
if key == "" {
return fmt.Errorf("line %d: empty variable name", lineNum)
}
// Remove matching single or double quotes around the whole value.
if len(val) >= 2 {
if (val[0] == '"' && val[len(val)-1] == '"') || (val[0] == '\'' && val[len(val)-1] == '\'') {
val = val[1 : len(val)-1]
}
}
if err := os.Setenv(key, val); err != nil {
return fmt.Errorf("line %d: set %q: %w", lineNum, key, err)
}
}
if err := scanner.Err(); err != nil {
return err
}
return nil
}