GetAPIKey returns the Google geocoding API key stored in the Perkeep configuration directory as google-geocode.key.
()
| 89 | // GetAPIKey returns the Google geocoding API key stored in the Perkeep |
| 90 | // configuration directory as google-geocode.key. |
| 91 | func GetAPIKey() (string, error) { |
| 92 | mu.RLock() |
| 93 | key := apiKey |
| 94 | mu.RUnlock() |
| 95 | if apiKey != "" { |
| 96 | return key, nil |
| 97 | } |
| 98 | mu.Lock() |
| 99 | defer mu.Unlock() |
| 100 | |
| 101 | dir, err := osutil.PerkeepConfigDir() |
| 102 | if err != nil { |
| 103 | return "", err |
| 104 | } |
| 105 | slurp, err := wkfs.ReadFile(filepath.Join(dir, apiKeyName)) |
| 106 | if os.IsNotExist(err) { |
| 107 | return "", ErrNoGoogleKey |
| 108 | } |
| 109 | if err != nil { |
| 110 | return "", err |
| 111 | } |
| 112 | key = strings.TrimSpace(string(slurp)) |
| 113 | if key == "" { |
| 114 | return "", ErrNoGoogleKey |
| 115 | } |
| 116 | apiKey = key |
| 117 | return key, nil |
| 118 | } |
| 119 | |
| 120 | var ErrNoGoogleKey = errors.New("geocode: Google API key not configured, using OpenStreetMap; see https://perkeep.org/doc/geocoding") |
| 121 |
no test coverage detected