From c0192d09cad9a0b1f3f35a0c4578b78f55d599ec Mon Sep 17 00:00:00 2001 From: Nate Bracy Date: Thu, 21 Dec 2023 08:37:16 -0500 Subject: [PATCH 1/4] refactor quoteFile to io.Reader, io.Writer interfaces --- commands.go | 2 +- main.go | 9 +++++++-- quotes.go | 18 +++++++----------- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/commands.go b/commands.go index ace29b3..9267d4b 100644 --- a/commands.go +++ b/commands.go @@ -125,7 +125,7 @@ var handlers = map[string]func(*discordgo.Session, *discordgo.InteractionCreate) return } - err := writeQuote(quote) + err := writeQuote(quoteFile, quote) if err != nil { sendError(s, d, err) return diff --git a/main.go b/main.go index 8d9cc69..11736ff 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,8 @@ import ( "github.com/joho/godotenv" ) +var quoteFile *os.File + func main() { log.Println("Starting BoydBot...") if err := godotenv.Load(); err != nil { @@ -30,13 +32,16 @@ func main() { } // open the quotes file - quoteFile, err = os.OpenFile("quotes.txt", os.O_RDWR|os.O_APPEND|os.O_CREATE, 0644) + quoteFile, err := os.OpenFile("quotes.txt", os.O_RDWR|os.O_APPEND|os.O_CREATE, 0644) if err != nil { panic(err) } + defer quoteFile.Close() // Load the quotes - loadQuotes() + log.Println("Loading quotes...") + loadQuotes(quoteFile) + log.Println("Loaded", len(quoteList), "quotes") // Index quotes indexQuotes() diff --git a/quotes.go b/quotes.go index 93e8c36..5f0e211 100644 --- a/quotes.go +++ b/quotes.go @@ -2,27 +2,23 @@ package main import ( "bufio" - "log" - "os" + "io" ) var quoteList []string -var quoteFile *os.File -// Loads the quote list from a file -func loadQuotes() { - log.Println("Loading quotes...") +// Loads the quote list from an input stream +func loadQuotes(input io.Reader) { quoteList = make([]string, 0) - scanner := bufio.NewScanner(bufio.NewReader(quoteFile)) + scanner := bufio.NewScanner(bufio.NewReader(input)) for scanner.Scan() { quoteList = append(quoteList, scanner.Text()) } - log.Println("Loaded", len(quoteList), "quotes") } -// Saves a quote to the database -func writeQuote(quote string) error { - _, err := quoteFile.WriteString(quote + "\n") +// Writes a quote to an output stream +func writeQuote(output io.Writer, quote string) error { + _, err := output.Write([]byte(quote + "\n")) return err } From acd6732b34f2fae2a58d10559948ce728ecd7ae0 Mon Sep 17 00:00:00 2001 From: Nate Bracy Date: Thu, 21 Dec 2023 08:41:41 -0500 Subject: [PATCH 2/4] simplified theory generation --- theory.go | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/theory.go b/theory.go index 53fe34c..de501a8 100644 --- a/theory.go +++ b/theory.go @@ -23,21 +23,13 @@ func buildSentence(asideChance uint32, interjectionChance uint32) string { aside := [2]string{"Visiting hours are over!", "Why does that hydrant keep looking at me?"} interjection := [15]string{"*chuckles*", "(Ho ho!)", "(Wait...)", "(Uh...)", "(Um...)", "*cough*", "(Uh...)", "(Hmm...)", "(Ha!)", "(Yeah, yeah, yeah...)", "(What?)", "(No, no, nonono...)", "(Okay, okay but...)", "(Huh?)", "(Oh-hoh, RIGHT...)"} - sentence := "" if randGen.Uint32()%asideChance == 0 { - sentence = aside[rand.Uint32()%2] - return sentence - } - if randGen.Uint32()%interjectionChance == 0 { - sentence = interjection[rand.Uint32()%15] - return sentence - } - - if randGen.Int()%2 == 0 { - sentence = subjects[rand.Int()%40] + " " + transitiveVerb[rand.Int()%12] + " " + object[rand.Int()%17] + " " + conclution[rand.Int()%9] + return aside[rand.Uint32()%2] + } else if randGen.Uint32()%interjectionChance == 0 { + return interjection[rand.Uint32()%15] + } else if randGen.Int()%2 == 0 { + return subjects[rand.Int()%40] + " " + transitiveVerb[rand.Int()%12] + " " + object[rand.Int()%17] + " " + conclution[rand.Int()%9] } else { - sentence = subjects[rand.Uint32()%40] + " " + subjectConnector[rand.Uint32()%8] + " " + subjects[rand.Uint32()%39] + " " + intransitiveVerb[rand.Uint32()%17] + " " + preposition[rand.Uint32()%7] + " " + object[rand.Uint32()%17] + return subjects[rand.Uint32()%40] + " " + subjectConnector[rand.Uint32()%8] + " " + subjects[rand.Uint32()%39] + " " + intransitiveVerb[rand.Uint32()%17] + " " + preposition[rand.Uint32()%7] + " " + object[rand.Uint32()%17] } - - return sentence } From 884b166a16099fd39fcfe45fb7cc7c050e59ce0e Mon Sep 17 00:00:00 2001 From: Nate Bracy Date: Thu, 21 Dec 2023 09:01:17 -0500 Subject: [PATCH 3/4] refactor to multistage Dockerfile --- Dockerfile | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index 92a2863..ec8d4b6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,12 +1,13 @@ -# The base go-image -FROM golang:1.21-alpine +# Builder image +FROM golang:1.21-alpine as build RUN mkdir /app COPY . /app - WORKDIR /app +RUN go build -RUN go build -v +# Server image +FROM gcr.io/distroless/static-debian12 -# Run the server executable +COPY --from=build /app/boyd_cooper /app/boyd_cooper CMD [ "/app/boyd_cooper" ] From 9d808fb5acd00b053875f192b24916cc55469013 Mon Sep 17 00:00:00 2001 From: Nate Bracy Date: Thu, 21 Dec 2023 10:12:46 -0500 Subject: [PATCH 4/4] fix Dockerfile --- Dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index ec8d4b6..5fef17e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,10 +4,11 @@ FROM golang:1.21-alpine as build RUN mkdir /app COPY . /app WORKDIR /app -RUN go build +RUN go build -v # Server image FROM gcr.io/distroless/static-debian12 COPY --from=build /app/boyd_cooper /app/boyd_cooper +WORKDIR /app CMD [ "/app/boyd_cooper" ]