From 748f9a98852cafb4db0204f6a537a6f5401d92c5 Mon Sep 17 00:00:00 2001 From: Wraient Date: Sun, 27 Oct 2024 18:09:22 +0530 Subject: [PATCH] Change token from cli or rofi --- cmd/curd/main.go | 19 ++++++++----------- internal/config.go | 15 +++++++++++++++ internal/curd.go | 8 ++++---- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/cmd/curd/main.go b/cmd/curd/main.go index 33b28f0..118cf8a 100644 --- a/cmd/curd/main.go +++ b/cmd/curd/main.go @@ -62,6 +62,7 @@ func main() { addNewAnime := flag.Bool("new", false, "Add new anime") rofiSelection := flag.Bool("rofi", false, "Open selection in rofi") noRofi := flag.Bool("no-rofi", false, "No rofi") + changeToken := flag.Bool("change-token", false, "Change token") updateScript := flag.Bool("u", false, "Update the script") editConfig := flag.Bool("e", false, "Edit config") subFlag := flag.Bool("sub", false, "Watch sub version") @@ -69,6 +70,7 @@ func main() { // Custom help/usage function flag.Usage = func() { + internal.RestoreScreen() fmt.Fprintf(os.Stderr, "Curd is a CLI tool to manage anime playback with advanced features like skipping intro, outro, filler, recap, tracking progress, and integrating with Discord.\n") fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0]) flag.PrintDefaults() // This prints the default flag information @@ -91,6 +93,11 @@ func main() { } } + if *changeToken { + internal.ChangeToken(&userCurdConfig, &user) + return + } + if *rofiSelection { userCurdConfig.RofiSelection = true } @@ -117,17 +124,7 @@ func main() { internal.Log("Error reading token", logFile) } if user.Token == "" { - if userCurdConfig.RofiSelection { - user.Token, err = internal.GetTokenFromRofi() - } else { - fmt.Println("No token found, please generate a token from https://anilist.co/api/v2/oauth/authorize?client_id=20686&response_type=token") - fmt.Scanln(&user.Token) - } - if err != nil { - internal.Log("Error getting user input: "+err.Error(), logFile) - internal.ExitCurd(err) - } - internal.WriteTokenToFile(user.Token, filepath.Join(os.ExpandEnv(userCurdConfig.StoragePath), "token")) + internal.ChangeToken(&userCurdConfig, &user) } // Load animes in database diff --git a/internal/config.go b/internal/config.go index 4dc438e..df3f00d 100644 --- a/internal/config.go +++ b/internal/config.go @@ -131,6 +131,21 @@ func createDefaultConfig(path string) error { return nil } +func ChangeToken(config *CurdConfig, user *User) { + var err error + if config.RofiSelection { + user.Token, err = GetTokenFromRofi() + } else { + fmt.Println("Please generate a token from https://anilist.co/api/v2/oauth/authorize?client_id=20686&response_type=token") + fmt.Scanln(&user.Token) + } + if err != nil { + Log("Error getting user input: "+err.Error(), logFile) + ExitCurd(err) + } + WriteTokenToFile(user.Token, filepath.Join(os.ExpandEnv(config.StoragePath), "token")) +} + // Load config file from disk into a map (key=value format) func loadConfigFromFile(path string) (map[string]string, error) { file, err := os.Open(path) diff --git a/internal/curd.go b/internal/curd.go index e192bfb..fd8fed8 100644 --- a/internal/curd.go +++ b/internal/curd.go @@ -256,13 +256,13 @@ func AddNewAnime(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseA user.Id, user.Username, err = GetAnilistUserID(user.Token) if err != nil { Log(fmt.Sprintf("Failed to get user ID: %v", err), logFile) - ExitCurd(fmt.Errorf("Failed to get user ID")) + ExitCurd(fmt.Errorf("Failed to get user ID\nYou can reset the token by running `curd -change-token`")) } } anilistUserData, err := GetUserData(user.Token, user.Id) if err != nil { Log(fmt.Sprintf("Failed to get user data: %v", err), logFile) - ExitCurd(fmt.Errorf("Failed to get user data")) + ExitCurd(fmt.Errorf("Failed to get user ID\nYou can reset the token by running `curd -change-token`")) } user.AnimeList = ParseAnimeList(anilistUserData) } @@ -274,12 +274,12 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni user.Id, user.Username, err = GetAnilistUserID(user.Token) if err != nil { Log(fmt.Sprintf("Failed to get user ID: %v", err), logFile) - ExitCurd(fmt.Errorf("Failed to get user ID")) + ExitCurd(fmt.Errorf("Failed to get user ID\nYou can reset the token by running `curd -change-token`")) } anilistUserData, err := GetUserData(user.Token, user.Id) if err != nil { Log(fmt.Sprintf("Failed to get user data: %v", err), logFile) - ExitCurd(fmt.Errorf("Failed to get user data")) + ExitCurd(fmt.Errorf("Failed to get user ID\nYou can reset the token by running `curd -change-token`")) } user.AnimeList = ParseAnimeList(anilistUserData) animeListMap := GetAnimeMap(user.AnimeList)