internal/rofi.go
61 lines · 1641 bytes
1package internal
2
3import (
4 "bytes"
5 "fmt"
6 "os"
7 "os/exec"
8 "path/filepath"
9 "strings"
10)
11
12func GetTokenFromRofi() (string, error) {
13 // The URL to open - using authorization code flow for consistency
14 url := "https://anilist.co/api/v2/oauth/authorize?client_id=20686&response_type=code&redirect_uri=http://localhost:8000/oauth/callback"
15
16 // Use rofi to display a prompt with the URL
17 message := "Press enter to open the anilist token page in your browser"
18 _, err := GetUserInputFromRofi(message)
19 if err != nil {
20 return "", err
21 }
22
23 // Open the URL in the default browser
24 err = exec.Command("xdg-open", url).Start()
25 if err != nil {
26 return "", err
27 }
28
29 // Use rofi again to get the token input from the user
30 token, err := GetUserInputFromRofi("Enter the token (from the redirect URL after 'access_token=')")
31 if err != nil {
32 return "", err
33 }
34
35 return token, nil
36}
37
38// GetUserInputFromRofi prompts the user for input using Rofi with a custom message
39func GetUserInputFromRofi(message string) (string, error) {
40 userCurdConfig := GetGlobalConfig()
41 if userCurdConfig.StoragePath == "" {
42 userCurdConfig.StoragePath = os.ExpandEnv("${HOME}/.local/share/curd")
43 }
44 // Create the Rofi command
45 cmd := exec.Command("rofi", "-dmenu", "-theme", filepath.Join(os.ExpandEnv(userCurdConfig.StoragePath), "userinput.rasi"), "-p", "Input", "-mesg", message)
46
47 // Set up pipes for output
48 var out bytes.Buffer
49 cmd.Stdout = &out
50
51 // Run the command
52 err := cmd.Run()
53 if err != nil {
54 return "", fmt.Errorf("failed to run Rofi: %w", err)
55 }
56
57 // Get the entered input
58 userInput := strings.TrimSpace(out.String())
59
60 return userInput, nil
61}