Call this function with the input args and return the value if it is possible to do so at this time.
(ctx context.Context, args []types.Value)
| 126 | // Call this function with the input args and return the value if it is possible |
| 127 | // to do so at this time. |
| 128 | func (obj *ReadFileFunc) Call(ctx context.Context, args []types.Value) (types.Value, error) { |
| 129 | if len(args) < 1 { |
| 130 | return nil, fmt.Errorf("not enough args") |
| 131 | } |
| 132 | filename := args[0].Str() |
| 133 | |
| 134 | if obj.data == nil { |
| 135 | return nil, funcs.ErrCantSpeculate |
| 136 | } |
| 137 | p := strings.TrimSuffix(obj.data.Base, "/") |
| 138 | if p == obj.data.Base { // didn't trim, so we fail |
| 139 | // programming error |
| 140 | return nil, fmt.Errorf("no trailing slash on Base, got: `%s`", p) |
| 141 | } |
| 142 | path := p |
| 143 | |
| 144 | if !strings.HasPrefix(filename, "/") { |
| 145 | return nil, fmt.Errorf("filename was not absolute, got: `%s`", filename) |
| 146 | //path += "/" // be forgiving ? |
| 147 | } |
| 148 | path += filename |
| 149 | |
| 150 | if obj.init == nil || obj.data == nil { |
| 151 | return nil, funcs.ErrCantSpeculate |
| 152 | } |
| 153 | fs, err := obj.init.World.Fs(ctx, obj.data.FsURI) // open the remote file system |
| 154 | if err != nil { |
| 155 | return nil, errwrap.Wrapf(err, "can't load data from file system `%s`", obj.data.FsURI) |
| 156 | } |
| 157 | // this is relative to the module dir the func is in! |
| 158 | content, err := fs.ReadFile(path) // open the remote file system |
| 159 | // We could use it directly, but it feels like less correct. |
| 160 | //content, err := obj.data.Fs.ReadFile(path) // open the remote file system |
| 161 | if err != nil { |
| 162 | return nil, errwrap.Wrapf(err, "can't read file `%s` (%s)", filename, path) |
| 163 | } |
| 164 | |
| 165 | return &types.StrValue{ |
| 166 | V: string(content), // convert to string |
| 167 | }, nil |
| 168 | } |