build spotify-to-navidrome migrator with recovery flow
This commit is contained in:
96
internal/session/session.go
Normal file
96
internal/session/session.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Data struct {
|
||||
Spotify SpotifyState `json:"spotify"`
|
||||
}
|
||||
|
||||
type SpotifyState struct {
|
||||
ClientID string `json:"client_id,omitempty"`
|
||||
AccessToken string `json:"access_token,omitempty"`
|
||||
RefreshToken string `json:"refresh_token,omitempty"`
|
||||
TokenType string `json:"token_type,omitempty"`
|
||||
Scope string `json:"scope,omitempty"`
|
||||
ExpiresAt string `json:"expires_at,omitempty"`
|
||||
}
|
||||
|
||||
func Load(path string) (Data, error) {
|
||||
path, err := expandPath(path)
|
||||
if err != nil {
|
||||
return Data{}, err
|
||||
}
|
||||
b, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return Data{}, nil
|
||||
}
|
||||
return Data{}, fmt.Errorf("read session file: %w", err)
|
||||
}
|
||||
var d Data
|
||||
if err := json.Unmarshal(b, &d); err != nil {
|
||||
return Data{}, fmt.Errorf("decode session file: %w", err)
|
||||
}
|
||||
return d, nil
|
||||
}
|
||||
|
||||
func Save(path string, d Data) error {
|
||||
path, err := expandPath(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dir := filepath.Dir(path)
|
||||
if err := os.MkdirAll(dir, 0o700); err != nil {
|
||||
return fmt.Errorf("create session directory: %w", err)
|
||||
}
|
||||
|
||||
b, err := json.MarshalIndent(d, "", " ")
|
||||
if err != nil {
|
||||
return fmt.Errorf("encode session file: %w", err)
|
||||
}
|
||||
|
||||
tmp := path + ".tmp"
|
||||
if err := os.WriteFile(tmp, b, 0o600); err != nil {
|
||||
return fmt.Errorf("write temp session file: %w", err)
|
||||
}
|
||||
if err := os.Rename(tmp, path); err != nil {
|
||||
return fmt.Errorf("replace session file: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s SpotifyState) ExpiresAtTime() time.Time {
|
||||
t, err := time.Parse(time.RFC3339, strings.TrimSpace(s.ExpiresAt))
|
||||
if err != nil {
|
||||
return time.Time{}
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
func expandPath(path string) (string, error) {
|
||||
path = strings.TrimSpace(path)
|
||||
if path == "" {
|
||||
return "", fmt.Errorf("session path cannot be empty")
|
||||
}
|
||||
if strings.HasPrefix(path, "~/") || path == "~" {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("resolve home directory: %w", err)
|
||||
}
|
||||
if path == "~" {
|
||||
path = home
|
||||
} else {
|
||||
path = filepath.Join(home, strings.TrimPrefix(path, "~/"))
|
||||
}
|
||||
}
|
||||
return filepath.Clean(path), nil
|
||||
}
|
||||
42
internal/session/session_test.go
Normal file
42
internal/session/session_test.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSaveLoadRoundTrip(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
path := filepath.Join(t.TempDir(), "session.json")
|
||||
in := Data{
|
||||
Spotify: SpotifyState{
|
||||
ClientID: "abc123",
|
||||
AccessToken: "access-token",
|
||||
RefreshToken: "refresh-token",
|
||||
ExpiresAt: "2026-01-02T03:04:05Z",
|
||||
},
|
||||
}
|
||||
|
||||
if err := Save(path, in); err != nil {
|
||||
t.Fatalf("Save failed: %v", err)
|
||||
}
|
||||
|
||||
out, err := Load(path)
|
||||
if err != nil {
|
||||
t.Fatalf("Load failed: %v", err)
|
||||
}
|
||||
|
||||
if out.Spotify.ClientID != in.Spotify.ClientID {
|
||||
t.Fatalf("ClientID mismatch: got %q want %q", out.Spotify.ClientID, in.Spotify.ClientID)
|
||||
}
|
||||
if out.Spotify.AccessToken != in.Spotify.AccessToken {
|
||||
t.Fatalf("AccessToken mismatch")
|
||||
}
|
||||
if out.Spotify.RefreshToken != in.Spotify.RefreshToken {
|
||||
t.Fatalf("RefreshToken mismatch")
|
||||
}
|
||||
if out.Spotify.ExpiresAt != in.Spotify.ExpiresAt {
|
||||
t.Fatalf("ExpiresAt mismatch: got %q want %q", out.Spotify.ExpiresAt, in.Spotify.ExpiresAt)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user