MCPcopy Create free account
hub / github.com/chainreactors/EvilProxy / Save

Method Save

internal/store/gitstore.go:258–347  ·  view source on GitHub ↗

Save persists token storage and metadata to the resolved auth file path.

(_ context.Context, auth *cliproxyauth.Auth)

Source from the content-addressed store, hash-verified

256
257// Save persists token storage and metadata to the resolved auth file path.
258func (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)

Callers

nothing calls this directly

Calls 7

resolveAuthPathMethod · 0.95
EnsureRepositoryMethod · 0.95
relativeToRepoMethod · 0.95
commitAndPushLockedMethod · 0.95
jsonEqualFunction · 0.70
SetMetadataMethod · 0.65
SaveTokenToFileMethod · 0.65

Tested by

no test coverage detected