Save persists token storage and metadata to the resolved auth file path.
(_ context.Context, auth *cliproxyauth.Auth)
| 256 | |
| 257 | // Save persists token storage and metadata to the resolved auth file path. |
| 258 | func (s *GitTokenStore) Save(_ context.Context, auth *cliproxyauth.Auth) (string, error) { |
| 259 | if auth == nil { |
| 260 | return "", fmt.Errorf("auth filestore: auth is nil") |
| 261 | } |
| 262 | |
| 263 | path, err := s.resolveAuthPath(auth) |
| 264 | if err != nil { |
| 265 | return "", err |
| 266 | } |
| 267 | if path == "" { |
| 268 | return "", fmt.Errorf("auth filestore: missing file path attribute for %s", auth.ID) |
| 269 | } |
| 270 | |
| 271 | if auth.Disabled { |
| 272 | if _, statErr := os.Stat(path); os.IsNotExist(statErr) { |
| 273 | return "", nil |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | if err = s.EnsureRepository(); err != nil { |
| 278 | return "", err |
| 279 | } |
| 280 | |
| 281 | s.mu.Lock() |
| 282 | defer s.mu.Unlock() |
| 283 | |
| 284 | if err = os.MkdirAll(filepath.Dir(path), 0o700); err != nil { |
| 285 | return "", fmt.Errorf("auth filestore: create dir failed: %w", err) |
| 286 | } |
| 287 | |
| 288 | switch { |
| 289 | case auth.Storage != nil: |
| 290 | if auth.Metadata == nil { |
| 291 | auth.Metadata = make(map[string]any) |
| 292 | } |
| 293 | auth.Metadata["disabled"] = auth.Disabled |
| 294 | if setter, ok := auth.Storage.(interface{ SetMetadata(map[string]any) }); ok { |
| 295 | setter.SetMetadata(auth.Metadata) |
| 296 | } |
| 297 | if err = auth.Storage.SaveTokenToFile(path); err != nil { |
| 298 | return "", err |
| 299 | } |
| 300 | case auth.Metadata != nil: |
| 301 | auth.Metadata["disabled"] = auth.Disabled |
| 302 | raw, errMarshal := json.Marshal(auth.Metadata) |
| 303 | if errMarshal != nil { |
| 304 | return "", fmt.Errorf("auth filestore: marshal metadata failed: %w", errMarshal) |
| 305 | } |
| 306 | if existing, errRead := os.ReadFile(path); errRead == nil { |
| 307 | if jsonEqual(existing, raw) { |
| 308 | return path, nil |
| 309 | } |
| 310 | } else if !os.IsNotExist(errRead) { |
| 311 | return "", fmt.Errorf("auth filestore: read existing failed: %w", errRead) |
| 312 | } |
| 313 | tmp := path + ".tmp" |
| 314 | if errWrite := os.WriteFile(tmp, raw, 0o600); errWrite != nil { |
| 315 | return "", fmt.Errorf("auth filestore: write temp failed: %w", errWrite) |
nothing calls this directly
no test coverage detected