Commit c0ef501ff2

c0ef501ff2458c81aea5009bacbec93f514d82d3

parent: 6575139a3f

Unsigned

Wraient <wraient.911@gmail.com> · 2024-12-30 10:23 UTC

Fix: Better token installer for windows

Layout: unified · split

internal/config.go +42 −2
@@ -138,17 +138,57 @@ func createDefaultConfig(path string) error {
138138
139139func ChangeToken(config *CurdConfig, user *User) {
140140 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 {
142181 user.Token, err = GetTokenFromRofi()
143182 } else {
144183 fmt.Println("Please generate a token from https://anilist.co/api/v2/oauth/authorize?client_id=20686&response_type=token")
145184 fmt.Scanln(&user.Token)
146185 }
186
147187 if err != nil {
148188 Log("Error getting user input: "+err.Error(), logFile)
149189 ExitCurd(err)
150190 }
151 WriteTokenToFile(user.Token, filepath.Join(os.ExpandEnv(config.StoragePath), "token"))
191 WriteTokenToFile(user.Token, tokenPath)
152192}
153193
154194// Load config file from disk into a map (key=value format)