-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
62 lines (58 loc) · 1.34 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func loadEnv(filename string) error {
if filename == "" {
return fmt.Errorf("must specify filename")
}
bytes, err := os.ReadFile(filename)
if err != nil {
fmt.Println("Error while reading filename '", filename, "' :", err)
return err
}
lines := strings.Split(string(bytes), "\n")
apiKeyFound := false
for _, line := range lines {
splitLine := strings.Split(line, "=")
if len(splitLine) < 2 {
continue
}
variable := splitLine[0]
value := splitLine[1]
if variable == "GEMINI_API_KEY" {
apiKeyFound = true
}
os.Setenv(variable, value)
}
if !apiKeyFound {
return fmt.Errorf("environment variable 'GEMINI_API_KEY' not found on %s", filename)
}
return nil
}
func removeCodeMarks(s string) string {
if strings.HasPrefix(s, "```") {
lines := strings.Split(s, "\n")
if lines[len(lines)-1] == "" {
if len(lines) > 2 && strings.TrimSpace(lines[len(lines)-2]) == "```" {
return strings.Join(lines[1:len(lines)-2], "\n")
}
} else {
if len(lines) > 2 && strings.TrimSpace(lines[len(lines)-1]) == "```" {
return strings.Join(lines[1:len(lines)-1], "\n")
}
}
}
return s
}
func readPrompt(message string) string {
scanner := bufio.NewScanner(os.Stdin)
fmt.Print(message)
if !scanner.Scan() {
return ""
}
return strings.TrimSpace(scanner.Text())
}