Skip to content

Commit

Permalink
Merge pull request #4 from ciaarraa/feat/cache-file-write
Browse files Browse the repository at this point in the history
Make cache location fixed
  • Loading branch information
ciaarraa authored Sep 21, 2024
2 parents 0557979 + ad63b6f commit 7152604
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (cache *Cache) Get(key string) (string, error) {

func checkCacheFolder(cacheFolder string) {
if _, err := os.Stat(cacheFolder); errors.Is(err, os.ErrNotExist) {
err := os.Mkdir(cacheFolder, os.ModePerm)
err := os.MkdirAll(cacheFolder, os.ModePerm)
if err != nil {
fmt.Println("An error has occured: ", err)
}
Expand Down
20 changes: 14 additions & 6 deletions cmd/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,27 @@ var locationCmd = &cobra.Command{
yrClient := client.NewYrClient(http.DefaultClient, "whats-the-weather-local/0.0 [email protected] ")
geoClient := geocoder.NewGeocoderClient(nil)
cahce_key := args[0]
cache_db := cache.NewCache("tmp/db", ".cache")

homeDirname, err := os.UserHomeDir()
if err != nil {
fmt.Println(err)
}
cachedFilesFolder := homeDirname + "/.whats-the-weather/.cache"
dbFolder := homeDirname + "/.whats-the-weather/tmp/db"

cache_db := cache.NewCache(dbFolder, cachedFilesFolder)

var longitude string
var latitude string

coords, err := cache_db.Get(cahce_key);
if err != nil {
coords, err := cache_db.Get(cahce_key)
if err != nil {
coordsAndPlaces, _ := geoClient.FindCoordinates(args[0])
firstMatch := coordsAndPlaces[0]
longitude = firstMatch.Longitude
latitude = firstMatch.Latitude
cache_db.Add([]byte(firstMatch.Latitude+","+firstMatch.Longitude), cahce_key)
coords = firstMatch.Latitude+","+firstMatch.Longitude
coords = firstMatch.Latitude + "," + firstMatch.Longitude
fmt.Printf("Location: %s\n", firstMatch.DisplayName)

} else {
Expand All @@ -60,7 +68,7 @@ var locationCmd = &cobra.Command{
// float64 objects but for now, we'll convert them here
floatValueLatitude, err1 := strconv.ParseFloat(latitude, 64)
floatValueLongitude, err2 := strconv.ParseFloat(longitude, 64)
if err1 != nil || err2 != nil {
if err1 != nil || err2 != nil {
fmt.Println("Error:", err1)
return
}
Expand Down Expand Up @@ -94,7 +102,7 @@ var locationCmd = &cobra.Command{
t.AppendRows([]table.Row{{"Weather Summary", *forecastData.Next1Hours.Summary.SymbolCode, *forecastData.Next6Hours.Summary.SymbolCode, *forecastData.Next12Hours.Summary.SymbolCode}})
t.Render()

fmt.Printf("temperature at is %v degrees celcius right now\n", utils.Float64Value(temperatureNow))
fmt.Printf("temperature at is %v degrees celcius right now\n", utils.Float64Value(temperatureNow))
},
}

Expand Down

0 comments on commit 7152604

Please sign in to comment.