Skip to content

Commit fdd9404

Browse files
Allow reading script from stdin using "gptscript -"
1 parent 7f7ef63 commit fdd9404

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

pkg/cli/gptscript.go

+20-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/gptscript-ai/gptscript/pkg/openai"
1919
"github.com/gptscript-ai/gptscript/pkg/runner"
2020
"github.com/gptscript-ai/gptscript/pkg/server"
21+
"github.com/gptscript-ai/gptscript/pkg/types"
2122
"github.com/gptscript-ai/gptscript/pkg/version"
2223
"github.com/spf13/cobra"
2324
"golang.org/x/term"
@@ -125,9 +126,25 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) error {
125126
return cmd.Help()
126127
}
127128

128-
prg, err := loader.Program(cmd.Context(), args[0], r.SubTool)
129-
if err != nil {
130-
return err
129+
var (
130+
prg types.Program
131+
err error
132+
)
133+
134+
if args[0] == "-" {
135+
data, err := io.ReadAll(os.Stdin)
136+
if err != nil {
137+
return err
138+
}
139+
prg, err = loader.ProgramFromSource(cmd.Context(), string(data), r.SubTool)
140+
if err != nil {
141+
return err
142+
}
143+
} else {
144+
prg, err = loader.Program(cmd.Context(), args[0], r.SubTool)
145+
if err != nil {
146+
return err
147+
}
131148
}
132149

133150
if r.Assemble {

pkg/loader/loader.go

+15
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,21 @@ func link(ctx context.Context, prg *types.Program, base *source, tool types.Tool
342342
return tool, nil
343343
}
344344

345+
func ProgramFromSource(ctx context.Context, content, subToolName string) (types.Program, error) {
346+
prg := types.Program{
347+
ToolSet: types.ToolSet{},
348+
}
349+
tool, err := readTool(ctx, &prg, &source{
350+
Content: io.NopCloser(strings.NewReader(content)),
351+
File: "inline",
352+
}, subToolName)
353+
if err != nil {
354+
return types.Program{}, err
355+
}
356+
prg.EntryToolID = tool.ID
357+
return prg, nil
358+
}
359+
345360
func Program(ctx context.Context, name, subToolName string) (types.Program, error) {
346361
prg := types.Program{
347362
ToolSet: types.ToolSet{},

0 commit comments

Comments
 (0)