Skip to content

Commit

Permalink
Big clean up (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
jjbubudi authored Nov 9, 2019
1 parent 3daeb42 commit 5e07d0b
Show file tree
Hide file tree
Showing 13 changed files with 318 additions and 246 deletions.
118 changes: 118 additions & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
---
kind: pipeline
name: amd64

platform:
os: linux
arch: amd64

steps:
- name: test
image: golang:1.12.5
commands:
- make ci

- name: docker-publish
image: plugins/docker
settings:
username:
from_secret: docker_username
password:
from_secret: docker_password
repo: "jjbubudi/tides"
tag: "${DRONE_TAG}-amd64"
when:
ref:
- refs/head/master
- refs/tags/*
event:
- tag

---
kind: pipeline
name: arm64

platform:
os: linux
arch: arm64

steps:
- name: test
image: golang:1.12.5
commands:
- make ci

- name: docker-publish
image: plugins/docker
settings:
username:
from_secret: docker_username
password:
from_secret: docker_password
repo: "jjbubudi/tides"
tag: "${DRONE_TAG}-arm64"
when:
ref:
- refs/head/master
- refs/tags/*
event:
- tag

---
kind: pipeline
name: arm

platform:
os: linux
arch: arm

steps:
- name: test
image: golang:1.12.5
commands:
- make ci

- name: docker-publish
image: plugins/docker
settings:
username:
from_secret: docker_username
password:
from_secret: docker_password
repo: "jjbubudi/tides"
tag: "${DRONE_TAG}-arm"
when:
ref:
- refs/head/master
- refs/tags/*
event:
- tag

---
kind: pipeline
name: manifest
steps:
- name: manifest
image: plugins/manifest:1.0.2
settings:
username:
from_secret: docker_username
password:
from_secret: docker_password
platforms:
- linux/amd64
- linux/arm64
- linux/arm
target: "jjbubudi/tides:${DRONE_TAG}"
template: "jjbubudi/tides:${DRONE_TAG}-ARCH"
when:
ref:
- refs/head/master
- refs/tags/*
event:
- tag

depends_on:
- amd64
- arm64
- arm
37 changes: 0 additions & 37 deletions .travis.yml

This file was deleted.

3 changes: 0 additions & 3 deletions .vscode/settings.json

This file was deleted.

3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
ARG ARCH
FROM multiarch/alpine:${ARCH}-v3.9
FROM alpine:3.10.3

RUN apk add --update tzdata ca-certificates && \
rm -rf /var/cache/apk/*
Expand Down
37 changes: 2 additions & 35 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,16 @@ export CGO_ENABLED = 0
export GOOS = $(shell go env GOOS)
export GOARCH = $(shell go env GOARCH)

GO := go
ARCH := $(GOARCH)
ifeq ($(ARCH), arm)
ARCH = armhf
endif

DOCKER_IMAGE := jjbubudi/tides
TAG ?= $(shell git describe --tags --exact-match 2>/dev/null)
COMMIT ?= $(shell git rev-parse --short HEAD)
VERSION = $(COMMIT)
ifneq ($(TAG),)
VERSION = $(TAG)
endif

.PHONY: build
build:
@$(GO) build -o dist/tides cmd/main.go

.PHONY: build-docker
build-docker: build
@docker build --build-arg ARCH=$(ARCH) -t $(DOCKER_IMAGE):$(VERSION)-$(GOARCH) .
@docker tag $(DOCKER_IMAGE):$(VERSION)-$(GOARCH) $(DOCKER_IMAGE):$(VERSION)
@docker tag $(DOCKER_IMAGE):$(VERSION)-$(GOARCH) $(DOCKER_IMAGE):latest

ifeq ($(PUSH), true)
@docker push $(DOCKER_IMAGE):$(VERSION)-$(GOARCH)
endif

.PHONY: push-manifest
push-manifest:
@curl https://github.com/estesp/manifest-tool/releases/download/v0.9.0/manifest-tool-linux-amd64 -L -s -o manifest-tool
@chmod +x manifest-tool
@./manifest-tool push from-args \
--platforms linux/arm,linux/arm64,linux/amd64 \
--template "$(DOCKER_IMAGE):$(VERSION)-ARCH" \
--target "$(DOCKER_IMAGE):$(VERSION)"
@go build -o dist/tides cmd/main.go

.PHONY: ci
ci: build test

.PHONY: test
test:
@$(GO) test ./...
@go test ./...

.PHONY: clean
clean:
Expand Down
92 changes: 92 additions & 0 deletions cmd/fetch/fetch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package fetch

import (
"net/http"
"time"

"github.com/jjbubudi/tides/internal"
"github.com/jjbubudi/tides/internal/observatory"
"github.com/jjbubudi/tides/internal/tides"
stan "github.com/nats-io/stan.go"
"github.com/urfave/cli"
)

const (
pNatsURL = "nats-url"
pNatsClusterID = "nats-cluster-id"
pNatsClientID = "nats-client-id"
)

// NewFetchCommand creates a new fetch command
func NewFetchCommand() cli.Command {
return cli.Command{
Name: "fetch",
Usage: "Fetches tidal data",
Subcommands: []cli.Command{
cli.Command{
Name: "realtime",
Usage: "Realtime tidal data",
Flags: publisherFlags(),
Action: func(c *cli.Context) error {
nats, err := internal.ConnectToNats(
c.String(pNatsClusterID),
c.String(pNatsClientID),
c.String(pNatsURL),
)
if err != nil {
return err
}
defer nats.Close()
return publish(nats, true)
},
},
cli.Command{
Name: "predicted",
Usage: "Predicted tidal data for today",
Flags: publisherFlags(),
Action: func(c *cli.Context) error {
nats, err := internal.ConnectToNats(
c.String(pNatsClusterID),
c.String(pNatsClientID),
c.String(pNatsURL),
)
if err != nil {
return err
}
defer nats.Close()
return publish(nats, false)
},
},
},
}
}

func publisherFlags() []cli.Flag {
return []cli.Flag{
cli.StringFlag{
Name: pNatsURL,
EnvVar: internal.AutoEnv(pNatsURL),
Required: true,
},
cli.StringFlag{
Name: pNatsClusterID,
EnvVar: internal.AutoEnv(pNatsClusterID),
Required: true,
},
cli.StringFlag{
Name: pNatsClientID,
EnvVar: internal.AutoEnv(pNatsClientID),
Required: true,
},
}
}

func publish(nats stan.Conn, realtime bool) error {
hongKong, _ := time.LoadLocation("Asia/Hong_Kong")
observatory := observatory.New(&http.Client{})
tidesService := tides.New(observatory.TidalDataAsOf, observatory.TidalPredictionsAsOf, nats.Publish)
if realtime {
return tidesService.PublishRealTimeTidalData(time.Now().In(hongKong))
}
return tidesService.PublishTidalPredictions(time.Now().In(hongKong))
}
28 changes: 11 additions & 17 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,23 @@ package main

import (
"log"
"strings"
"os"

"github.com/jjbubudi/tides/cmd/publisher"
"github.com/jjbubudi/tides/cmd/fetch"
"github.com/jjbubudi/tides/cmd/server"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/urfave/cli"
)

func main() {
rootCommand := &cobra.Command{
Use: "tides",
app := cli.NewApp()
app.Name = "tides"
app.HelpName = "tides"
app.Usage = "Fetches tidal data from the Hong Kong Observatory"
app.Commands = []cli.Command{
server.NewServerCommand(),
fetch.NewFetchCommand(),
}
rootCommand.PersistentFlags().String("nats-url", "nats://127.0.0.1:4222", "Nats URL to connect to")
rootCommand.PersistentFlags().String("nats-cluster-id", "test-cluster", "Nats cluster ID")
rootCommand.PersistentFlags().String("nats-client-id", "test-client", "Nats client ID")
viper.BindPFlags(rootCommand.PersistentFlags())
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))

rootCommand.AddCommand(publisher.NewPublisherCommand())
rootCommand.AddCommand(server.NewServerCommand())

if err := rootCommand.Execute(); err != nil {
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
Loading

0 comments on commit 5e07d0b

Please sign in to comment.