Skip to content

Commit

Permalink
Modify: メッセージの出し方をStdoutとStderrに分類
Browse files Browse the repository at this point in the history
  • Loading branch information
kajikentaro committed Mar 7, 2023
1 parent 0fbae53 commit 1ab97b3
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 22 deletions.
16 changes: 8 additions & 8 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var SPOTIFY_PLAYLIST_ROOT = "spotify-fbc"

func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
Expand All @@ -46,7 +46,7 @@ var cleanCmd = &cobra.Command{
repository := repositories.NewRepository(client, ctx, SPOTIFY_PLAYLIST_ROOT)
deleted, err := repository.CleanUpPlaylistContent()
for d := range deleted {
log.Println(d, "was deleted.")
fmt.Fprintln(os.Stderr, d, "wad deleted.")
}
if err != nil {
log.Fatalln(err)
Expand All @@ -71,7 +71,7 @@ var pushCmd = &cobra.Command{
repository := repositories.NewRepository(client, ctx, SPOTIFY_PLAYLIST_ROOT)
model := services.NewModel(repository)
if err := model.PushPlaylists(); err != nil {
log.Fatalf(err.Error())
log.Fatalln(err)
}
},
}
Expand All @@ -85,7 +85,7 @@ var compareCmd = &cobra.Command{
repository := repositories.NewRepository(client, ctx, SPOTIFY_PLAYLIST_ROOT)
model := services.NewModel(repository)
if err := model.ComparePlaylists(); err != nil {
log.Fatalf(err.Error())
log.Fatalln(err)
}
},
}
Expand All @@ -105,7 +105,7 @@ var logoutCmd = &cobra.Command{
ctx := context.Background()
_, login := setup(ctx)
if err := login.Logout(); err != nil {
log.Fatalf(err.Error())
log.Fatalln(err)
}
},
}
Expand Down Expand Up @@ -139,7 +139,7 @@ If you have local-specific files, It will be remained`,
repository := repositories.NewRepository(client, ctx, SPOTIFY_PLAYLIST_ROOT)
model := services.NewModel(repository)
if err := model.PullPlaylists(); err != nil {
log.Fatalf(err.Error())
log.Fatalln(err)
}
},
}
Expand Down Expand Up @@ -178,10 +178,10 @@ func setup(ctx context.Context) (*spotify.Client, logins.Login) {
// save cache
err = login.SaveCache()
if err != nil {
log.Println("failed to save cache: ", err)
fmt.Fprintln(os.Stderr, "failed to save cache: ", err)
} else {
cachePath, _ := logins.GetCachePath()
log.Println("token cache was saved to ", cachePath)
fmt.Fprintln(os.Stderr, "token cache was saved to ", cachePath)
}
}

Expand Down
2 changes: 1 addition & 1 deletion logins/logins.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (l *Login) Login() error {
go func() {
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal(err)
log.Fatalln(err)
}
}()
} else {
Expand Down
5 changes: 2 additions & 3 deletions repositories/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package repositories

import (
"fmt"
"log"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -86,7 +85,7 @@ func (r *Repository) FetchLocalPlaylistTrack(dirName string) ([]models.TrackCont
t.FileName = e.Name()
}
if t.FileName != e.Name() {
log.Printf("Warning: a file_name property was incorrect. The property in the file was '%s', but path was '%s'.", t.FileName, e.Name())
fmt.Fprintf(os.Stderr, "Warning: a file_name property was incorrect. The property in the file was '%s', but path was '%s'.", t.FileName, e.Name())
t.FileName = e.Name()
}
result = append(result, t)
Expand All @@ -110,7 +109,7 @@ func (r *Repository) CreatePlaylistDirectory(playlist models.PlaylistContent) er
dirPath := filepath.Join(r.rootPath, playlist.DirName)
err := os.Mkdir(dirPath, os.ModePerm)
if os.IsExist(err) {
log.Println(playlist.Name, "is already created")
fmt.Fprintln(os.Stderr, playlist.Name, "is already created")
} else if err != nil {
return fmt.Errorf("failed to create %s", dirPath)
}
Expand Down
13 changes: 7 additions & 6 deletions repositories/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package repositories
import (
"errors"
"fmt"
"os"
"time"

"github.com/kajikentaro/spotify-file-based-client/models"
Expand Down Expand Up @@ -78,19 +79,19 @@ func (r *Repository) addRemoteTrack(playlistId string, tracks []models.TrackCont
if err != nil {
// 失敗したときはIDが存在するすべてのトラックをエラーにする
for _, w := range inputTracks {
fmt.Println(w.FileName, "failed to search track: ", err.Error())
fmt.Fprintln(os.Stderr, w.FileName, "failed to search track: ", err.Error())
}
} else {
for idx, w := range result {
if w == nil {
fmt.Println(inputTracks[idx].FileName, "no search result found", err.Error())
fmt.Fprintln(os.Stderr, inputTracks[idx].FileName, "no search result found", err.Error())
continue
}
t := models.FullTrackToContent(w)
t.FileName = inputTracks[idx].FileName
confirmedIds = append(confirmedIds, spotify.ID(t.Id))
confirmedTracks = append(confirmedTracks, t)
fmt.Println(t.Name, "was found")
fmt.Fprintln(os.Stderr, t.Name, "was found")
}
}
}
Expand All @@ -104,18 +105,18 @@ func (r *Repository) addRemoteTrack(playlistId string, tracks []models.TrackCont
// 30秒ごとのaccess limitがあるので1秒待機する
time.Sleep(time.Second * 1)
if err != nil {
fmt.Println(v.FileName, "failed to search track: ", err.Error())
fmt.Fprintln(os.Stderr, v.FileName, "failed to search track: ", err.Error())
continue
}
if len(res.Tracks.Tracks) == 0 {
fmt.Println(v.FileName, "no search result found")
fmt.Fprintln(os.Stderr, v.FileName, "no search result found")
continue
}
t := models.FullTrackToContent(&res.Tracks.Tracks[0])
t.FileName = v.FileName
confirmedIds = append(confirmedIds, spotify.ID(t.Id))
confirmedTracks = append(confirmedTracks, t)
fmt.Println(t.Name, "was found")
fmt.Fprintln(os.Stderr, t.Name, "was found")
}
if len(confirmedIds) == 0 {
// 検索結果が何も見つからなかった場合
Expand Down
7 changes: 3 additions & 4 deletions services/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package services
import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -45,7 +44,7 @@ func (m *model) recreateTrackTxt(usedFileStem *uniques.Unique, playlist models.P
err := m.repository.RemoveTrackContent(playlist.DirName, w)
if err != nil {
// 削除に失敗した場合
fmt.Println("failed to remove the old track content: ", filepath.Join(playlist.DirName, w.FileName))
fmt.Fprintln(os.Stderr, "failed to remove the old track content: ", filepath.Join(playlist.DirName, w.FileName))
continue
}
fileStem, _ := getFileStem(w.FileName)
Expand All @@ -56,7 +55,7 @@ func (m *model) recreateTrackTxt(usedFileStem *uniques.Unique, playlist models.P
w.FileName = usedFileStem.Take(stemName) + ".txt"
err = m.repository.CreateTrackContent(playlist.DirName, w)
if err != nil {
fmt.Println("failed to create a new track content: ", filepath.Join(playlist.DirName, w.FileName))
fmt.Fprintln(os.Stderr, "failed to create a new track content: ", filepath.Join(playlist.DirName, w.FileName))
}
}
}
Expand Down Expand Up @@ -151,7 +150,7 @@ func (m *model) PushPlaylists() error {
// 後片付け: 不要なプレイリストテキストを消去
deleted, err := m.repository.CleanUpPlaylistContent()
for _, d := range deleted {
log.Println(d, "was deleted.")
fmt.Fprintln(os.Stderr, d, "was deleted.")
}
if err != nil {
return err
Expand Down

0 comments on commit 1ab97b3

Please sign in to comment.