Compare commits

...

6 Commits

Author SHA1 Message Date
e68d22337b cmd: add die roller 2024-06-14 20:27:00 -07:00
4cb6f5b6f0 fix minor issues in Print calls. 2024-05-19 20:51:35 -07:00
6d5708800f circleci: Some days I really hate computers. 2024-05-19 20:48:59 -07:00
fa3eb821e6 circleci: Update go container. 2024-05-19 20:48:25 -07:00
dd5ed403b9 circleci: Update go version. 2024-05-19 20:46:28 -07:00
b4fde22c31 circleci: don't use bazel 2024-05-19 20:45:32 -07:00
4 changed files with 53 additions and 22 deletions

View File

@@ -2,22 +2,6 @@
# See: https://circleci.com/docs/2.0/configuration-reference # See: https://circleci.com/docs/2.0/configuration-reference
version: 2.1 version: 2.1
commands:
setup-bazel:
description: |
Setup the Bazel build system used for building the repo
steps:
- run:
name: Add Bazel Apt repository
command: |
sudo apt install curl gnupg
curl -fsSL https://bazel.build/bazel-release.pub.gpg | gpg --dearmor > bazel.gpg
sudo mv bazel.gpg /etc/apt/trusted.gpg.d/
echo "deb [arch=amd64] https://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
- run:
name: Install Bazel from Apt
command: sudo apt update && sudo apt install bazel
# Define a job to be invoked later in a workflow. # Define a job to be invoked later in a workflow.
# See: https://circleci.com/docs/2.0/configuration-reference/#jobs # See: https://circleci.com/docs/2.0/configuration-reference/#jobs
jobs: jobs:
@@ -26,12 +10,11 @@ jobs:
# Specify the execution environment. You can specify an image from Dockerhub or use one of our Convenience Images from CircleCI's Developer Hub. # Specify the execution environment. You can specify an image from Dockerhub or use one of our Convenience Images from CircleCI's Developer Hub.
# See: https://circleci.com/docs/2.0/configuration-reference/#docker-machine-macos-windows-executor # See: https://circleci.com/docs/2.0/configuration-reference/#docker-machine-macos-windows-executor
docker: docker:
- image: circleci/golang:1.15.8 - image: cimg/go:1.22.2
# Add steps to the job # Add steps to the job
# See: https://circleci.com/docs/2.0/configuration-reference/#steps # See: https://circleci.com/docs/2.0/configuration-reference/#steps
steps: steps:
- checkout - checkout
- setup-bazel
- restore_cache: - restore_cache:
keys: keys:
- go-mod-v4-{{ checksum "go.sum" }} - go-mod-v4-{{ checksum "go.sum" }}
@@ -44,10 +27,10 @@ jobs:
- "/go/pkg/mod" - "/go/pkg/mod"
- run: - run:
name: Run tests name: Run tests
command: bazel test //... command: go test ./...
- run: - run:
name: Run build name: Run build
command: bazel build //... command: go build ./...
- store_test_results: - store_test_results:
path: /tmp/test-reports path: /tmp/test-reports

View File

@@ -35,7 +35,7 @@ func main() {
for _, arg := range flag.Args() { for _, arg := range flag.Args() {
if err := lookupHost(arg); err != nil { if err := lookupHost(arg); err != nil {
log.Println("%s: %s", arg, err) log.Printf("%s: %s", arg, err)
} }
} }
} }

48
cmd/rolldie/main.go Normal file
View File

@@ -0,0 +1,48 @@
package main
import (
"flag"
"fmt"
"math/rand"
"os"
"regexp"
"strconv"
"git.wntrmute.dev/kyle/goutils/die"
)
var dieRollFormat = regexp.MustCompile(`^(\d+)[dD](\d+)$`)
func rollDie(count, sides int) []int {
sum := 0
var rolls []int
for i := 0; i < count; i++ {
roll := rand.Intn(sides) + 1
sum += roll
rolls = append(rolls, roll)
}
rolls = append(rolls, sum)
return rolls
}
func main() {
flag.Parse()
for _, arg := range flag.Args() {
if !dieRollFormat.MatchString(arg) {
fmt.Fprintf(os.Stderr, "invalid die format %s: should be XdY\n", arg)
os.Exit(1)
}
dieRoll := dieRollFormat.FindAllStringSubmatch(arg, -1)
count, err := strconv.Atoi(dieRoll[0][1])
die.If(err)
sides, err := strconv.Atoi(dieRoll[0][2])
die.If(err)
fmt.Println(rollDie(count, sides))
}
}

View File

@@ -68,7 +68,7 @@ func showFile(path string) {
func searchFile(path string, search *regexp.Regexp) error { func searchFile(path string, search *regexp.Regexp) error {
file, err := os.Open(path) file, err := os.Open(path)
if err != nil { if err != nil {
errorf("%v") errorf("%v", err)
return err return err
} }
defer file.Close() defer file.Close()