SaveAuthCache saves the auth cache to the cache file.
(cache AuthCache)
| 180 | |
| 181 | // SaveAuthCache saves the auth cache to the cache file. |
| 182 | func SaveAuthCache(cache AuthCache) (err error) { |
| 183 | if !cache.changed { |
| 184 | return nil |
| 185 | } |
| 186 | cacheFile := cacheFile() |
| 187 | if cacheFile == "" { |
| 188 | return nil |
| 189 | } |
| 190 | _, err = os.Stat(filepath.Dir(cacheFile)) |
| 191 | if os.IsNotExist(err) { |
| 192 | err = os.MkdirAll(filepath.Dir(cacheFile), 0o700) |
| 193 | } |
| 194 | if err != nil { |
| 195 | return err |
| 196 | } |
| 197 | f, err := os.OpenFile(cacheFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600) |
| 198 | if err != nil { |
| 199 | return err |
| 200 | } |
| 201 | defer func() { |
| 202 | if err == nil { |
| 203 | err = f.Close() |
| 204 | } |
| 205 | if err != nil { |
| 206 | os.Remove(cacheFile) |
| 207 | } |
| 208 | }() |
| 209 | return json.NewEncoder(f).Encode(&cache.data) |
| 210 | } |