🚚 Embed the resources

This commit is contained in:
Andreas Schneider 2021-03-06 14:21:30 +01:00
parent 247c750932
commit 79d322de14
1 changed files with 10 additions and 8 deletions

18
main.go
View File

@ -1,9 +1,9 @@
package main package main
import ( import (
"embed"
"encoding/json" "encoding/json"
"fmt" "fmt"
"os"
"sync" "sync"
"fyne.io/fyne/v2" "fyne.io/fyne/v2"
@ -15,6 +15,9 @@ import (
"github.com/sahilm/fuzzy" "github.com/sahilm/fuzzy"
) )
//go:embed data/gitmojis.json data/images
var data embed.FS
type Gitmoji struct { type Gitmoji struct {
Emoji string Emoji string
Code string Code string
@ -65,13 +68,13 @@ func main() {
currentId := 0 currentId := 0
for i, gitmoji := range gitmojiData.Gitmojis { for i, gitmoji := range gitmojiData.Gitmojis {
res, err := fyne.LoadResourceFromPath("data/images/" + gitmoji.Name + ".png") b, err := data.ReadFile("data/images/" + gitmoji.Name + ".png")
if err != nil { if err != nil {
panic(err) panic(err)
} }
allEntries[i] = &GitmojiEntry{ allEntries[i] = &GitmojiEntry{
Gitmoji: gitmoji, Gitmoji: gitmoji,
Icon: res, Icon: fyne.NewStaticResource(gitmoji.Name, b),
} }
allTerms[i] = gitmoji.Name + " " + gitmoji.Description allTerms[i] = gitmoji.Name + " " + gitmoji.Description
} }
@ -195,15 +198,14 @@ func main() {
} }
func loadData() (*GitmojiData, error) { func loadData() (*GitmojiData, error) {
f, err := os.Open("data/gitmojis.json") b, err := data.ReadFile("data/gitmojis.json")
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot load gitmoji file: %w", err) return nil, fmt.Errorf("cannot read gitmoji data: %w", err)
} }
defer f.Close()
var result GitmojiData var result GitmojiData
if err := json.NewDecoder(f).Decode(&result); err != nil { if err := json.Unmarshal(b, &result); err != nil {
return nil, fmt.Errorf("cannot load gitmoji file: %w", err) return nil, fmt.Errorf("cannot decode gitmoji data: %w", err)
} }
return &result, nil return &result, nil