Skip to content

Commit

Permalink
Refactoring to a library and a command.
Browse files Browse the repository at this point in the history
  • Loading branch information
dherbst committed Oct 13, 2019
1 parent 9ebc0af commit 923431a
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 29 deletions.
10 changes: 2 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
FROM alpine:latest

RUN mkdir /usr/local/septabot
WORKDIR /usr/local/septabot
COPY bin/septa /usr/local/bin/septa

COPY bin/septabot /usr/local/bin/septabot

EXPOSE 8080
ENTRYPOINT ["/usr/local/bin/septabot"]

CMD ["--help"]
CMD septa
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# go-septa
Septa alerting slack bot
# septa
Septa command line for getting the next to arrive.

## Commands

### septa nta <src> <dest>
### septa next <src> <dest>
Returns the next to arrive between the src and dest stations.

### septa stations
TODO: will list the stations.
13 changes: 13 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package septa

import "time"

// Client is used to make calls to the septa website.
type Client struct {

// Domain is the api domain.
Domain string

// Timeout is the number of seconds before the call times out.
Timeout time.Duration
}
14 changes: 14 additions & 0 deletions cmd/septa/next.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"context"
"flag"
"fmt"
)

// NextToArrive takes a "from" station name and a "to" station name and returns the expected trains.
func NextToArrive(ctx context.Context) {
from := flag.Arg(1)
to := flag.Arg(2)
fmt.Printf("from=%v to=%v\n", from, to)
}
47 changes: 47 additions & 0 deletions cmd/septa/septa.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"context"
"flag"
"fmt"
)

var funcMap map[string]func(context.Context)

func init() {
funcMap = map[string]func(context.Context){
"help": Usage,
"next": NextToArrive,
}
}

// Usage prints how to invoke `septa` from the command line.
func Usage(ctx context.Context) {
fmt.Printf(`
Usage:
septa next [from-station] [to-station]
`)

}

func main() {
flag.Parse()

ctx := context.Background()

command := flag.Arg(0)
if command == "" || command == "help" {
Usage(ctx)
return
}

f := funcMap[command]
if f == nil {
fmt.Println("Unknown command")
Usage(ctx)
return
}

f(ctx)
}
48 changes: 30 additions & 18 deletions makefile
Original file line number Diff line number Diff line change
@@ -1,32 +1,44 @@
.PHONY: *
.PHONY: all clean pull get build build-in-container image

GOLANG = golang:1.10
GOOS = darwin
GOLANG := golang:1.13
GOOS := darwin

all: clean pull build septaimage
all: clean pull lint sec build install

clean:
rm -rf vendor/
mkdir -p bin
rm -f bin/septabot bin/narb bin/sub || true
rm -f bin/septa || true

get:
mkdir -p vendor

pull:
docker pull ${GOLANG}

lint:
docker run -i --rm -v ${PWD}:/go/src/github.com/dherbst/septa -w /go/src/github.com/dherbst/septa ${GOLANG} make lint-in-container

lint-in-container:
go get -u golang.org/x/lint/golint
golint github.com/dherbst/septa
golint github.com/dherbst/septa/cmd/septa/...

sec:
docker run -it --rm -v ${PWD}:/go/src/github.com/dherbst/septa -w /go/src/github.com/dherbst/septa ${GOLANG} make sec-in-container

sec-in-container:
go get -u github.com/securego/gosec/cmd/gosec
gosec .

build:
docker run -i --rm -v "$(PWD)":/usr/src/myapp -w /usr/src/myapp ${GOLANG} make build-in-container
docker run -i --rm -v "$(PWD)":/go/src/github.com/dherbst/septa -w /go/src/github.com/dherbst/septa ${GOLANG} make build-in-container

build-in-container:
GOPATH=/usr/src/myapp go vet septabot
GOPATH=/usr/src/myapp go test -coverprofile=coverage.out septabot
GOPATH=/usr/src/myapp go tool cover -html=coverage.out -o coverage.html
GOOS=${GOOS} GOPATH=/usr/src/myapp go build -o bin/septabot /usr/src/myapp/src/septabot/cmd/septabot/septabot.go
GOOS=${GOOS} GOPATH=/usr/src/myapp go build -o bin/septa /usr/src/myapp/src/septabot/cmd/septa/septa.go
GOOS=${GOOS} GOPATH=/usr/src/myapp go build -o bin/narb /usr/src/myapp/src/septabot/cmd/narb/narb.go
GOOS=${GOOS} GOPATH=/usr/src/myapp go build -o bin/sub /usr/src/myapp/src/septabot/cmd/sub/sub.go

GOOS=darwin go build -o bin/septa cmd/septa/*.go

septaimage: build
docker build -t septabot:latest .
install:
cp bin/septa ~/bin/septa

run:
docker run -p 8080:8080 -d septabot:latest
image: build
docker build -t septa:latest .

0 comments on commit 923431a

Please sign in to comment.