pkger/parser/open.go

82 lines
1.3 KiB
Go
Raw Normal View History

2019-10-21 17:27:04 +03:00
package parser
import (
2019-10-22 21:50:48 +03:00
"encoding/json"
2019-10-21 17:27:04 +03:00
"go/token"
"os"
"path/filepath"
)
var _ Decl = OpenDecl{}
type OpenDecl struct {
file *File
pos token.Position
2019-10-21 17:27:04 +03:00
value string
}
2019-10-23 20:19:39 +03:00
func (d OpenDecl) String() string {
b, _ := json.Marshal(d)
return string(b)
}
2019-10-22 21:50:48 +03:00
func (d OpenDecl) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": "pkger.Open",
"file": d.file,
"pos": d.pos,
"value": d.value,
})
}
2019-10-21 17:27:04 +03:00
func (d OpenDecl) File() (*File, error) {
if d.file == nil {
return nil, os.ErrNotExist
}
return d.file, nil
}
func (d OpenDecl) Position() (token.Position, error) {
2019-10-21 17:27:04 +03:00
return d.pos, nil
}
func (d OpenDecl) Value() (string, error) {
if d.value == "" {
return "", os.ErrNotExist
}
return d.value, nil
}
2019-10-23 20:19:39 +03:00
func (d OpenDecl) Files(virtual map[string]string) ([]*File, error) {
if _, ok := virtual[d.value]; ok {
return nil, nil
}
2019-10-21 17:27:04 +03:00
2019-11-01 22:53:54 +03:00
her := d.file.Here
pt := d.file.Path
2019-10-21 17:27:04 +03:00
2019-10-30 18:31:39 +03:00
fp := filepath.Join(her.Module.Dir, pt.Name)
2019-10-21 17:27:04 +03:00
osf, err := os.Stat(fp)
if err != nil {
return nil, err
}
if osf.IsDir() {
wd := WalkDecl{
file: d.file,
pos: d.pos,
value: d.value,
}
2019-10-23 20:19:39 +03:00
return wd.Files(virtual)
2019-10-21 17:27:04 +03:00
}
var files []*File
files = append(files, &File{
2019-10-30 18:31:39 +03:00
Abs: filepath.Join(her.Module.Dir, pt.Name),
2019-10-21 17:27:04 +03:00
Path: pt,
Here: her,
})
return files, nil
}