mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-16 10:09:28 +02:00
Code cleanup and missing error checks
This commit is contained in:
parent
6eaef18188
commit
c4193f42a1
7 changed files with 23 additions and 19 deletions
|
@ -17,6 +17,7 @@ package auth
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
|
@ -34,5 +35,12 @@ func RunOauth2CallbackServer(redirectURL url.URL, param string, responseChan cha
|
|||
}
|
||||
})
|
||||
|
||||
go http.ListenAndServe(redirectURL.Host, nil)
|
||||
go runServer(redirectURL.Host)
|
||||
}
|
||||
|
||||
func runServer(addr string) {
|
||||
err := http.ListenAndServe(addr, nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -79,7 +79,10 @@ func listRequest[T Result](c Client, path string, offset int, limit int) (result
|
|||
"limit": strconv.Itoa(limit),
|
||||
}).
|
||||
SetResult(&result)
|
||||
c.setToken(request)
|
||||
err = c.setToken(request)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
response, err := request.Get(path)
|
||||
|
||||
if response.StatusCode() != 200 {
|
||||
|
|
|
@ -105,7 +105,7 @@ func Write(data io.Writer, listens models.ListensList) (lastTimestamp time.Time,
|
|||
if !ok || rating == "" {
|
||||
rating = "L"
|
||||
}
|
||||
tsvWriter.Write([]string{
|
||||
err = tsvWriter.Write([]string{
|
||||
listen.ArtistName(),
|
||||
listen.ReleaseName,
|
||||
listen.TrackName,
|
||||
|
|
|
@ -103,7 +103,7 @@ func TestWrite(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
lastTimestamp, err := scrobblerlog.Write(buffer, log.Listens)
|
||||
require.NoError(t, err)
|
||||
result := string(buffer.Bytes())
|
||||
result := buffer.String()
|
||||
lines := strings.Split(result, "\n")
|
||||
assert.Equal(5, len(lines))
|
||||
assert.Equal("#AUDIOSCROBBLER/1.1", lines[0])
|
||||
|
|
|
@ -91,18 +91,18 @@ func (b *ScrobblerLogBackend) StartImport() error {
|
|||
} else {
|
||||
// Verify existing file is a scrobbler log
|
||||
reader := bufio.NewReader(file)
|
||||
err = ReadHeader(reader, &b.log)
|
||||
if err != nil {
|
||||
if err = ReadHeader(reader, &b.log); err != nil {
|
||||
file.Close()
|
||||
return err
|
||||
}
|
||||
file.Seek(0, 2)
|
||||
if _, err = file.Seek(0, 2); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !b.append {
|
||||
err = WriteHeader(file, &b.log)
|
||||
if err != nil {
|
||||
if err = WriteHeader(file, &b.log); err != nil {
|
||||
file.Close()
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -24,11 +24,3 @@ func GetServiceConfigFromFlag(cmd *cobra.Command, flagName string) (config.Servi
|
|||
name := cmd.Flag(flagName).Value.String()
|
||||
return config.GetService(name)
|
||||
}
|
||||
|
||||
func getInt64FromFlag(cmd *cobra.Command, flagName string) (result int64) {
|
||||
result, err := cmd.Flags().GetInt64(flagName)
|
||||
if err != nil {
|
||||
result = 0
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
|
@ -61,7 +61,8 @@ func InitConfig(cfgFile string) error {
|
|||
// Create global config if it does not exist
|
||||
if viper.ConfigFileUsed() == "" && cfgFile == "" {
|
||||
if err := os.MkdirAll(configDir, 0750); err == nil {
|
||||
viper.SafeWriteConfig()
|
||||
// This call is expected to return an error if the file already exists
|
||||
viper.SafeWriteConfig() //nolint:errcheck
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -82,7 +83,7 @@ func WriteConfig(removedKeys ...string) error {
|
|||
configMap := viper.AllSettings()
|
||||
for _, key := range removedKeys {
|
||||
c := configMap
|
||||
ok := true
|
||||
var ok bool
|
||||
subKeys := strings.Split(key, ".")
|
||||
keyLen := len(subKeys)
|
||||
// Deep search the key in the config and delete the deepest key, if it exists
|
||||
|
|
Loading…
Add table
Reference in a new issue