Skip to content

Commit

Permalink
Merge pull request #3 from roycald245/dev
Browse files Browse the repository at this point in the history
embed the resources for the executable
  • Loading branch information
roycald245 authored Jul 23, 2024
2 parents 7aa0a87 + c021e95 commit 614caf5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
16 changes: 7 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package main

import (
"bufio"
"embed"
"fmt"
"github.com/spf13/cobra"
"math/rand"
"os"
"strings"
)

//go:embed resources
var f embed.FS

func main() {
var rootCmd = &cobra.Command{
Use: "yogev",
Expand Down Expand Up @@ -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 {
Expand All @@ -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())
}
Expand Down

0 comments on commit 614caf5

Please sign in to comment.