Commit c0ef501ff2
Unsigned
Layout: unified · split
internal/config.go +42 −2
| @@ -138,17 +138,57 @@ func createDefaultConfig(path string) error { | |||
| 138 | 138 | ||
| 139 | func ChangeToken(config *CurdConfig, user *User) { | 139 | func ChangeToken(config *CurdConfig, user *User) { |
| 140 | var err error | 140 | var err error |
| 141 | if config.RofiSelection { | 141 | tokenPath := filepath.Join(os.ExpandEnv(config.StoragePath), "token") |
| 142 | |||
| 143 | if runtime.GOOS == "windows" { | ||
| 144 | // Create a temporary file for the token | ||
| 145 | tempFile, err := os.CreateTemp("", "curd-token-*.txt") | ||
| 146 | if err != nil { | ||
| 147 | Log("Error creating temp file: "+err.Error(), logFile) | ||
| 148 | ExitCurd(err) | ||
| 149 | } | ||
| 150 | tempPath := tempFile.Name() | ||
| 151 | tempFile.Close() | ||
| 152 | |||
| 153 | // Write instructions to the temp file | ||
| 154 | instructions := "Please generate a token from https://anilist.co/api/v2/oauth/authorize?client_id=20686&response_type=token\n" + | ||
| 155 | "Replace this text with your token and save the file.\n" | ||
| 156 | if err := os.WriteFile(tempPath, []byte(instructions), 0644); err != nil { | ||
| 157 | Log("Error writing instructions: "+err.Error(), logFile) | ||
| 158 | ExitCurd(err) | ||
| 159 | } | ||
| 160 | |||
| 161 | // Open notepad with the temp file | ||
| 162 | cmd := exec.Command("notepad.exe", tempPath) | ||
| 163 | if err := cmd.Run(); err != nil { | ||
| 164 | Log("Error opening notepad: "+err.Error(), logFile) | ||
| 165 | ExitCurd(err) | ||
| 166 | } | ||
| 167 | |||
| 168 | // Read the token from the file | ||
| 169 | content, err := os.ReadFile(tempPath) | ||
| 170 | if err != nil { | ||
| 171 | Log("Error reading token: "+err.Error(), logFile) | ||
| 172 | ExitCurd(err) | ||
| 173 | } | ||
| 174 | |||
| 175 | // Clean up the temp file | ||
| 176 | os.Remove(tempPath) | ||
| 177 | |||
| 178 | // Extract token (remove instructions and whitespace) | ||
| 179 | user.Token = strings.TrimSpace(string(content)) | ||
| 180 | } else if config.RofiSelection { | ||
| 142 | user.Token, err = GetTokenFromRofi() | 181 | user.Token, err = GetTokenFromRofi() |
| 143 | } else { | 182 | } else { |
| 144 | fmt.Println("Please generate a token from https://anilist.co/api/v2/oauth/authorize?client_id=20686&response_type=token") | 183 | fmt.Println("Please generate a token from https://anilist.co/api/v2/oauth/authorize?client_id=20686&response_type=token") |
| 145 | fmt.Scanln(&user.Token) | 184 | fmt.Scanln(&user.Token) |
| 146 | } | 185 | } |
| 186 | |||
| 147 | if err != nil { | 187 | if err != nil { |
| 148 | Log("Error getting user input: "+err.Error(), logFile) | 188 | Log("Error getting user input: "+err.Error(), logFile) |
| 149 | ExitCurd(err) | 189 | ExitCurd(err) |
| 150 | } | 190 | } |
| 151 | WriteTokenToFile(user.Token, filepath.Join(os.ExpandEnv(config.StoragePath), "token")) | 191 | WriteTokenToFile(user.Token, tokenPath) |
| 152 | } | 192 | } |
| 153 | 193 | ||
| 154 | // Load config file from disk into a map (key=value format) | 194 | // Load config file from disk into a map (key=value format) |