diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2026-01-19 15:15:18 +0100 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2026-01-20 21:12:57 +0000 |
| commit | 8088ac4199a6e947c38db669c11d4441a9d59581 (patch) | |
| tree | fe7d6a6f5c9a6de0be54a14b40cab448bb80aa59 /pkg/codesearch/codesearch.go | |
| parent | fb714834adfb0e1e36c4cfc7ca288391cfc18986 (diff) | |
pkg/codesearch: add read-file command
Just provides full file contents as last resort.
Diffstat (limited to 'pkg/codesearch/codesearch.go')
| -rw-r--r-- | pkg/codesearch/codesearch.go | 28 |
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 { |
