yr/cmd/cache.go

65 lines
1.1 KiB
Go
Raw Permalink Normal View History

package cmd
import (
"fmt"
"os"
"git.sr.ht/~timharek/yr/yr"
"github.com/spf13/cobra"
)
var cacheCmd = &cobra.Command{
Use: "cache",
Short: "List cached locations",
Args: cobra.MaximumNArgs(0),
Run: caches,
}
var cacheClearCmd = &cobra.Command{
Use: "clear",
Short: "Clear cached locations",
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))
}