disk.go

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