feat: add move

This commit is contained in:
Dan Anglin 2022-10-29 23:22:52 +01:00
parent f279056f25
commit 9f51b5e7d1
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
4 changed files with 113 additions and 0 deletions

View file

@ -0,0 +1,4 @@
*
!*.go
!go.sum
!go.mod

View file

@ -0,0 +1,22 @@
# Build the application
FROM docker.io/library/golang:1.19.2-alpine3.16@sha256:46752c2ee3bd8388608e41362964c84f7a6dffe99d86faeddc82d917740c5968 AS builder
ENV GOOS=linux
ENV GOARCH=amd64
ENV CGO_ENABLED=0
WORKDIR /build
COPY . .
RUN \
go mod tidy \
&& go build -ldflags="-s -w" -a -o /build/move .
FROM scratch
COPY --from=builder /build/move /move
USER 3000:3000
ENTRYPOINT ["/move"]

View file

@ -0,0 +1,60 @@
package main
import (
"fmt"
"strings"
"errors"
"flag"
"os"
)
//go:generate stringer -type=Movement
type Movement int
const (
Unknown Movement = iota
Forward
Backward
Left
Right
)
func newMovement(movement string) (Movement) {
switch strings.ToLower(movement) {
case "forward":
return Forward
case "backward":
return Backward
case "left":
return Left
case "right":
return Right
default:
return Unknown
}
}
func main() {
direction := flag.String("direction", "", "The direction to move.")
flag.Parse()
if err := run(*direction); err != nil {
fmt.Printf("Error: %v.\n", err)
os.Exit(1)
}
}
func run(direction string) error {
if len(direction) == 0 {
return errors.New("direction is not set")
}
move := newMovement(direction)
if move == Unknown {
return errors.New("unknown direction")
}
fmt.Printf("I moved %s.\n", move)
return nil
}

View file

@ -0,0 +1,27 @@
// Code generated by "stringer -type=Movement"; DO NOT EDIT.
package main
import "strconv"
func _() {
// An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again.
var x [1]struct{}
_ = x[Unknown-0]
_ = x[Forward-1]
_ = x[Backward-2]
_ = x[Left-3]
_ = x[Right-4]
}
const _Movement_name = "UnknownForwardBackwardLeftRight"
var _Movement_index = [...]uint8{0, 7, 14, 22, 26, 31}
func (i Movement) String() string {
if i < 0 || i >= Movement(len(_Movement_index)-1) {
return "Movement(" + strconv.FormatInt(int64(i), 10) + ")"
}
return _Movement_name[_Movement_index[i]:_Movement_index[i+1]]
}