Commit 748f9a9885
Unsigned
Layout: unified · split
cmd/curd/main.go +8 −11
| @@ -62,6 +62,7 @@ func main() { | |||
| 62 | addNewAnime := flag.Bool("new", false, "Add new anime") | 62 | addNewAnime := flag.Bool("new", false, "Add new anime") |
| 63 | rofiSelection := flag.Bool("rofi", false, "Open selection in rofi") | 63 | rofiSelection := flag.Bool("rofi", false, "Open selection in rofi") |
| 64 | noRofi := flag.Bool("no-rofi", false, "No rofi") | 64 | noRofi := flag.Bool("no-rofi", false, "No rofi") |
| 65 | changeToken := flag.Bool("change-token", false, "Change token") | ||
| 65 | updateScript := flag.Bool("u", false, "Update the script") | 66 | updateScript := flag.Bool("u", false, "Update the script") |
| 66 | editConfig := flag.Bool("e", false, "Edit config") | 67 | editConfig := flag.Bool("e", false, "Edit config") |
| 67 | subFlag := flag.Bool("sub", false, "Watch sub version") | 68 | subFlag := flag.Bool("sub", false, "Watch sub version") |
| @@ -69,6 +70,7 @@ func main() { | |||
| 69 | 70 | ||
| 70 | // Custom help/usage function | 71 | // Custom help/usage function |
| 71 | flag.Usage = func() { | 72 | flag.Usage = func() { |
| 73 | internal.RestoreScreen() | ||
| 72 | 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") | 74 | 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") |
| 73 | fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0]) | 75 | fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0]) |
| 74 | flag.PrintDefaults() // This prints the default flag information | 76 | flag.PrintDefaults() // This prints the default flag information |
| @@ -91,6 +93,11 @@ func main() { | |||
| 91 | } | 93 | } |
| 92 | } | 94 | } |
| 93 | 95 | ||
| 96 | if *changeToken { | ||
| 97 | internal.ChangeToken(&userCurdConfig, &user) | ||
| 98 | return | ||
| 99 | } | ||
| 100 | |||
| 94 | if *rofiSelection { | 101 | if *rofiSelection { |
| 95 | userCurdConfig.RofiSelection = true | 102 | userCurdConfig.RofiSelection = true |
| 96 | } | 103 | } |
| @@ -117,17 +124,7 @@ func main() { | |||
| 117 | internal.Log("Error reading token", logFile) | 124 | internal.Log("Error reading token", logFile) |
| 118 | } | 125 | } |
| 119 | if user.Token == "" { | 126 | if user.Token == "" { |
| 120 | if userCurdConfig.RofiSelection { | 127 | internal.ChangeToken(&userCurdConfig, &user) |
| 121 | user.Token, err = internal.GetTokenFromRofi() | ||
| 122 | } else { | ||
| 123 | fmt.Println("No token found, please generate a token from https://anilist.co/api/v2/oauth/authorize?client_id=20686&response_type=token") | ||
| 124 | fmt.Scanln(&user.Token) | ||
| 125 | } | ||
| 126 | if err != nil { | ||
| 127 | internal.Log("Error getting user input: "+err.Error(), logFile) | ||
| 128 | internal.ExitCurd(err) | ||
| 129 | } | ||
| 130 | internal.WriteTokenToFile(user.Token, filepath.Join(os.ExpandEnv(userCurdConfig.StoragePath), "token")) | ||
| 131 | } | 128 | } |
| 132 | 129 | ||
| 133 | // Load animes in database | 130 | // Load animes in database |
internal/config.go +15
| @@ -131,6 +131,21 @@ func createDefaultConfig(path string) error { | |||
| 131 | return nil | 131 | return nil |
| 132 | } | 132 | } |
| 133 | 133 | ||
| 134 | func ChangeToken(config *CurdConfig, user *User) { | ||
| 135 | var err error | ||
| 136 | if config.RofiSelection { | ||
| 137 | user.Token, err = GetTokenFromRofi() | ||
| 138 | } else { | ||
| 139 | fmt.Println("Please generate a token from https://anilist.co/api/v2/oauth/authorize?client_id=20686&response_type=token") | ||
| 140 | fmt.Scanln(&user.Token) | ||
| 141 | } | ||
| 142 | if err != nil { | ||
| 143 | Log("Error getting user input: "+err.Error(), logFile) | ||
| 144 | ExitCurd(err) | ||
| 145 | } | ||
| 146 | WriteTokenToFile(user.Token, filepath.Join(os.ExpandEnv(config.StoragePath), "token")) | ||
| 147 | } | ||
| 148 | |||
| 134 | // Load config file from disk into a map (key=value format) | 149 | // Load config file from disk into a map (key=value format) |
| 135 | func loadConfigFromFile(path string) (map[string]string, error) { | 150 | func loadConfigFromFile(path string) (map[string]string, error) { |
| 136 | file, err := os.Open(path) | 151 | file, err := os.Open(path) |
internal/curd.go +4 −4
| @@ -256,13 +256,13 @@ func AddNewAnime(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseA | |||
| 256 | user.Id, user.Username, err = GetAnilistUserID(user.Token) | 256 | user.Id, user.Username, err = GetAnilistUserID(user.Token) |
| 257 | if err != nil { | 257 | if err != nil { |
| 258 | Log(fmt.Sprintf("Failed to get user ID: %v", err), logFile) | 258 | Log(fmt.Sprintf("Failed to get user ID: %v", err), logFile) |
| 259 | ExitCurd(fmt.Errorf("Failed to get user ID")) | 259 | ExitCurd(fmt.Errorf("Failed to get user ID\nYou can reset the token by running `curd -change-token`")) |
| 260 | } | 260 | } |
| 261 | } | 261 | } |
| 262 | anilistUserData, err := GetUserData(user.Token, user.Id) | 262 | anilistUserData, err := GetUserData(user.Token, user.Id) |
| 263 | if err != nil { | 263 | if err != nil { |
| 264 | Log(fmt.Sprintf("Failed to get user data: %v", err), logFile) | 264 | Log(fmt.Sprintf("Failed to get user data: %v", err), logFile) |
| 265 | ExitCurd(fmt.Errorf("Failed to get user data")) | 265 | ExitCurd(fmt.Errorf("Failed to get user ID\nYou can reset the token by running `curd -change-token`")) |
| 266 | } | 266 | } |
| 267 | user.AnimeList = ParseAnimeList(anilistUserData) | 267 | user.AnimeList = ParseAnimeList(anilistUserData) |
| 268 | } | 268 | } |
| @@ -274,12 +274,12 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni | |||
| 274 | user.Id, user.Username, err = GetAnilistUserID(user.Token) | 274 | user.Id, user.Username, err = GetAnilistUserID(user.Token) |
| 275 | if err != nil { | 275 | if err != nil { |
| 276 | Log(fmt.Sprintf("Failed to get user ID: %v", err), logFile) | 276 | Log(fmt.Sprintf("Failed to get user ID: %v", err), logFile) |
| 277 | ExitCurd(fmt.Errorf("Failed to get user ID")) | 277 | ExitCurd(fmt.Errorf("Failed to get user ID\nYou can reset the token by running `curd -change-token`")) |
| 278 | } | 278 | } |
| 279 | anilistUserData, err := GetUserData(user.Token, user.Id) | 279 | anilistUserData, err := GetUserData(user.Token, user.Id) |
| 280 | if err != nil { | 280 | if err != nil { |
| 281 | Log(fmt.Sprintf("Failed to get user data: %v", err), logFile) | 281 | Log(fmt.Sprintf("Failed to get user data: %v", err), logFile) |
| 282 | ExitCurd(fmt.Errorf("Failed to get user data")) | 282 | ExitCurd(fmt.Errorf("Failed to get user ID\nYou can reset the token by running `curd -change-token`")) |
| 283 | } | 283 | } |
| 284 | user.AnimeList = ParseAnimeList(anilistUserData) | 284 | user.AnimeList = ParseAnimeList(anilistUserData) |
| 285 | animeListMap := GetAnimeMap(user.AnimeList) | 285 | animeListMap := GetAnimeMap(user.AnimeList) |