edit, provider: stop inline comments leaking into dotenv values (#439)
* edit,provider: stop reading inline comments as part of dotenv values * provider: read Google env values through edit.GetEnvFile
This commit is contained in:
@@ -20,7 +20,11 @@ func GetEnvFile(path, key string) (string, bool) {
|
||||
if v, ok := unquote(m[2]); ok {
|
||||
return v, true
|
||||
}
|
||||
return m[2], true
|
||||
v := m[2]
|
||||
if i := strings.IndexByte(v, '#'); i >= 0 {
|
||||
v = strings.TrimSpace(v[:i])
|
||||
}
|
||||
return v, true
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package edit
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestEnvInlineComments(t *testing.T) {
|
||||
for _, tc := range []struct{ line, want string }{{"GEMINI_API_KEY=abc123 # my key\n", "abc123"}, {"GEMINI_API_KEY=abc123#comment\n", "abc123"}, {"export GEMINI_API_KEY='abc#123' # my key\n", "abc#123"}, {"GEMINI_API_KEY=\"abc#123\" # my key\n", "abc#123"}, {"GEMINI_API_KEY= # none\n", ""}} {
|
||||
p := tmpFile(t, ".env", tc.line)
|
||||
got, ok := GetEnvFile(p, "GEMINI_API_KEY")
|
||||
if !ok || got != tc.want {
|
||||
t.Fatalf("line %q: got %q %v, want %q", tc.line, got, ok, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -35,6 +35,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/catalog"
|
||||
"github.com/yetone/magpie/internal/edit"
|
||||
)
|
||||
|
||||
// CodeAssist is Google's Code Assist API, which magpie only speaks
|
||||
@@ -181,28 +182,8 @@ func googleClientOf(idToken string) string {
|
||||
|
||||
// envFileValue is one KEY=value of a dotenv file.
|
||||
func envFileValue(path, key string) string {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
defer f.Close()
|
||||
sc := bufio.NewScanner(f)
|
||||
for sc.Scan() {
|
||||
line := strings.TrimSpace(sc.Text())
|
||||
line = strings.TrimPrefix(line, "export ")
|
||||
k, v, ok := strings.Cut(line, "=")
|
||||
if !ok || strings.TrimSpace(k) != key {
|
||||
continue
|
||||
}
|
||||
v = strings.TrimSpace(v)
|
||||
if uq, err := strconv.Unquote(v); err == nil {
|
||||
v = uq
|
||||
} else {
|
||||
v = strings.Trim(v, `'`)
|
||||
}
|
||||
return v
|
||||
}
|
||||
return ""
|
||||
v, _ := edit.GetEnvFile(path, key)
|
||||
return v
|
||||
}
|
||||
|
||||
// savedGoogleProject is the project the user named for the agent's own
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGoogleEnvInlineComment(t *testing.T) {
|
||||
p := filepath.Join(t.TempDir(), ".env")
|
||||
os.WriteFile(p, []byte("GOOGLE_CLOUD_PROJECT=my-project-123 # work project\n"), 0o600)
|
||||
if got := envFileValue(p, "GOOGLE_CLOUD_PROJECT"); got != "my-project-123" {
|
||||
t.Fatalf("got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvFileValueQuotedInlineComment(t *testing.T) {
|
||||
for _, tc := range []struct{ name, line, want string }{
|
||||
{"double quoted", `GOOGLE_CLOUD_PROJECT="my-proj" # work`, "my-proj"},
|
||||
{"single quoted hash", `GOOGLE_CLOUD_PROJECT='a#b' # c`, "a#b"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
p := filepath.Join(t.TempDir(), ".env")
|
||||
if err := os.WriteFile(p, []byte(tc.line+"\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := envFileValue(p, "GOOGLE_CLOUD_PROJECT"); got != tc.want {
|
||||
t.Fatalf("envFileValue = %q, want %q", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user