Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Linting
Browse files Browse the repository at this point in the history
  • Loading branch information
myoung34 committed Aug 25, 2022
1 parent 6b8e010 commit 6e6cf3a
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 43 deletions.
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
linters:
enable:
- golint
issues:
exclude:
- "Error return value of .* is not checked"
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ repos:
- id: check-merge-conflict
exclude: ^vendor/
- id: detect-private-key
- repo: https://github.com/golangci/golangci-lint
rev: v1.49.0
hooks:
- id: golangci-lint
exclude: ^vendor/
- repo: https://github.com/hadolint/hadolint
rev: v2.10.0
hooks:
Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ RUN go build -o /usr/local/bin/tilty main.go
FROM alpine:3.16
LABEL maintainer="[email protected]"

# hadolint ignore=DL3018
RUN apk add -U --no-cache bluez

COPY --from=builder /usr/local/bin/tilty /usr/local/bin/tilty
Expand Down
7 changes: 3 additions & 4 deletions emitters/datadog.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import (
"encoding/json"
"fmt"
"github.com/DataDog/datadog-go/v5/statsd"
"github.com/go-kit/kit/log/level"
_ "github.com/mattn/go-sqlite3"
"github.com/go-kit/log/level"
"github.com/myoung34/tilty/tilt"
)

Expand All @@ -15,7 +14,7 @@ type Datadog struct {
StatsdPort int `json:"statsd_port"`
}

func DatadogEmitWithClient(payload tilt.TiltPayload, emitterConfig interface{}, client statsd.ClientInterface) (string, error) {
func DatadogEmitWithClient(payload tilt.Payload, emitterConfig interface{}, client statsd.ClientInterface) (string, error) {

defer client.Close()

Expand All @@ -39,7 +38,7 @@ func DatadogEmitWithClient(payload tilt.TiltPayload, emitterConfig interface{},
return "", nil
}

func DatadogEmit(payload tilt.TiltPayload, emitterConfig interface{}) (string, error) {
func DatadogEmit(payload tilt.Payload, emitterConfig interface{}) (string, error) {
datadog := Datadog{}
jsonString, _ := json.Marshal(emitterConfig)
json.Unmarshal(jsonString, &datadog)
Expand Down
4 changes: 2 additions & 2 deletions emitters/datadog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ func TestDatadogEmit(t *testing.T) {
sampleConfig.ConfigData.Set("datadog.statsd_host", "testing")
sampleConfig.ConfigData.Set("datadog.statsd_port", "8125")

payload := tilt.TiltPayload{
Id: "0987654321",
payload := tilt.Payload{
ID: "0987654321",
Mac: "66:77:88:99:00",
Color: "BLACK",
Major: 65,
Expand Down
6 changes: 3 additions & 3 deletions emitters/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"database/sql"
"encoding/json"
"fmt"
"github.com/go-kit/kit/log/level"
_ "github.com/mattn/go-sqlite3"
"github.com/go-kit/log/level"
_ "github.com/mattn/go-sqlite3" // Per docs
"github.com/myoung34/tilty/tilt"
"log"
)
Expand All @@ -15,7 +15,7 @@ type SQLite struct {
File string
}

func SQLiteEmit(payload tilt.TiltPayload, emitterConfig interface{}) (string, error) {
func SQLiteEmit(payload tilt.Payload, emitterConfig interface{}) (string, error) {
sqlite := SQLite{}
jsonString, _ := json.Marshal(emitterConfig)
json.Unmarshal(jsonString, &sqlite)
Expand Down
4 changes: 2 additions & 2 deletions emitters/sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ func TestSQLite(t *testing.T) {
sampleConfig.ConfigData.Set("sqlite.enabled", true)
sampleConfig.ConfigData.Set("sqlite.file", "foo.db")

payload := tilt.TiltPayload{
Id: "0987654321",
payload := tilt.Payload{
ID: "0987654321",
Mac: "66:77:88:99:00",
Color: "BLACK",
Major: 65,
Expand Down
10 changes: 5 additions & 5 deletions emitters/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/go-kit/kit/log/level"
"github.com/go-kit/log/level"
"github.com/myoung34/tilty/tilt"
"io"
"net/http"
Expand All @@ -14,18 +14,18 @@ import (

type Webhook struct {
Enabled bool
Url string
URL string
Headers string
Template string
Method string
}

func WebhookEmit(payload tilt.TiltPayload, emitterConfig interface{}) (string, error) {
func WebhookEmit(payload tilt.Payload, emitterConfig interface{}) (string, error) {
webhook := Webhook{}
jsonString, _ := json.Marshal(emitterConfig)
json.Unmarshal(jsonString, &webhook)

level.Info(tilt.Logger).Log("emitters.webhook", fmt.Sprintf("%s", webhook.Url))
level.Info(tilt.Logger).Log("emitters.webhook", webhook.URL)
level.Info(tilt.Logger).Log("emitters.webhook", fmt.Sprintf("%v", webhook.Enabled))
level.Info(tilt.Logger).Log("emitters.webhook", fmt.Sprintf("%+v", webhook.Headers))
level.Info(tilt.Logger).Log("emitters.webhook", fmt.Sprintf("%+v", webhook.Template))
Expand Down Expand Up @@ -59,7 +59,7 @@ func WebhookEmit(payload tilt.TiltPayload, emitterConfig interface{}) (string, e
bodyReader := bytes.NewReader(tpl.Bytes())

// Set up the request
req, err := http.NewRequest(webhook.Method, webhook.Url, bodyReader)
req, err := http.NewRequest(webhook.Method, webhook.URL, bodyReader)
if err != nil {
level.Error(tilt.Logger).Log("emitters.webhook", err)
return "", err
Expand Down
20 changes: 10 additions & 10 deletions emitters/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (

type TiltWebhookTest struct {
Type string
Payload tilt.TiltPayload
Payload tilt.Payload
Enabled bool
Url string
URL string
Headers string
Template string
Method string
Expand Down Expand Up @@ -43,8 +43,8 @@ func TestWebhook(t *testing.T) {
name: "POST",
in: TiltWebhookTest{
Type: "webhook",
Payload: tilt.TiltPayload{
Id: "1234567890",
Payload: tilt.Payload{
ID: "1234567890",
Mac: "11:22:33:44:55",
Color: "RED",
Major: 90,
Expand All @@ -53,7 +53,7 @@ func TestWebhook(t *testing.T) {
Timestamp: 1661445284,
},
Enabled: true,
Url: "http://something.com",
URL: "http://something.com",
Headers: "{\"Content-Type\": \"application/json\", \"Foo\": \"bar\"}",
Template: "{\"color\": \"{{.Color}}\", \"gravity\": {{.Gravity}}, \"mac\": \"{{.Mac}}\", \"temp\": {{.Temp}}, \"timestamp\": \"{{.Timestamp}}\", \"gravity_unit\": \"G\", \"temp_unit\": \"F\"}",
Method: "POST",
Expand All @@ -68,8 +68,8 @@ func TestWebhook(t *testing.T) {
name: "GET",
in: TiltWebhookTest{
Type: "webhook",
Payload: tilt.TiltPayload{
Id: "0987654321",
Payload: tilt.Payload{
ID: "0987654321",
Mac: "66:77:88:99:00",
Color: "BLACK",
Major: 65,
Expand All @@ -78,7 +78,7 @@ func TestWebhook(t *testing.T) {
Timestamp: 1661445284,
},
Enabled: true,
Url: "http://fake.com",
URL: "http://fake.com",
Headers: "{\"Content-Type\": \"application/json\"}",
Template: "{\"color\": \"{{.Color}}\", \"gravity\": {{.Gravity}}, \"mac\": \"{{.Mac}}\", \"temp\": {{.Temp}}, \"timestamp\": \"{{.Timestamp}}\", \"gravity_unit\": \"G\", \"temp_unit\": \"F\"}",
Method: "GET",
Expand All @@ -92,7 +92,7 @@ func TestWebhook(t *testing.T) {
}
for _, theT := range theTests {

httpmock.RegisterResponder(theT.in.Method, theT.in.Url,
httpmock.RegisterResponder(theT.in.Method, theT.in.URL,
func(req *http.Request) (*http.Response, error) {
buf := new(bytes.Buffer)
buf.ReadFrom(req.Body)
Expand All @@ -104,7 +104,7 @@ func TestWebhook(t *testing.T) {
t.Run(theT.name, func(t *testing.T) {
sampleConfig := tilt.ParseConfig("some/file/somewhere.toml")

sampleConfig.ConfigData.Set("webhook.url", theT.in.Url)
sampleConfig.ConfigData.Set("webhook.url", theT.in.URL)
sampleConfig.ConfigData.Set("webhook.headers", theT.in.Headers)
sampleConfig.ConfigData.Set("webhook.template", theT.in.Template)
sampleConfig.ConfigData.Set("webhook.method", theT.in.Method)
Expand Down
23 changes: 11 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/hex"
"errors"
"fmt"
"github.com/go-kit/kit/log/level"
"github.com/go-kit/log/level"
"github.com/go-playground/validator/v10"
"github.com/myoung34/gatt"
"github.com/myoung34/gatt/examples/option"
Expand Down Expand Up @@ -56,7 +56,7 @@ func OnStateChanged(device gatt.Device, s gatt.State) {
}
}

func NewTilt(data []byte) (tilt.TiltPayload, error) {
func NewTilt(data []byte) (tilt.Payload, error) {
// http://www.havlena.net/wp-content/themes/striking/includes/timthumb.php?src=/wp-content/uploads/ibeacon-packet.png&w=600&zc=1
//pkt = b' \x04>* \x02\x01x03\x01w\t \xbc\xd0W\xef\x1e\x02\x01\x04\x1a\xffL\x00\x02\x15 \xa4\x95\xbb0\xc5\xb1KD\xb5\x12\x13p\xf0-t\xde \x00B \x03\xf7 \xc5\xa7' # noqa
// | | | | | | | | | # noqa
Expand All @@ -65,10 +65,10 @@ func NewTilt(data []byte) (tilt.TiltPayload, error) {
// | | | mac addr | uuid | unused data | major| minor | tx | # noqa
// | | | | |
if len(data) < 25 || binary.BigEndian.Uint32(data) != 0x4c000215 {
return tilt.TiltPayload{}, errors.New("not an iBeacon")
return tilt.Payload{}, errors.New("not an iBeacon")
}
return tilt.TiltPayload{
Id: strings.ToLower(strings.Replace(strings.ToUpper(hex.EncodeToString(data[4:8])+"-"+hex.EncodeToString(data[8:10])+"-"+hex.EncodeToString(data[10:12])+"-"+hex.EncodeToString(data[12:14])+"-"+hex.EncodeToString(data[14:20])), "-", "", -1)),
return tilt.Payload{
ID: strings.ToLower(strings.Replace(strings.ToUpper(hex.EncodeToString(data[4:8])+"-"+hex.EncodeToString(data[8:10])+"-"+hex.EncodeToString(data[10:12])+"-"+hex.EncodeToString(data[12:14])+"-"+hex.EncodeToString(data[14:20])), "-", "", -1)),
Major: binary.BigEndian.Uint16(data[20:22]),
Minor: binary.BigEndian.Uint16(data[22:24]),
}, nil
Expand All @@ -77,26 +77,26 @@ func NewTilt(data []byte) (tilt.TiltPayload, error) {
func OnPeripheralDiscovered(p gatt.Peripheral, a *gatt.Advertisement, rssi int) {
_tilt, err := NewTilt(a.ManufacturerData)
if err == nil {
payload := tilt.TiltPayload{
Id: _tilt.Id,
payload := tilt.Payload{
ID: _tilt.ID,
Mac: p.ID(),
Color: tilt.TiltMap[_tilt.Id],
Color: tilt.TiltMap[_tilt.ID],
Major: _tilt.Major,
Minor: _tilt.Minor,
Rssi: rssi,
Timestamp: time.Now().UTC().Unix(),
}
err = validate.Struct(payload)
if err == nil {
level.Info(tilt.Logger).Log("main.OnPeripheralDiscovered", fmt.Sprintf("%s [%s] temp: %d gravity: %d rssi: %d", payload.Id, payload.Color, payload.Major, payload.Minor, rssi))
level.Info(tilt.Logger).Log("main.OnPeripheralDiscovered", fmt.Sprintf("%s [%s] temp: %d gravity: %d rssi: %d", payload.ID, payload.Color, payload.Major, payload.Minor, rssi))
returnStr, _ := callEmitter(fmt.Sprintf("%s.emit", config.EnabledEmitter), payload, config.ConfigData.Get(config.EnabledEmitter))
level.Info(tilt.Logger).Log("main.OnPeripheralDiscovered", returnStr)
}

}
}

func callEmitter(funcName string, payload tilt.TiltPayload, emitterConfig interface{}) (result interface{}, err error) {
func callEmitter(funcName string, payload tilt.Payload, emitterConfig interface{}) (result interface{}, err error) {
level.Info(tilt.Logger).Log("main.callEmitter", fmt.Sprintf("Attempting to call %+v", funcName))
_, ok := EmittersMap[funcName]
if !ok {
Expand All @@ -106,8 +106,7 @@ func callEmitter(funcName string, payload tilt.TiltPayload, emitterConfig interf
in := make([]reflect.Value, 2)
in[0] = reflect.ValueOf(payload)
in[1] = reflect.ValueOf(emitterConfig)
var res []reflect.Value
res = f.Call(in)
var res []reflect.Value = f.Call(in)
result = res[0].Interface()
return
}
2 changes: 1 addition & 1 deletion tilt/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package tilt
import (
"errors"
"fmt"
"github.com/go-kit/kit/log/level"
"github.com/go-kit/log/level"
"github.com/spf13/viper"
"os"
"path/filepath"
Expand Down
4 changes: 2 additions & 2 deletions tilt/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ var TiltMap = map[string]string{
"25cc0b60914de76ead903f903bfd5e53": "MIGHTY",
}

type TiltPayload struct {
Id string
type Payload struct {
ID string
Mac string
Color string `validate:"required"`
Major uint16
Expand Down
4 changes: 2 additions & 2 deletions tilt/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package tilt

import (
"fmt"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"os"
"strings"
)
Expand Down

0 comments on commit 6e6cf3a

Please sign in to comment.