Skip to content

Commit

Permalink
Change token from cli or rofi
Browse files Browse the repository at this point in the history
  • Loading branch information
Wraient committed Oct 27, 2024
1 parent b0f3a9f commit 748f9a9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
19 changes: 8 additions & 11 deletions cmd/curd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ 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")
dubFlag := flag.Bool("dub", false, "Watch dub version")

// 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
Expand All @@ -91,6 +93,11 @@ func main() {
}
}

if *changeToken {
internal.ChangeToken(&userCurdConfig, &user)
return
}

if *rofiSelection {
userCurdConfig.RofiSelection = true
}
Expand All @@ -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
Expand Down
15 changes: 15 additions & 0 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions internal/curd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down

0 comments on commit 748f9a9

Please sign in to comment.