forked from gptscript-ai/gptscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.go
75 lines (67 loc) · 1.92 KB
/
input.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
63
64
65
66
67
68
69
70
71
72
73
74
75
package input
import (
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/gptscript-ai/gptscript/internal"
"github.com/gptscript-ai/gptscript/pkg/loader"
"github.com/gptscript-ai/gptscript/pkg/types"
)
func FromArgs(args []string) string {
return strings.Join(args, " ")
}
func FromCLI(file string, args []string) (string, error) {
toolInput, err := FromFile(file)
if err != nil || toolInput != "" {
return toolInput, err
}
return FromArgs(args[1:]), nil
}
func FromFile(file string) (string, error) {
if file == "-" {
log.Debugf("reading stdin")
data, err := io.ReadAll(os.Stdin)
if err != nil {
return "", fmt.Errorf("reading stdin: %w", err)
}
return string(data), nil
} else if file != "" {
if s, err := fs.Stat(internal.FS, file); err == nil && s.IsDir() {
for _, ext := range types.DefaultFiles {
if _, err := os.Stat(filepath.Join(file, ext)); err == nil {
file = filepath.Join(file, ext)
break
}
}
}
log.Debugf("reading file %s", file)
data, err := fs.ReadFile(internal.FS, file)
if err != nil {
return "", fmt.Errorf("reading %s: %w", file, err)
}
return string(data), nil
}
return "", nil
}
// FromLocation takes a string that can be a file path or a URL to a file and returns the content of that file.
func FromLocation(s string, disableCache bool) (string, error) {
// Attempt to read the file first, if that fails, try to load the URL. Finally,
// return an error if both fail.
content, err := FromFile(s)
if err != nil {
log.Debugf("failed to read file %s (due to %v) attempting to load the URL...", s, err)
content, err = loader.ContentFromURL(s, disableCache)
if err != nil {
return "", err
}
// If the content is empty and there was no error, this is not a remote file. Return a generic
// error indicating that the file could not be loaded.
if content == "" {
return "", fmt.Errorf("failed to load %v", s)
}
}
return content, nil
}