yr/cmd/cache.go
Tim Hårek Andreassen 985f8a53d3
feat: Add cache cmd
Implements: https://todo.sr.ht/~timharek/yr/4
Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
2024-10-17 21:17:12 +02:00

64 lines
1.2 KiB
Go

package cmd
import (
"fmt"
"os"
"git.sr.ht/~timharek/yr/yr"
"github.com/spf13/cobra"
)
var cacheCmd = &cobra.Command{
Use: "cache",
Short: "Get details about your cached locations",
Args: cobra.MaximumNArgs(0),
Run: caches,
}
var cacheClearCmd = &cobra.Command{
Use: "clear",
Short: "Clear cache",
Args: cobra.MaximumNArgs(0),
Run: cachesClear,
}
func init() {
rootCmd.AddCommand(cacheCmd)
cacheCmd.AddCommand(cacheClearCmd)
}
func caches(cmd *cobra.Command, args []string) {
c, err := yr.New()
cobra.CheckErr(err)
list, err := c.Cache.List()
cobra.CheckErr(err)
if len(list) == 0 {
fmt.Println("No location is cached")
os.Exit(0)
}
// NOTE: Both location name and coordinates are used to store files
fmt.Println("Cached locations")
for _, item := range list {
fmt.Println("\t" + item.Filename)
}
}
func cachesClear(cmd *cobra.Command, args []string) {
c, err := yr.New()
cobra.CheckErr(err)
list, err := c.Cache.List()
cobra.CheckErr(err)
if len(list) == 0 {
fmt.Println("No location is cached")
os.Exit(0)
}
err = c.Cache.Clear()
cobra.CheckErr(err)
fmt.Printf("Cleared %d locations from cache\n", len(list))
}