Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
"sort"

// "io"
"net/http"
Expand Down Expand Up @@ -77,8 +78,10 @@ type CurdConfig struct {
MyAnimeListImportDismissed bool `config:"MyAnimeListImportDismissed"`
}

const DefaultMpvPlaybackStartTimeout = 20
const maxMpvPlaybackStartTimeout = 600
const (
DefaultMpvPlaybackStartTimeout = 20
maxMpvPlaybackStartTimeout = 600
)

func MpvPlaybackStartTimeoutDuration(config *CurdConfig) time.Duration {
seconds := DefaultMpvPlaybackStartTimeout
Expand Down Expand Up @@ -313,7 +316,7 @@ func createDefaultConfig(path string) error {

// Ensure the directory exists
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, 0755); err != nil {
if err := os.MkdirAll(dir, 0o755); err != nil {
return fmt.Errorf("error creating directory: %v", err)
}

Expand Down Expand Up @@ -534,7 +537,7 @@ func loadToken(tokenPath string) (*AnilistToken, error) {
// saveToken saves the token to the token file
func saveToken(tokenPath string, token *AnilistToken) error {
// Ensure directory exists
if err := os.MkdirAll(filepath.Dir(tokenPath), 0755); err != nil {
if err := os.MkdirAll(filepath.Dir(tokenPath), 0o755); err != nil {
return fmt.Errorf("failed to create directory: %w", err)
}

Expand All @@ -543,7 +546,7 @@ func saveToken(tokenPath string, token *AnilistToken) error {
return fmt.Errorf("failed to marshal token: %w", err)
}

return os.WriteFile(tokenPath, data, 0600)
return os.WriteFile(tokenPath, data, 0o600)
}

// isTokenValid checks if the token is still valid
Expand Down Expand Up @@ -584,7 +587,6 @@ func ChangeToken(config *CurdConfig, user *User) {
// Try browser-based OAuth first
fmt.Println("Starting browser-based authentication...")
user.Token, err = authenticateWithBrowser(tokenPath)

if err != nil {
Log("Browser authentication failed: " + err.Error())
fmt.Printf("Browser authentication failed: %v\n", err)
Expand Down Expand Up @@ -659,9 +661,15 @@ func SaveConfigToFile(path string, configMap map[string]string) error {
}
defer file.Close()

keys := make([]string, 0, len(configMap))
for key := range configMap {
keys = append(keys, key)
}
sort.Strings(keys)

writer := bufio.NewWriter(file)
for key, value := range configMap {
line := fmt.Sprintf("%s=%s\n", key, value)
for _, key := range keys {
line := fmt.Sprintf("%s=%s\n", key, configMap[key])
if _, err := writer.WriteString(line); err != nil {
return err
}
Expand Down
7 changes: 4 additions & 3 deletions internal/tracking.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ func EnsureTrackingConfigured(config *CurdConfig) error {

normalizeTrackingConfig(config)
if config.TrackingConfigured {
return persistTrackingConfig(config)
// NOTE: persistTrackingConfig is only needed when tracker is changed not every startup
return nil
}

options := []SelectionOption{
Expand Down Expand Up @@ -376,7 +377,7 @@ func writeTrackingBackup(config *CurdConfig, action string, aniList, myAnimeList
}

backupDir := filepath.Join(os.ExpandEnv(config.StoragePath), "tracking-backups")
if err := os.MkdirAll(backupDir, 0755); err != nil {
if err := os.MkdirAll(backupDir, 0o755); err != nil {
return "", err
}

Expand All @@ -398,7 +399,7 @@ func writeTrackingBackup(config *CurdConfig, action string, aniList, myAnimeList
if err != nil {
return "", err
}
if err := os.WriteFile(backupPath, data, 0644); err != nil {
if err := os.WriteFile(backupPath, data, 0o644); err != nil {
return "", err
}
return backupPath, nil
Expand Down
Loading