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