Skip to content

Commit dea2702

Browse files
authoredJun 13, 2024··
Merge pull request #28 from thedadams/global-opts-to-env
fix: set global options as environment variables for security
2 parents ab9b8c9 + 5d584bb commit dea2702

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed
 

‎gptscript.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@ func NewGPTScript(opts GlobalOptions) (GPTScript, error) {
4949
defer lock.Unlock()
5050
gptscriptCount++
5151

52-
if serverURL == "" {
52+
disableServer := os.Getenv("GPT_SCRIPT_DISABLE_SERVER") == "true"
53+
54+
if serverURL == "" && disableServer {
5355
serverURL = os.Getenv("GPTSCRIPT_URL")
5456
}
5557

56-
if serverProcessCancel == nil && os.Getenv("GPTSCRIPT_DISABLE_SERVER") != "true" {
58+
if serverProcessCancel == nil && !disableServer {
5759
if serverURL == "" {
5860
l, err := net.Listen("tcp", "127.0.0.1:0")
5961
if err != nil {
@@ -71,7 +73,8 @@ func NewGPTScript(opts GlobalOptions) (GPTScript, error) {
7173
ctx, cancel := context.WithCancel(context.Background())
7274

7375
in, _ := io.Pipe()
74-
serverProcess = exec.CommandContext(ctx, getCommand(), append(opts.toArgs(), "--listen-address", serverURL, "sdkserver")...)
76+
serverProcess = exec.CommandContext(ctx, getCommand(), "--listen-address", serverURL, "sdkserver")
77+
serverProcess.Env = append(os.Environ(), opts.toEnv()...)
7578
serverProcess.Stdin = in
7679

7780
serverProcessCancel = func() {

‎opts.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ type GlobalOptions struct {
88
DefaultModel string `json:"DefaultModel"`
99
}
1010

11-
func (g GlobalOptions) toArgs() []string {
11+
func (g GlobalOptions) toEnv() []string {
1212
var args []string
1313
if g.OpenAIAPIKey != "" {
14-
args = append(args, "--openai-api-key", g.OpenAIAPIKey)
14+
args = append(args, "OPENAI_API_KEY="+g.OpenAIAPIKey)
1515
}
1616
if g.OpenAIBaseURL != "" {
17-
args = append(args, "--openai-base-url", g.OpenAIBaseURL)
17+
args = append(args, "OPENAI_BASE_URL="+g.OpenAIBaseURL)
1818
}
1919
if g.DefaultModel != "" {
20-
args = append(args, "--default-model", g.DefaultModel)
20+
args = append(args, "GPTSCRIPT_DEFAULT_MODEL="+g.DefaultModel)
2121
}
2222

2323
return args

0 commit comments

Comments
 (0)
Please sign in to comment.