disk.go 799 B

123456789101112131415161718192021222324252627282930313233343536
  1. package copilot
  2. import (
  3. "encoding/json"
  4. "os"
  5. "path/filepath"
  6. "runtime"
  7. )
  8. func RefreshTokenFromDisk() (string, bool) {
  9. data, err := os.ReadFile(tokenFilePath())
  10. if err != nil {
  11. return "", false
  12. }
  13. var content map[string]struct {
  14. User string `json:"user"`
  15. OAuthToken string `json:"oauth_token"`
  16. GitHubAppID string `json:"githubAppId"`
  17. }
  18. if err := json.Unmarshal(data, &content); err != nil {
  19. return "", false
  20. }
  21. if app, ok := content["github.com:Iv1.b507a08c87ecfe98"]; ok {
  22. return app.OAuthToken, true
  23. }
  24. return "", false
  25. }
  26. func tokenFilePath() string {
  27. switch runtime.GOOS {
  28. case "windows":
  29. return filepath.Join(os.Getenv("LOCALAPPDATA"), "github-copilot/apps.json")
  30. default:
  31. return filepath.Join(os.Getenv("HOME"), ".config/github-copilot/apps.json")
  32. }
  33. }