diff --git a/go.mod b/go.mod index a2ba97a..8bd3feb 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.22.2 require github.com/spf13/cobra v1.8.1 require ( + github.com/go-bindata/go-bindata v3.1.2+incompatible // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect ) diff --git a/go.sum b/go.sum index 912390a..5bb2194 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,6 @@ github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/go-bindata/go-bindata v3.1.2+incompatible h1:5vjJMVhowQdPzjE1LdxyFF7YFTXg5IgGVW4gBr5IbvE= +github.com/go-bindata/go-bindata v3.1.2+incompatible/go.mod h1:xK8Dsgwmeed+BBsSy2XTopBn/8uK2HWuGSnA11C3Joo= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= diff --git a/main.go b/main.go index b48987d..4968699 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "bufio" + "embed" "fmt" "github.com/spf13/cobra" "math/rand" @@ -9,6 +10,9 @@ import ( "strings" ) +//go:embed resources +var f embed.FS + func main() { var rootCmd = &cobra.Command{ Use: "yogev", @@ -62,16 +66,10 @@ func generateFact() { } func randomLine(wordType string) string { - file, err := os.Open(fmt.Sprintf("./resources/%s/%s.txt", wordType, "english")) + file, err := f.ReadFile(fmt.Sprintf("resources/%s/%s.txt", wordType, "english")) if err != nil { panic("Got an error. Very weird..." + err.Error()) } - defer func(file *os.File) { - err := file.Close() - if err != nil { - panic("Got an error. Very weird..." + err.Error()) - } - }(file) lines, err := readLines(file) if err != nil { @@ -80,9 +78,9 @@ func randomLine(wordType string) string { return lines[rand.Intn(len(lines))] } -func readLines(file *os.File) ([]string, error) { +func readLines(file []byte) ([]string, error) { var lines []string - scanner := bufio.NewScanner(file) + scanner := bufio.NewScanner(strings.NewReader(string(file))) for scanner.Scan() { lines = append(lines, scanner.Text()) }