| 1 | module retrievalfixtures |
| 2 | |
| 3 | import os |
| 4 | |
| 5 | pub struct Reply { |
| 6 | pub: |
| 7 | status int = -1 |
| 8 | data []u8 |
| 9 | } |
| 10 | |
| 11 | pub struct Call { |
| 12 | pub: |
| 13 | url string |
| 14 | filename string |
| 15 | force bool |
| 16 | trailing_slash bool |
| 17 | } |
| 18 | |
| 19 | pub struct Plan { |
| 20 | pub mut: |
| 21 | replies map[string]Reply |
| 22 | calls []Call |
| 23 | } |
| 24 | |
| 25 | pub fn (mut plan Plan) fetch(url string, directory string, filename string, force bool) int { |
| 26 | plan.calls << Call{ url: url, filename: filename, force: force, trailing_slash: directory.ends_with('/') } |
| 27 | reply := plan.replies[url] or { return -1 } |
| 28 | if reply.status == 0 { |
| 29 | os.write_file_array(os.join_path(directory, filename), reply.data) or { |
| 30 | eprintln(err) |
| 31 | return -1 |
| 32 | } |
| 33 | } |
| 34 | return reply.status |
| 35 | } |
| 36 | |