locate returns the schema file to use: the file named by the environment variable, a cached copy, or a fresh download into the cache.
(progress io.Writer)
| 63 | // locate returns the schema file to use: the file named by the environment |
| 64 | // variable, a cached copy, or a fresh download into the cache. |
| 65 | func (s schemaSource) locate(progress io.Writer) (string, error) { |
| 66 | if p := os.Getenv(s.envVar); p != "" { |
| 67 | info, err := os.Stat(p) |
| 68 | if err != nil { |
| 69 | return "", fmt.Errorf("%s: %w", s.envVar, err) |
| 70 | } |
| 71 | if info.IsDir() { |
| 72 | return "", fmt.Errorf("%s: %s is a directory", s.envVar, p) |
| 73 | } |
| 74 | return p, nil |
| 75 | } |
| 76 | p, err := s.cachePath() |
| 77 | if err != nil { |
| 78 | return "", err |
| 79 | } |
| 80 | if _, err := os.Stat(p); err == nil { |
| 81 | return p, nil |
| 82 | } else if !errors.Is(err, fs.ErrNotExist) { |
| 83 | return "", err |
| 84 | } |
| 85 | return p, s.download(p, progress) |
| 86 | } |
| 87 | |
| 88 | func (s schemaSource) cachePath() (string, error) { |
| 89 | dir, err := schemaCacheDir() |