From 5ee4d45ca8897edc34676f9db3f8f09647575deb Mon Sep 17 00:00:00 2001 From: Yakiyo Date: Mon, 28 Aug 2023 15:03:36 +0000 Subject: [PATCH] feat: index reader --- cache/index.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 cache/index.go diff --git a/cache/index.go b/cache/index.go new file mode 100644 index 0000000..4c6ee24 --- /dev/null +++ b/cache/index.go @@ -0,0 +1,41 @@ +package cache + +import ( + "os" + + "github.com/Yakiyo/tilde/utils" + "github.com/Yakiyo/tilde/where" + json "github.com/json-iterator/go" +) + +// using string instead of `error` cz stupid ide screams when i use +// capitalized error strings. REEEEEEEEEEEEEEEEEEEEEE! + +// read index.json from cache dir +func ReadIndex() (Index, string) { + indexPath := where.Index() + index := Index{} + if !utils.FsExists(indexPath) { + return index, "Cannot find index file in cache directory. Use the `--update/-u` flag to update cache" + } + f, err := os.ReadFile(indexPath) + if err != nil { + return index, err.Error() + } + err = json.Unmarshal(f, &index) + return index, err.Error() +} + +type Index struct { + Commands []Command `json:"commands"` +} + +type Command struct { + Name string `json:"name"` + Targets []Target `json:"targets"` +} + +type Target struct { + Os string `json:"os"` + Language string `json:"language"` +}