Skip to content

Commit

Permalink
Merge pull request #554 from cloudnautique/add_envfile_support
Browse files Browse the repository at this point in the history
enhance: add support for gptscript.env file in workspace
  • Loading branch information
cloudnautique authored Jun 26, 2024
2 parents 7c6f5c3 + f1c0282 commit c8936c6
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions pkg/builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,11 @@ func SysExec(_ context.Context, env []string, input string, progress chan<- stri
}
combined = io.MultiWriter(&out, &pw)
)

if envvars, err := getWorkspaceEnvFileContents(env); err == nil {
env = append(env, envvars...)
}

cmd.Env = env
cmd.Dir = params.Directory
cmd.Stdout = combined
Expand All @@ -355,6 +360,43 @@ func (pw *progressWriter) Write(p []byte) (n int, err error) {
return len(p), nil
}

func getWorkspaceEnvFileContents(envs []string) ([]string, error) {
dir, err := getWorkspaceDir(envs)
if err != nil {
return nil, err
}

file := filepath.Join(dir, "gptscript.env")

// Lock the file to prevent concurrent writes from other tool calls.
locker.RLock(file)
defer locker.RUnlock(file)

// This is optional, so no errors are returned if the file does not exist.
log.Debugf("Reading file %s", file)
data, err := os.ReadFile(file)
if errors.Is(err, fs.ErrNotExist) {
log.Debugf("The file %s does not exist", file)
return []string{}, nil
} else if err != nil {
log.Debugf("Failed to read file %s: %v", file, err.Error())
return []string{}, nil
}

lines := strings.Split(string(data), "\n")
var envContents []string

for _, line := range lines {
line = strings.TrimSpace(line)
if strings.Contains(line, "=") {
envContents = append(envContents, line)
}
}

return envContents, nil

}

func getWorkspaceDir(envs []string) (string, error) {
for _, env := range envs {
dir, ok := strings.CutPrefix(env, "GPTSCRIPT_WORKSPACE_DIR=")
Expand Down

0 comments on commit c8936c6

Please sign in to comment.