Skip to content

Commit a99e063

Browse files
committed
fix: fixed issue with download cache
1 parent c39ae9c commit a99e063

File tree

6 files changed

+70
-23
lines changed

6 files changed

+70
-23
lines changed

cmd/root.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"github.com/rawnly/splash-cli/lib/blurhash"
2222
"github.com/rawnly/splash-cli/lib/keys"
2323
"github.com/rawnly/splash-cli/lib/terminal"
24-
"github.com/rawnly/splash-cli/unsplash"
2524
"github.com/rawnly/splash-cli/unsplash/models"
2625
"github.com/sirupsen/logrus"
2726
"github.com/spf13/cobra"
@@ -32,7 +31,7 @@ type photoFlags struct {
3231
Day bool `json:"day" description:"Get a the photo of the day"`
3332
Orientation string `json:"orientation" default:"landscape" description:"Specifies the photo orientation"`
3433
Query string `json:"query" description:"Search for a photo"`
35-
Id string `json:"id" description:"Get a photo by id"`
34+
ID string `json:"id" description:"Get a photo by id"`
3635
Save bool `json:"save" description:"Save the photo without setting it as wallpaper"`
3736
Scale string `json:"scale" default:"auto" description:"Set wallpaper scale"`
3837
IgnoreCache bool `json:"ignore-cache" default:"false" description:"Ignore cache and download image again"`
@@ -88,10 +87,10 @@ var rootCmd = &cobra.Command{
8887
var err error
8988

9089
ctx := cmd.Context()
91-
api := ctx.Value("api").(unsplash.Api)
90+
api := keys.GetAPIInstance(ctx)
9291
analytics := keys.GetAnalyticsInstance(ctx)
9392

94-
photoOfTheDayId := viper.GetString("photo-of-the-day.id")
93+
photoOfTheDayID := viper.GetString("photo-of-the-day.id")
9594

9695
ConnectionSpinnerSuffix := []string{" Connecting to Unsplash...", "Failed to connect\n", "✔ Connected"}
9796
DownloadSpinnerSuffix := []string{" Downloading photo...", "Failed to download\n", "✔ Downloaded"}
@@ -136,8 +135,8 @@ var rootCmd = &cobra.Command{
136135
if dayFlag {
137136
analytics.Capture("photo_of_the_day", nil)
138137

139-
if photoOfTheDayId != "" && !ignoreCacheFlag {
140-
photo, err = api.GetPhoto(photoOfTheDayId)
138+
if photoOfTheDayID != "" && !ignoreCacheFlag {
139+
photo, err = api.GetPhoto(photoOfTheDayID)
141140
} else {
142141
photo, err = api.GetPhotoOfTheDay()
143142
if err != nil {
@@ -161,7 +160,7 @@ var rootCmd = &cobra.Command{
161160

162161
handleSpinnerError(err, connectionSpinner, cmd, ConnectionSpinnerSuffix[1])
163162
} else if idFlag != "" {
164-
idFlag = lib.ParsePhotoIDFromUrl(idFlag)
163+
idFlag = lib.ParsePhotoIDFromURL(idFlag)
165164

166165
analytics.Capture("photo_by_id", map[string]any{
167166
"photo_id": idFlag,
@@ -172,7 +171,8 @@ var rootCmd = &cobra.Command{
172171
} else {
173172
analytics.Capture("random_photo", nil)
174173

175-
photos, err := api.GetRandomPhoto(models.RandomPhotoParams{
174+
var photos []models.Photo
175+
photos, err = api.GetRandomPhoto(models.RandomPhotoParams{
176176
Orientation: orientationFlag,
177177
Query: queryFlag,
178178
Count: 1,
@@ -199,7 +199,8 @@ var rootCmd = &cobra.Command{
199199
downloadFolder := viper.GetString("download_dir")
200200

201201
if downloadFolder == "" {
202-
folder, err := lib.HomePath("Pictures")
202+
var folder string
203+
folder, err = lib.HomePath("Pictures")
203204
handleSpinnerError(err, downloadSpinner, cmd, DownloadSpinnerSuffix[1])
204205

205206
downloadFolder = folder
@@ -213,6 +214,11 @@ var rootCmd = &cobra.Command{
213214

214215
var location string
215216

217+
// debug printing
218+
fmt.Println("Download location:", downloadLocation)
219+
fmt.Println("Ignore cache:", ignoreCacheFlag)
220+
fmt.Println("File exists:", lib.FileExists(downloadLocation))
221+
216222
if lib.FileExists(downloadLocation) && !ignoreCacheFlag {
217223
location = downloadLocation
218224

@@ -235,10 +241,11 @@ var rootCmd = &cobra.Command{
235241
fmt.Println("Would you like to set it from the URL?")
236242
fmt.Println("")
237243

238-
_, key, err := keyboard.GetSingleKey()
244+
var key keyboard.Key
245+
_, key, err = keyboard.GetSingleKey()
239246
if err != nil {
240-
evtId := sentry.CaptureException(err)
241-
logrus.WithField("event_id", evtId).Fatal(err)
247+
eventID := sentry.CaptureException(err)
248+
logrus.WithField("event_id", eventID).Fatal(err)
242249
}
243250

244251
switch key {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ require (
2222
github.com/spf13/cobra v1.8.0
2323
github.com/spf13/viper v1.18.2
2424
github.com/voxelite-ai/env v0.0.1
25+
github.com/zalando/go-keyring v0.2.6
2526
)
2627

2728
require (
@@ -44,7 +45,6 @@ require (
4445
github.com/spf13/afero v1.11.0 // indirect
4546
github.com/spf13/pflag v1.0.5 // indirect
4647
github.com/subosito/gotenv v1.6.0 // indirect
47-
github.com/zalando/go-keyring v0.2.6 // indirect
4848
go.uber.org/multierr v1.11.0 // indirect
4949
golang.org/x/exp v0.0.0-20231226003508-02704c960a9b // indirect
5050
golang.org/x/sys v0.33.0 // indirect

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
4141
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
4242
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
4343
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
44+
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
45+
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
4446
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
4547
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
4648
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
@@ -120,6 +122,8 @@ github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMV
120122
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
121123
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
122124
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
125+
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
126+
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
123127
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
124128
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
125129
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
@@ -158,8 +162,6 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc
158162
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
159163
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
160164
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
161-
golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20=
162-
golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
163165
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
164166
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
165167
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=

lib/file_util.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package lib
22

33
import (
4+
"errors"
45
"fmt"
56
"io"
7+
"io/fs"
68
"net/http"
79
"os"
810
"regexp"
@@ -29,7 +31,7 @@ func HomePath(path string) (string, error) {
2931
return fmt.Sprintf("%s/%s", homedir, path), nil
3032
}
3133

32-
func InsertHomeIfNeeded(path string) (string, error) {
34+
func expandPath(path string) (string, error) {
3335
if len(path) > 0 && path[0] != '~' {
3436
return path, nil
3537
}
@@ -48,14 +50,22 @@ func InsertHomeIfNeeded(path string) (string, error) {
4850
}
4951

5052
func FileExists(filename string) bool {
51-
_, err := os.Stat(filename)
53+
p, err := expandPath(filename)
54+
if err != nil {
55+
return false
56+
}
57+
58+
info, err := os.Stat(p)
59+
if err != nil {
60+
return !errors.Is(err, fs.ErrNotExist)
61+
}
5262

53-
return err != nil
63+
return !info.IsDir()
5464
}
5565

5666
// DownloadFile / Download a file from a URL and returns the path as string
5767
func DownloadFile(url string, filename string) (string, error) {
58-
path, err := InsertHomeIfNeeded(filename)
68+
path, err := expandPath(filename)
5969
if err != nil {
6070
return "", err
6171
}

lib/file_util_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package lib
2+
3+
import (
4+
"os"
5+
"testing"
6+
)
7+
8+
func TestFileExists(t *testing.T) {
9+
// 1. create a temporary file
10+
tmpFile, err := os.CreateTemp("", "testfile")
11+
if err != nil {
12+
t.Fatalf("Failed to create temp file: %v", err)
13+
}
14+
15+
defer os.Remove(tmpFile.Name()) // clean up
16+
17+
// 2. test that FileExists returns true for the existing file
18+
if !FileExists(tmpFile.Name()) {
19+
t.Errorf("FileExists returned false for an existing file: %s", tmpFile.Name())
20+
}
21+
22+
// 3. test that FileExists returns false for a non-existing file
23+
nonExistentFile := tmpFile.Name() + "_nonexistent"
24+
if FileExists(nonExistentFile) {
25+
t.Errorf("FileExists returned true for a non-existing file: %s", nonExistentFile)
26+
}
27+
}

lib/util.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func GetHomeDir() string {
2020
return usr.HomeDir
2121
}
2222

23+
// ExpandPath expands a path starting with ~ to the user's home directory
2324
func ExpandPath(path string) string {
2425
if path[:2] == "~/" {
2526
return GetHomeDir() + path[1:]
@@ -46,12 +47,12 @@ func ParseStringValue(value string) any {
4647
return val
4748
}
4849

49-
func ParsePhotoIDFromUrl(urlOrId string) string {
50-
if expressions.IsPhotoUrl(urlOrId) {
51-
return expressions.CleanupUrl(urlOrId)
50+
func ParsePhotoIDFromURL(urlOrID string) string {
51+
if expressions.IsPhotoUrl(urlOrID) {
52+
return expressions.CleanupUrl(urlOrID)
5253
}
5354

54-
return urlOrId
55+
return urlOrID
5556
}
5657

5758
func ParseCollections(collections []string) []string {

0 commit comments

Comments
 (0)