| @@ -177,59 +177,74 @@ func createDefaultConfig(path string) error { |
| 177 | return nil |
177 | return nil |
| 178 | } |
178 | } |
| 179 | |
179 | |
| 180 | func ChangeToken(config *CurdConfig, user *User) { |
180 | func getTokenFromTempFile(isWindowsPlatform bool) (string, error) { |
| 181 | var err error |
181 | // Create a temporary file for the token |
| 182 | tokenPath := filepath.Join(os.ExpandEnv(config.StoragePath), "token") |
182 | tempFile, err := os.CreateTemp("", "curd-token-*.txt") |
| 183 | |
183 | if err != nil { |
| 184 | if runtime.GOOS == "windows" { |
184 | return "", fmt.Errorf("error creating temp file: %v", err) |
| 185 | // Create a temporary file for the token |
185 | } |
| 186 | tempFile, err := os.CreateTemp("", "curd-token-*.txt") |
186 | tempPath := tempFile.Name() |
| 187 | if err != nil { |
187 | tempFile.Close() |
| 188 | Log("Error creating temp file: "+err.Error(), logFile) |
188 | |
| 189 | ExitCurd(err) |
189 | // Write instructions to the temp file |
| 190 | } |
190 | instructions := "Please generate a token from https://anilist.co/api/v2/oauth/authorize?client_id=20686&response_type=token\n" + |
| 191 | tempPath := tempFile.Name() |
191 | "Replace this text with your token and save the file.\n" |
| 192 | tempFile.Close() |
192 | if err := os.WriteFile(tempPath, []byte(instructions), 0644); err != nil { |
| 193 | |
193 | return "", fmt.Errorf("error writing instructions: %v", err) |
| 194 | // Write instructions to the temp file |
194 | } |
| 195 | instructions := "Please generate a token from https://anilist.co/api/v2/oauth/authorize?client_id=20686&response_type=token\n" + |
195 | |
| 196 | "Replace this text with your token and save the file.\n" |
196 | // Open the file with appropriate editor |
| 197 | if err := os.WriteFile(tempPath, []byte(instructions), 0644); err != nil { |
197 | var cmd *exec.Cmd |
| 198 | Log("Error writing instructions: "+err.Error(), logFile) |
198 | if isWindowsPlatform { |
| 199 | ExitCurd(err) |
199 | cmd = exec.Command("notepad.exe", tempPath) |
| 200 | } |
200 | } else { |
| 201 | |
201 | editor := os.Getenv("EDITOR") |
| 202 | // Open notepad with the temp file |
202 | if editor == "" { |
| 203 | cmd := exec.Command("notepad.exe", tempPath) |
203 | editor = "nano" // Default to nano if $EDITOR is not set |
| 204 | if err := cmd.Run(); err != nil { |
204 | } |
| 205 | Log("Error opening notepad: "+err.Error(), logFile) |
205 | cmd = exec.Command(editor, tempPath) |
| 206 | ExitCurd(err) |
206 | cmd.Stdin = os.Stdin |
| 207 | } |
207 | cmd.Stdout = os.Stdout |
| 208 | |
208 | cmd.Stderr = os.Stderr |
| 209 | // Read the token from the file |
209 | } |
| 210 | content, err := os.ReadFile(tempPath) |
210 | |
| 211 | if err != nil { |
211 | if err := cmd.Run(); err != nil { |
| 212 | Log("Error reading token: "+err.Error(), logFile) |
212 | return "", fmt.Errorf("error opening editor: %v", err) |
| 213 | ExitCurd(err) |
213 | } |
| 214 | } |
214 | |
| 215 | |
215 | // Read the token from the file |
| 216 | // Clean up the temp file |
216 | content, err := os.ReadFile(tempPath) |
| 217 | os.Remove(tempPath) |
217 | if err != nil { |
| 218 | |
218 | return "", fmt.Errorf("error reading token: %v", err) |
| 219 | // Extract token (remove instructions and whitespace) |
219 | } |
| 220 | user.Token = strings.TrimSpace(string(content)) |
220 | |
| 221 | } else if config.RofiSelection { |
221 | // Clean up the temp file |
| 222 | user.Token, err = GetTokenFromRofi() |
222 | os.Remove(tempPath) |
| 223 | } else { |
223 | |
| 224 | fmt.Println("Please generate a token from https://anilist.co/api/v2/oauth/authorize?client_id=20686&response_type=token") |
224 | // Extract token (remove instructions and whitespace) |
| 225 | fmt.Scanln(&user.Token) |
225 | return strings.TrimSpace(string(content)), nil |
| 226 | } |
226 | } |
| 227 | |
227 | |
| 228 | if err != nil { |
228 | func ChangeToken(config *CurdConfig, user *User) { |
| 229 | Log("Error getting user input: "+err.Error(), logFile) |
229 | var err error |
| 230 | ExitCurd(err) |
230 | tokenPath := filepath.Join(os.ExpandEnv(config.StoragePath), "token") |
| 231 | } |
231 | |
| 232 | WriteTokenToFile(user.Token, tokenPath) |
232 | switch { |
| |
233 | case runtime.GOOS == "darwin" || runtime.GOOS == "windows": |
| |
234 | user.Token, err = getTokenFromTempFile(runtime.GOOS == "windows") |
| |
235 | case config.RofiSelection: |
| |
236 | user.Token, err = GetTokenFromRofi() |
| |
237 | default: |
| |
238 | fmt.Println("Please generate a token from https://anilist.co/api/v2/oauth/authorize?client_id=20686&response_type=token") |
| |
239 | fmt.Scanln(&user.Token) |
| |
240 | } |
| |
241 | |
| |
242 | if err != nil { |
| |
243 | Log("Error getting user input: "+err.Error(), logFile) |
| |
244 | ExitCurd(err) |
| |
245 | } |
| |
246 | |
| |
247 | WriteTokenToFile(user.Token, tokenPath) |
| 233 | } |
248 | } |
| 234 | |
249 | |
| 235 | // Load config file from disk into a map (key=value format) |
250 | // Load config file from disk into a map (key=value format) |