internal/rofi.go

56 lines · 1459 bytes

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