Kaynağa Gözat

chore: better file structure

Fela Maslen 5 yıl önce
ebeveyn
işleme
722bf7517b

+ 31 - 0
music-player/pkg/read/audio.go

@@ -0,0 +1,31 @@
+package read
+
+import (
+  "os"
+
+  tag "github.com/dhowden/tag"
+
+  duration "github.com/felamaslen/go-music-player/pkg/read/duration"
+)
+
+func ReadFile(fileName string) (song *Song, err error) {
+  file, errFile := os.Open(fileName)
+  if errFile != nil {
+    return &Song{}, errFile
+  }
+  defer file.Close()
+
+  tags, errTags := tag.ReadFrom(file)
+  if errTags != nil {
+    return &Song{}, errTags
+  }
+
+  result := Song{
+    title: tags.Title(),
+    artist: tags.Artist(),
+    album: tags.Album(),
+    length: duration.GetSongDuration(file, tags),
+  }
+
+  return &result, nil
+}

+ 17 - 0
music-player/pkg/read/audio_test.go

@@ -0,0 +1,17 @@
+package read
+
+import (
+  "testing"
+  "github.com/stretchr/testify/assert"
+)
+
+func TestReadFile(t *testing.T) {
+  result, err := ReadFile(testFile)
+
+  assert.Nil(t, err)
+
+  assert.Equal(t, result.title, testTitle, "title must be correct")
+  assert.Equal(t, result.artist, testArtist, "artist must be correct")
+  assert.Equal(t, result.album, testAlbum, "album must be correct")
+  assert.Equal(t, result.length, testLengthSeconds, "length must be correct")
+}

+ 0 - 32
music-player/pkg/read/read_files.go → music-player/pkg/read/files.go

@@ -1,43 +1,11 @@
 package read
 
 import (
-  "os"
   "fmt"
   "io/ioutil"
   "path/filepath"
-
-  tag "github.com/dhowden/tag"
-
-  duration "github.com/felamaslen/go-music-player/pkg/read/duration"
 )
 
-type Song struct {
-  title, artist, album string
-  length int
-}
-
-func ReadFile(fileName string) (song *Song, err error) {
-  file, errFile := os.Open(fileName)
-  if errFile != nil {
-    return &Song{}, errFile
-  }
-  defer file.Close()
-
-  tags, errTags := tag.ReadFrom(file)
-  if errTags != nil {
-    return &Song{}, errTags
-  }
-
-  result := Song{
-    title: tags.Title(),
-    artist: tags.Artist(),
-    album: tags.Album(),
-    length: duration.GetSongDuration(file, tags),
-  }
-
-  return &result, nil
-}
-
 func ReadMultipleFiles(files chan string) chan *Song {
   songs := make(chan *Song)
 

+ 0 - 18
music-player/pkg/read/read_files_test.go → music-player/pkg/read/files_test.go

@@ -5,24 +5,6 @@ import (
   "github.com/stretchr/testify/assert"
 )
 
-const testFile = "testdata/file_example_OOG_1MG.ogg"
-
-const testTitle = "Impact Moderato"
-const testArtist = "Kevin MacLeod"
-const testAlbum = "YouTube Audio Library"
-const testLengthSeconds = 74
-
-func TestReadFile(t *testing.T) {
-  result, err := ReadFile(testFile)
-
-  assert.Nil(t, err)
-
-  assert.Equal(t, result.title, testTitle, "title must be correct")
-  assert.Equal(t, result.artist, testArtist, "artist must be correct")
-  assert.Equal(t, result.album, testAlbum, "album must be correct")
-  assert.Equal(t, result.length, testLengthSeconds, "length must be correct")
-}
-
 func TestReadMultipleFiles(t *testing.T) {
   files := make(chan string, 1)
 

+ 8 - 0
music-player/pkg/read/test_file_info.go

@@ -0,0 +1,8 @@
+package read
+
+const testFile = "testdata/file_example_OOG_1MG.ogg"
+
+const testTitle = "Impact Moderato"
+const testArtist = "Kevin MacLeod"
+const testAlbum = "YouTube Audio Library"
+const testLengthSeconds = 74

+ 6 - 0
music-player/pkg/read/types.go

@@ -0,0 +1,6 @@
+package read
+
+type Song struct {
+  title, artist, album string
+  length int
+}