65 lines
1.2 KiB
Go
65 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))
|
||
|
}
|