Skip to content

Commit 21f0fea

Browse files
authored
Merge pull request #461 from tylerslaton/parse-remote
feat: add support for remote file parsing
2 parents f458945 + 5b0c439 commit 21f0fea

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

pkg/cli/parse.go

+21-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package cli
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"os"
67
"strings"
78

89
"github.com/gptscript-ai/gptscript/pkg/input"
10+
"github.com/gptscript-ai/gptscript/pkg/loader"
911
"github.com/gptscript-ai/gptscript/pkg/parser"
1012
"github.com/spf13/cobra"
1113
)
@@ -26,12 +28,28 @@ func locationName(l string) string {
2628
}
2729

2830
func (e *Parse) Run(_ *cobra.Command, args []string) error {
29-
input, err := input.FromFile(args[0])
31+
var (
32+
content string
33+
err error
34+
)
35+
36+
// Attempt to read the file first, if that fails, try to load the URL. Finally,
37+
// return an error if both fail.
38+
content, err = input.FromFile(args[0])
3039
if err != nil {
31-
return err
40+
log.Debugf("failed to read file %s (due to %v) attempting to load the URL...", args[0], err)
41+
content, err = loader.ContentFromURL(args[0])
42+
if err != nil {
43+
return err
44+
}
45+
// If the content is empty and there was no error, this is not a remote file. Return a generic
46+
// error indicating that the file could not be loaded.
47+
if content == "" {
48+
return fmt.Errorf("failed to load %v", args[0])
49+
}
3250
}
3351

34-
docs, err := parser.Parse(strings.NewReader(input), parser.Options{
52+
docs, err := parser.Parse(strings.NewReader(content), parser.Options{
3553
Location: locationName(args[0]),
3654
})
3755
if err != nil {

pkg/loader/url.go

+18
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,21 @@ func loadURL(ctx context.Context, cache *cache.Client, base *source, name string
137137

138138
return result, true, nil
139139
}
140+
141+
func ContentFromURL(url string) (string, error) {
142+
cache, err := cache.New()
143+
if err != nil {
144+
return "", fmt.Errorf("failed to create cache: %w", err)
145+
}
146+
147+
source, ok, err := loadURL(context.Background(), cache, &source{}, url)
148+
if err != nil {
149+
return "", fmt.Errorf("failed to load %s: %w", url, err)
150+
}
151+
152+
if !ok {
153+
return "", nil
154+
}
155+
156+
return string(source.Content), nil
157+
}

0 commit comments

Comments
 (0)