aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/codesearch/codesearch.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/codesearch/codesearch.go')
-rw-r--r--pkg/codesearch/codesearch.go28
1 files changed, 28 insertions, 0 deletions
diff --git a/pkg/codesearch/codesearch.go b/pkg/codesearch/codesearch.go
index 8e0259af7..96ee1c696 100644
--- a/pkg/codesearch/codesearch.go
+++ b/pkg/codesearch/codesearch.go
@@ -45,6 +45,13 @@ var Commands = []Command{
}
return b.String(), nil
}},
+ {"read-file", 1, func(index *Index, args []string) (string, error) {
+ ok, contents, err := index.ReadFile(args[0])
+ if err != nil || !ok {
+ return notFound, err
+ }
+ return contents, nil
+ }},
{"file-index", 1, func(index *Index, args []string) (string, error) {
ok, entities, err := index.FileIndex(args[0])
if err != nil || !ok {
@@ -137,6 +144,27 @@ func (index *Index) DirIndex(dir string) (bool, []string, []string, error) {
return exists, subdirs, files, nil
}
+func (index *Index) ReadFile(file string) (bool, string, error) {
+ if err := escaping(file); err != nil {
+ return false, "", nil
+ }
+ for _, dir := range index.srcDirs {
+ data, err := os.ReadFile(filepath.Join(dir, file))
+ if err != nil {
+ if os.IsNotExist(err) {
+ continue
+ }
+ var errno syscall.Errno
+ if errors.As(err, &errno) && errno == syscall.EISDIR {
+ return false, "", nil
+ }
+ return false, "", err
+ }
+ return true, string(data), nil
+ }
+ return false, "", nil
+}
+
func (index *Index) FileIndex(file string) (bool, []Entity, error) {
var entities []Entity
for _, def := range index.db.Definitions {