Skip to content

Commit

Permalink
Added cli tool. 👔
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbeutler committed Jul 26, 2024
1 parent 8dbfd2d commit 0636f5e
Show file tree
Hide file tree
Showing 14 changed files with 263 additions and 33 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Release 🚀

on:
release:
types: [published]

jobs:
release:
name: Release
runs-on: ubuntu-latest
timeout-minutes: 15
permissions:
contents: write
packages: write
attestations: write

steps:
- name: Check out the repo
uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: 1.22

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v4
with:
version: '${{ github.event.release.tag_name }}'
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@

# Exclude all .env files except .env.example
.env*
!.env.example
!.env.example

decoder
dist/
46 changes: 46 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# This is an example .goreleaser.yml file with some sensible defaults.
# Make sure to check the documentation at https://goreleaser.com

# The lines below are called `modelines`. See `:help modeline`
# Feel free to remove those if you don't want/need to use them.
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
# vim: set ts=2 sw=2 tw=0 fo=cnqoj

version: 2

before:
hooks:
# You may remove this if you don't use go modules.
- go mod tidy
# you may remove this if you don't need go generate
- go generate ./...

builds:
- env:
- CGO_ENABLED=0
goos:
- linux
- windows
- darwin

archives:
- format: tar.gz
# this name template makes the OS and Arch compatible with the results of `uname`.
name_template: >-
{{ .ProjectName }}_
{{- title .Os }}_
{{- if eq .Arch "amd64" }}x86_64
{{- else if eq .Arch "386" }}i386
{{- else }}{{ .Arch }}{{ end }}
{{- if .Arm }}v{{ .Arm }}{{ end }}
# use zip for windows archives
format_overrides:
- goos: windows
format: zip

changelog:
sort: asc
filters:
exclude:
- "^docs:"
- "^test:"
1 change: 1 addition & 0 deletions .testcoverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ exclude:
# - ^pkg/bar # exclude package `pkg/bar`
paths:
- ^cmd/
- ^examples/


# NOTES:
Expand Down
21 changes: 21 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
Use: "decoder",
Short: "truvami payload decoder cli helper",
Long: `A CLI tool to help decode truvami payloads.`,
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
17 changes: 17 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cmd

import "testing"

func TestExecute(t *testing.T) {
Execute()
}

func TestRootCmd(t *testing.T) {
// should panic because of nil values
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
rootCmd.Run(nil, nil)
}
46 changes: 46 additions & 0 deletions cmd/tagsl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cmd

import (
"encoding/json"
"fmt"
"strconv"

"github.com/spf13/cobra"
"github.com/truvami/decoder/pkg/decoder/tagsl/v1"
)

func init() {
rootCmd.AddCommand(tagslCmd)
}

var tagslCmd = &cobra.Command{
Use: "tagsl [port] [payload]",
Short: "decode tag S / L payloads",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
d := tagsl.NewTagSLv1Decoder()

port, err := strconv.Atoi(args[0])
if err != nil {
fmt.Printf("error parsing port: %v", err)
return
}

data, err := d.Decode(args[1], int16(port), "")
if err != nil {
fmt.Printf("error decoding data: %v", err)
return
}

printJSON(data)
},
}

func printJSON(data interface{}) {
j, err := json.Marshal(data)
if err != nil {
panic(err)
}

fmt.Println(string(j))
}
32 changes: 32 additions & 0 deletions cmd/tagsl_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cmd

import (
"bytes"
"log"
"strings"
"testing"
)

func TestPrintJSON(t *testing.T) {
// Test input
data := map[string]interface{}{
"key1": "value1",
"key2": 2,
"key3": true,
}

// Expected output
expectedOutput := `{"key1":"value1","key2":2,"key3":true}`

// Capture the output of the printJSON function
var capturedOutput bytes.Buffer
log.SetOutput(&capturedOutput)

// Call the function
printJSON(data)

// Check if the output matches the expected output
if !strings.Contains(capturedOutput.String(), expectedOutput) {
t.Errorf("printJSON output = %s, expected %s", capturedOutput.String(), expectedOutput)
}
}
3 changes: 2 additions & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
ignore:
- ^cmd/
- ^cmd/
- ^examples/
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
module github.com/truvami/decoder

go 1.22.0

require github.com/spf13/cobra v1.8.1

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/truvami/decoder/cmd"

func main() {
cmd.Execute()
}
7 changes: 7 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "testing"

func TestMain(t *testing.T) {
main()
}
62 changes: 31 additions & 31 deletions pkg/decoder/tagsl/v1/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,37 +41,6 @@ func TestDecode(t *testing.T) {
Moving: true,
},
},
{
payload: "808c59c3c99fc0ad",
port: 5,
expected: Port5Payload{
Moving: false,
Mac1: "8c59c3c99fc0",
Rssi1: -83,
},
},
{
payload: "80e0286d8a2742a1",
port: 5,
expected: Port5Payload{
Moving: false,
Mac1: "e0286d8a2742",
Rssi1: -95,
},
},
{
payload: "001f3fd57cecb4f0b0140c96bbb2e0286d8a9478b8",
port: 5,
expected: Port5Payload{
Moving: false,
Mac1: "1f3fd57cecb4",
Rssi1: -16,
Mac2: "b0140c96bbb2",
Rssi2: -32,
Mac3: "286d8a9478b8",
Rssi3: 0,
},
},
{
payload: "822f0101f052fab920feafd0e4158b38b9afe05994cb2f5cb2",
port: 3,
Expand Down Expand Up @@ -121,6 +90,37 @@ func TestDecode(t *testing.T) {
BatteryKeepAliveMessageInterval: 9000,
},
},
{
payload: "808c59c3c99fc0ad",
port: 5,
expected: Port5Payload{
Moving: false,
Mac1: "8c59c3c99fc0",
Rssi1: -83,
},
},
{
payload: "80e0286d8a2742a1",
port: 5,
expected: Port5Payload{
Moving: false,
Mac1: "e0286d8a2742",
Rssi1: -95,
},
},
{
payload: "001f3fd57cecb4f0b0140c96bbb2e0286d8a9478b8",
port: 5,
expected: Port5Payload{
Moving: false,
Mac1: "1f3fd57cecb4",
Rssi1: -16,
Mac2: "b0140c96bbb2",
Rssi2: -32,
Mac3: "286d8a9478b8",
Rssi3: 0,
},
},
{
payload: "01",
port: 6,
Expand Down

0 comments on commit 0636f5e

Please sign in to comment.