Skip to content

Commit

Permalink
feat: logging
Browse files Browse the repository at this point in the history
  • Loading branch information
agukrapo committed Dec 31, 2024
1 parent e85bda2 commit 6daef9e
Show file tree
Hide file tree
Showing 11 changed files with 264 additions and 69 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.env
bin/
fyne-cross/
*.log
17 changes: 14 additions & 3 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ import (
"path/filepath"
"strings"

"fyne.io/fyne/v2"
"github.com/agukrapo/go-http-client/client"
"github.com/agukrapo/playlist-creator/deezer"
"github.com/agukrapo/playlist-creator/internal/env"
"github.com/agukrapo/playlist-creator/internal/logs"
"github.com/agukrapo/playlist-creator/internal/random"
"github.com/agukrapo/playlist-creator/internal/results"
"github.com/agukrapo/playlist-creator/playlists"
"github.com/agukrapo/playlist-creator/spotify"
)

const appTitle = "playlist-creator-cli"

func main() {
if err := run(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Error: %v\n", err)
Expand All @@ -38,7 +42,14 @@ func run() error {
cancel()
}()

manager, err := buildManager()
logFile, err := logs.NewFile(appTitle)
if err != nil {
fyne.LogError("logs.NewFile", err)
os.Exit(1)
}
defer logFile.Close()

manager, err := buildManager(logs.New(logFile))
if err != nil {
return err
}
Expand Down Expand Up @@ -84,7 +95,7 @@ func run() error {
return nil
}

func buildManager() (*playlists.Manager, error) {
func buildManager(log *logs.Logger) (*playlists.Manager, error) {
if len(os.Args) < 2 {
return nil, errors.New("target argument missing")
}
Expand All @@ -102,7 +113,7 @@ func buildManager() (*playlists.Manager, error) {
if err != nil {
return nil, err
}
target = deezer.New(client.New(), cookie)
target = deezer.New(client.New(), cookie, log)
default:
return nil, fmt.Errorf("unknown target %s", os.Args[1])
}
Expand Down
17 changes: 14 additions & 3 deletions cmd/gui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/agukrapo/go-http-client/client"
"github.com/agukrapo/playlist-creator/deezer"
"github.com/agukrapo/playlist-creator/internal/env"
"github.com/agukrapo/playlist-creator/internal/logs"
"github.com/agukrapo/playlist-creator/internal/random"
"github.com/agukrapo/playlist-creator/internal/results"
"github.com/agukrapo/playlist-creator/playlists"
Expand All @@ -30,7 +31,14 @@ func main() {
fyne.LogError("env.Lookup", err)
}

app := newApplication(cookie)
logFile, err := logs.NewFile(appTitle)
if err != nil {
fyne.LogError("logs.NewFile", err)
os.Exit(1)
}
defer logFile.Close()

app := newApplication(cookie, logs.New(logFile))
app.ShowAndRun()
}

Expand All @@ -41,9 +49,11 @@ type application struct {
dialogs chan dialoger

cookie string

log *logs.Logger
}

func newApplication(cookie string) *application {
func newApplication(cookie string, log *logs.Logger) *application {
out := fyneapp.New()

version := out.Metadata().Custom["version"]
Expand All @@ -58,6 +68,7 @@ func newApplication(cookie string) *application {
window: w,
dialogs: make(chan dialoger),
cookie: cookie,
log: log,
}
}

Expand Down Expand Up @@ -102,7 +113,7 @@ func (a *application) renderForm() {

a.working()

target := deezer.New(client.New(), arl.Text)
target := deezer.New(client.New(), arl.Text, a.log)
a.renderResults(target, name.Text, lines(songs.Text))
}

Expand Down
38 changes: 28 additions & 10 deletions deezer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"

"github.com/agukrapo/go-http-client/requests"
"github.com/agukrapo/playlist-creator/internal/logs"
"github.com/agukrapo/playlist-creator/playlists"
)

Expand All @@ -26,13 +27,16 @@ type Client struct {
tokenizer func(ctx context.Context) (string, cookieJar, error)

arl string

log *logs.Logger
}

func New(httpClient doer, arl string) *Client {
func New(httpClient doer, arl string, log *logs.Logger) *Client {
out := &Client{
httpClient: httpClient,
apiURL: "https://www.deezer.com/ajax/gw-light.php",
arl: arl,
log: log,
}

out.tokenizer = out.token
Expand All @@ -55,11 +59,14 @@ type userResponse struct {
CheckForm string `json:"checkForm"`
}

func (c *Client) token(ctx context.Context) (string, cookieJar, error) {
func (c *Client) token(ctx context.Context) (token string, cookies cookieJar, err error) {
tr := c.log.Trace("deezer.token").Begins()
defer func() { tr.Ends(err, logs.Var("token", token), logs.Var("cookies", cookies)) }()

arl := newJar(&http.Cookie{Name: "arl", Value: c.arl})

var out userResponse
cookies, err := c.send(ctx, "", "deezer.getUserData", arl, nil, &out)
cookies, err = c.send(ctx, tr, "", "deezer.getUserData", arl, nil, &out)
if err != nil {
return "", nil, err
}
Expand Down Expand Up @@ -115,7 +122,10 @@ func (sr searchResponse) tracks() []playlists.Track {
return out
}

func (c *Client) SearchTrack(ctx context.Context, query string) ([]playlists.Track, error) {
func (c *Client) SearchTracks(ctx context.Context, query string) (tracks []playlists.Track, err error) {
tr := c.log.Trace("deezer.SearchTracks").Begins(logs.Var("query", query))
defer func() { tr.Ends(err, logs.Var("tracks", tracks)) }()

token, cookies, err := c.tokenizer(ctx)
if err != nil {
return nil, err
Expand All @@ -124,14 +134,17 @@ func (c *Client) SearchTrack(ctx context.Context, query string) ([]playlists.Tra
in := map[string]string{"query": query}

var out searchResponse
if _, err := c.send(ctx, token, "deezer.pageSearch", cookies, in, &out); err != nil {
if _, err := c.send(ctx, tr, token, "deezer.pageSearch", cookies, in, &out); err != nil {
return nil, err
}

return out.tracks(), nil
}

func (c *Client) CreatePlaylist(ctx context.Context, title string) (string, error) {
func (c *Client) CreatePlaylist(ctx context.Context, title string) (id string, err error) {
tr := c.log.Trace("deezer.CreatePlaylist").Begins(logs.Var("title", title))
defer func() { tr.Ends(err, logs.Var("id", id)) }()

token, cookies, err := c.tokenizer(ctx)
if err != nil {
return "", err
Expand All @@ -140,7 +153,7 @@ func (c *Client) CreatePlaylist(ctx context.Context, title string) (string, erro
in := map[string]any{"title": title}

var out json.Number
if _, err := c.send(ctx, token, "playlist.create", cookies, in, &out); err != nil {
if _, err := c.send(ctx, tr, token, "playlist.create", cookies, in, &out); err != nil {
return "", err
}

Expand All @@ -151,7 +164,10 @@ func (c *Client) CreatePlaylist(ctx context.Context, title string) (string, erro
return out.String(), nil
}

func (c *Client) PopulatePlaylist(ctx context.Context, playlist string, tracks []string) error {
func (c *Client) PopulatePlaylist(ctx context.Context, playlist string, tracks []string) (err error) {
tr := c.log.Trace("deezer.PopulatePlaylist").Begins(logs.Var("playlist", playlist), logs.Var("tracks", tracks))
defer func() { tr.Ends(err) }()

token, cookies, err := c.tokenizer(ctx)
if err != nil {
return err
Expand All @@ -168,7 +184,7 @@ func (c *Client) PopulatePlaylist(ctx context.Context, playlist string, tracks [
}

var out bool
if _, err := c.send(ctx, token, "playlist.addSongs", cookies, in, &out); err != nil {
if _, err := c.send(ctx, tr, token, "playlist.addSongs", cookies, in, &out); err != nil {
return err
}

Expand Down Expand Up @@ -228,7 +244,7 @@ func newJar(cookies ...*http.Cookie) cookieJar {
return out
}

func (c *Client) send(ctx context.Context, token, method string, cookies cookieJar, in, out any) (cookieJar, error) {
func (c *Client) send(ctx context.Context, trace *logs.Trace, token, method string, cookies cookieJar, in, out any) (cookieJar, error) {
req, err := requests.New(c.apiURL).Post().JSON(in).Build(ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -257,6 +273,8 @@ func (c *Client) send(ctx context.Context, token, method string, cookies cookieJ
return nil, err
}

trace.Dump(raw)

if len(raw) == 0 {
return nil, fmt.Errorf("%s: empty response", method)
}
Expand Down
9 changes: 5 additions & 4 deletions deezer/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http/httptest"
"testing"

"github.com/agukrapo/playlist-creator/internal/logs"
"github.com/agukrapo/playlist-creator/internal/tests"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -44,7 +45,7 @@ func TestClient_token(t *testing.T) {
}))
defer svr.Close()

client := New(http.DefaultClient, "_ARL")
client := New(http.DefaultClient, "_ARL", logs.New(nil))
client.apiURL = svr.URL

token, cookies, err := client.token(context.Background())
Expand Down Expand Up @@ -89,13 +90,13 @@ func TestClient_SearchTrack(t *testing.T) {
}))
defer svr.Close()

client := New(http.DefaultClient, "_ARL")
client := New(http.DefaultClient, "_ARL", logs.New(nil))
client.apiURL = svr.URL
client.tokenizer = func(context.Context) (string, cookieJar, error) {
return "_TOKEN", newJar(&http.Cookie{Name: "arl", Value: "_ARL"}), nil
}

matches, err := client.SearchTrack(context.Background(), "_QUERY")
matches, err := client.SearchTracks(context.Background(), "_QUERY")
require.NoError(t, err)

assert.Len(t, matches, test.expectedMatchesLength)
Expand Down Expand Up @@ -140,7 +141,7 @@ func TestClient_PopulatePlaylist(t *testing.T) {
}))
defer svr.Close()

client := New(http.DefaultClient, "_ARL")
client := New(http.DefaultClient, "_ARL", logs.New(nil))
client.apiURL = svr.URL
client.tokenizer = func(context.Context) (string, cookieJar, error) {
return "_TOKEN", newJar(&http.Cookie{Name: "arl", Value: "_ARL"}), nil
Expand Down
28 changes: 14 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,40 @@ module github.com/agukrapo/playlist-creator
go 1.23

require (
fyne.io/fyne/v2 v2.5.2
github.com/agukrapo/go-http-client v1.3.0
fyne.io/fyne/v2 v2.5.3
github.com/agukrapo/go-http-client v1.3.1
github.com/joho/godotenv v1.5.1
github.com/stretchr/testify v1.9.0
golang.org/x/sync v0.8.0
github.com/stretchr/testify v1.10.0
golang.org/x/sync v0.10.0
)

require (
fyne.io/systray v1.11.0 // indirect
github.com/BurntSushi/toml v1.4.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fredbi/uri v1.1.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/fsnotify/fsnotify v1.8.0 // indirect
github.com/fyne-io/gl-js v0.0.0-20230506162202-1fdaa286a934 // indirect
github.com/fyne-io/glfw-js v0.0.0-20240101223322-6e1efdc71b7a // indirect
github.com/fyne-io/glfw-js v0.0.0-20241126112943-313d8a0fe1d0 // indirect
github.com/fyne-io/image v0.0.0-20240417123036-dc0ee9e7c964 // indirect
github.com/go-gl/gl v0.0.0-20231021071112-07e5d0ea2e71 // indirect
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20240506104042-037f3cc74f2a // indirect
github.com/go-text/render v0.2.0 // indirect
github.com/go-text/typesetting v0.2.0 // indirect
github.com/go-text/typesetting v0.2.1 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/gopherjs/gopherjs v1.17.2 // indirect
github.com/jeandeaual/go-locale v0.0.0-20240223122105-ce5225dcaa49 // indirect
github.com/jeandeaual/go-locale v0.0.0-20241217141322-fcc2cadd6f08 // indirect
github.com/jsummers/gobmp v0.0.0-20230614200233-a9de23ed2e25 // indirect
github.com/nicksnyder/go-i18n/v2 v2.4.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rymdport/portal v0.2.6 // indirect
github.com/rymdport/portal v0.3.0 // indirect
github.com/srwiley/oksvg v0.0.0-20221011165216-be6e8873101c // indirect
github.com/srwiley/rasterx v0.0.0-20220730225603-2ab79fcdd4ef // indirect
github.com/yuin/goldmark v1.7.8 // indirect
golang.org/x/image v0.21.0 // indirect
golang.org/x/mobile v0.0.0-20241016134751-7ff83004ec2c // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
golang.org/x/image v0.23.0 // indirect
golang.org/x/mobile v0.0.0-20241213221354-a87c1cf6cf46 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 6daef9e

Please sign in to comment.