-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f61f13e
Showing
13 changed files
with
785 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
name: Golang CI | ||
|
||
on: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
types: [opened, reopened, synchronize] | ||
schedule: | ||
- cron: '15 3 * * 0' | ||
|
||
jobs: | ||
build: | ||
name: Test and Build | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v3 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Setup Go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version: '1.20' | ||
|
||
- name: Fetch vendor | ||
run: go mod vendor | ||
|
||
- name: Test and build | ||
run: go run github.com/rebuy-de/rebuy-go-sdk/v5/cmd/buildutil |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
name: Release Binaries | ||
|
||
on: | ||
release: | ||
types: [created] | ||
permissions: | ||
contents: write | ||
|
||
jobs: | ||
build: | ||
name: Release | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v3 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Setup Go | ||
uses: actions/setup-go@v3 | ||
with: | ||
go-version: '1.20' | ||
|
||
- name: Fetch vendor | ||
run: go mod vendor | ||
|
||
- name: Test and build | ||
run: | | ||
go run github.com/rebuy-de/rebuy-go-sdk/v5/cmd/buildutil \ | ||
--compress \ | ||
-x linux/amd64 \ | ||
-x linux/arm64 \ | ||
-x darwin/arm64 | ||
# The symlinks cause duplicate files on the GitHub releases page. Perhaps | ||
# we should fix this in buildutil. | ||
- name: Clean dist directory | ||
run: | | ||
cd dist && rm $(find . -type l) | ||
- name: Upload binaries to release | ||
uses: svenstaro/upload-release-action@v2 | ||
with: | ||
repo_token: ${{ secrets.GITHUB_TOKEN }} | ||
file: dist/i3block-mqtt-*.tar.gz | ||
tag: ${{ github.ref }} | ||
overwrite: true | ||
file_glob: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/vendor | ||
/dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 Sven Walter | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# i3blocks-mqtt | ||
|
||
Command for templating message for MQTT to use in [i3blocks](https://github.com/vivien/i3blocks). | ||
|
||
> **Development Status:** Currently, I am only using this myself. | ||
> This project is open source, but not open for contribution, since helping contributors to ship a PR usually takes more time than doing it myself. Please drop an issue, if you want something changed. Also of course it is possible to fork the whole project. | ||
## Example | ||
|
||
``` | ||
[temperature-inside] | ||
label=In: | ||
interval=persist | ||
format=json | ||
command=i3block-mqtt subscribe | ||
BLOCKS_BROKER=mqtt://localhost:1883 | ||
BLOCKS_TOPIC=wadra/dashboard/temperature/wohnzimmer | ||
BLOCKS_JSON=true | ||
BLOCKS_TEMPLATE_FULL={{. | printf "%.1f"}}°C | ||
BLOCKS_TEMPLATE_COLOR={{if gt . 24.}}#cc0000{{end}}{{if lt . .20}}#73d216{{end}} | ||
``` | ||
|
||
Prints lines like this to stdout: | ||
|
||
``` | ||
{"full_text":"27.6°C","color":"#cc0000"} | ||
``` | ||
|
||
Which show `In:27.6°C` in a red color in the i3 bar. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env sh | ||
set -eu | ||
go mod vendor | ||
go generate ./... | ||
exec go run github.com/rebuy-de/rebuy-go-sdk/v5/cmd/buildutil "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
func AutoEnv(cmd *cobra.Command, args []string) { | ||
cmd.PersistentFlags().VisitAll(func(flag *pflag.Flag) { | ||
if !flag.Changed { | ||
var ( | ||
envName = "BLOCKS_" + ConvertToValidEnvVarName(flag.Name) | ||
envValue = os.Getenv(envName) | ||
) | ||
|
||
if envValue == "" { | ||
return | ||
} | ||
|
||
flag.Value.Set(envValue) | ||
} | ||
}) | ||
} | ||
|
||
func ConvertToValidEnvVarName(s string) string { | ||
// Remove non-alphanumeric characters and replace them with underscores | ||
re := regexp.MustCompile(`[^a-zA-Z0-9]+`) | ||
s = re.ReplaceAllString(s, "_") | ||
|
||
// Remove leading and trailing underscores | ||
s = strings.Trim(s, "_") | ||
|
||
// Convert to uppercase | ||
s = strings.ToUpper(s) | ||
|
||
return s | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/rebuy-de/rebuy-go-sdk/v5/pkg/cmdutil" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type RunnerFunc func(ctx context.Context) error | ||
|
||
func (r RunnerFunc) Bind(cmd *cobra.Command) error { | ||
return nil | ||
} | ||
|
||
func (r RunnerFunc) Run(ctx context.Context) error { | ||
return r(ctx) | ||
} | ||
|
||
func NewRootCommand() *cobra.Command { | ||
return cmdutil.New( | ||
"i3blocks-mqtt", "Tools for adding MQTT support to i3blocks", | ||
cmdutil.WithLogVerboseFlag(), | ||
cmdutil.WithVersionCommand(), | ||
|
||
cmdutil.WithSubCommand(cmdutil.New( | ||
"subscribe", "Subscribes to a single topic and output the data to stdout", | ||
cmdutil.WithRunner(new(SubscribeRunner)), | ||
)), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"text/template" | ||
|
||
mqtt "github.com/eclipse/paho.mqtt.golang" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type SubscribeRunner struct { | ||
broker string | ||
topic string | ||
|
||
json bool | ||
templateFull string | ||
templateShort string | ||
templateColor string | ||
} | ||
|
||
func (r *SubscribeRunner) Bind(cmd *cobra.Command) error { | ||
cmd.PersistentFlags().StringVar( | ||
&r.broker, "broker", "", | ||
`Specify the MQTT broker URI (e.g., tcp://10.10.1.1:1883)`) | ||
cmd.PersistentFlags().StringVar( | ||
&r.topic, "topic", "", | ||
`Specify the topic to subscribe to`) | ||
|
||
cmd.PersistentFlags().BoolVar( | ||
&r.json, "json", false, | ||
`Parse the message payload as JSON before passing to the template`) | ||
|
||
cmd.PersistentFlags().StringVar( | ||
&r.templateFull, "template-full", "{{.}}", | ||
`Specify the Go template to format the full_text`) | ||
cmd.PersistentFlags().StringVar( | ||
&r.templateShort, "template-short", "", | ||
`Specify the Go template to format the short_text`) | ||
cmd.PersistentFlags().StringVar( | ||
&r.templateColor, "template-color", "", | ||
`Specify the Go template to format the color`) | ||
|
||
cmd.PreRun = AutoEnv | ||
|
||
return nil | ||
} | ||
|
||
func (r *SubscribeRunner) Run(ctx context.Context) error { | ||
templateFull, err := template.New("full_text").Parse(r.templateFull) | ||
if err != nil { | ||
return fmt.Errorf("parse message_full: %w", err) | ||
} | ||
|
||
templateShort, err := template.New("short_text").Parse(r.templateShort) | ||
if err != nil { | ||
return fmt.Errorf("parse message_full: %w", err) | ||
} | ||
|
||
templateColor, err := template.New("message_full").Parse(r.templateColor) | ||
if err != nil { | ||
return fmt.Errorf("parse message_full: %w", err) | ||
} | ||
|
||
handler := SubscribeHandler{ | ||
TemplateFull: templateFull, | ||
TemplateShort: templateShort, | ||
TemplateColor: templateColor, | ||
ParseJSON: r.json, | ||
} | ||
|
||
opts := mqtt.NewClientOptions() | ||
opts.AddBroker(r.broker) | ||
opts.SetAutoReconnect(true) | ||
|
||
client := mqtt.NewClient(opts) | ||
token := client.Connect() | ||
token.Wait() | ||
if token.Error() != nil { | ||
return token.Error() | ||
} | ||
|
||
token = client.Subscribe(r.topic, 1, handler.Handle) | ||
token.Wait() | ||
if token.Error() != nil { | ||
return token.Error() | ||
} | ||
|
||
<-ctx.Done() | ||
return nil | ||
} | ||
|
||
type SubscribeHandler struct { | ||
TemplateFull *template.Template | ||
TemplateShort *template.Template | ||
TemplateColor *template.Template | ||
ParseJSON bool | ||
} | ||
|
||
func (h *SubscribeHandler) Handle(client mqtt.Client, message mqtt.Message) { | ||
err := h.handle(message.Payload()) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
} | ||
|
||
type Line struct { | ||
FullText string `json:"full_text"` | ||
ShortText string `json:"short_text,omitempty"` | ||
Color string `json:"color,omitempty"` | ||
} | ||
|
||
func (h *SubscribeHandler) handle(payload []byte) error { | ||
var data any | ||
if h.ParseJSON { | ||
err := json.Unmarshal(payload, &data) | ||
if err != nil { | ||
return fmt.Errorf("parse json: %w", err) | ||
} | ||
} else { | ||
data = string(payload) | ||
} | ||
|
||
var ( | ||
err error | ||
result Line | ||
) | ||
|
||
result.FullText, err = h.executeTemplate(h.TemplateFull, data) | ||
if err != nil { | ||
return fmt.Errorf("execute full_text: %w", err) | ||
} | ||
|
||
result.ShortText, err = h.executeTemplate(h.TemplateShort, data) | ||
if err != nil { | ||
return fmt.Errorf("execute short_text: %w", err) | ||
} | ||
|
||
result.Color, err = h.executeTemplate(h.TemplateColor, data) | ||
if err != nil { | ||
return fmt.Errorf("execute color: %w", err) | ||
} | ||
|
||
out, err := json.Marshal(result) | ||
if err != nil { | ||
return fmt.Errorf("encode JSON: %w", err) | ||
} | ||
|
||
fmt.Println(string(out)) | ||
|
||
return nil | ||
} | ||
|
||
func (h *SubscribeHandler) executeTemplate(t *template.Template, data any) (string, error) { | ||
var buf bytes.Buffer | ||
err := t.Execute(&buf, data) | ||
if err != nil { | ||
return "", err | ||
} | ||
return buf.String(), nil | ||
} |
Oops, something went wrong.