Skip to content

Commit

Permalink
Merge pull request #2 from mlcdf/wip
Browse files Browse the repository at this point in the history
Merge wip into main
  • Loading branch information
mlcdf authored Nov 26, 2022
2 parents 81c55d1 + ae79e9a commit aab1a89
Show file tree
Hide file tree
Showing 29 changed files with 1,095 additions and 450 deletions.
29 changes: 17 additions & 12 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,44 @@ jobs:
name: Upload Release Asset
runs-on: ubuntu-latest
steps:
- name: Install Go
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: ">=1.18.0-rc.1"
- name: Checkout repository
uses: actions/checkout@v2
go-version: "1.19"
check-latest: true
cache: true

- name: Build binaries
run: |
export VERSION=$(git describe --tags)
export CGO_ENABLED=0
export LDFLAGS="-X 'go.mlcdf.fr/sally/build.Version=${VERSION}'"
GOOS=linux GOARCH=amd64 go build -o "dyndns-${VERSION}-${GOOS}-${GOARCH}" -ldflags ${LDFLAGS}
GOOS=linux GOARCH=arm GOARM=6 go build -o "dyndns-${VERSION}-${GOOS}-${GOARCH}" -ldflags ${LDFLAGS}
GOOS=linux GOARCH=arm64 go build -o "dyndns-${VERSION}-${GOOS}-${GOARCH}" -ldflags ${LDFLAGS}
GOOS=darwin GOARCH=amd64 go build -o "dyndns-${VERSION}-${GOOS}-${GOARCH}" -ldflags ${LDFLAGS}
GOOS=windows GOARCH=amd64 go build -o "dyndns-${VERSION}-${GOOS}-${GOARCH}.exe" -ldflags ${LDFLAGS}
export GOOS=linux GOARCH=amd64 && go build -v -o "dyndns-${VERSION}-${GOOS}-${GOARCH}" -ldflags "${LDFLAGS}"
export GOOS=linux GOARCH=arm GOARM=6 && go build -o "dyndns-${VERSION}-${GOOS}-${GOARCH}" -ldflags "${LDFLAGS}"
export GOOS=linux GOARCH=arm64 && go build -o "dyndns-${VERSION}-${GOOS}-${GOARCH}" -ldflags "${LDFLAGS}"
export GOOS=darwin GOARCH=amd64 && go build -o "dyndns-${VERSION}-${GOOS}-${GOARCH}" -ldflags "${LDFLAGS}"
export GOOS=windows GOARCH=amd64 && go build -o "dyndns-${VERSION}-${GOOS}-${GOARCH}.exe" -ldflags "${LDFLAGS}"
- name: Upload release artifacts
uses: actions/github-script@v3
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require("fs").promises;
const { repo: { owner, repo }, sha } = context;
const release = await github.repos.getReleaseByTag({
const release = await github.rest.repos.getReleaseByTag({
owner, repo,
tag: process.env.GITHUB_REF.replace("refs/tags/", ""),
});
console.log("Release:", { release });
for (let file of await fs.readdir(".")) {
if (!file.startsWith("dyndns-")) continue;
console.log("Uploading", file);
await github.repos.uploadReleaseAsset({
await github.rest.repos.uploadReleaseAsset({
owner, repo,
release_id: release.data.id,
name: file,
Expand Down
52 changes: 52 additions & 0 deletions .github/workflows/test-coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: test

on:
push:
branches: [main]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: "1.19"
check-latest: true
cache: true

- name: Test
run: go test -v -race ./...

- name: Generate coverage report in HTML
run: go tool cover -html dist/coverage.out -o report.html

- name: GitHub Badge
run: |
total=$(go tool cover -func dist/coverage.out | grep total| grep -Eo '[0-9]+\.[0-9]+')
if (( $(echo "$total <= 50" | bc -l) )) ; then
COLOR=red
elif (( $(echo "$total > 80" | bc -l) )); then
COLOR=brightgreen
else
COLOR=orange
fi
mkdir -p dist
cp report.html dist/report.html
curl "https://img.shields.io/badge/coverage-$total%25-$COLOR" > dist/badge.svg
cd dist
git init
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git add .
git commit -m "Updated coverage"
- name: Push changes
uses: mlcdf/github-push-action@master
with:
github_token: ${{ secrets.github_token }}
branch: coverage
force: true
directory: dist
10 changes: 4 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
name: test

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: ">=1.18.0-rc.1"
go-version: "1.19"
check-latest: true
cache: true

- name: Test
run: go test -v -race ./...
env:
GANDI_TOKEN: ${{ secrets.GANDI_TOKEN }}
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
*.out

dist/
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# dyndns

[![test](https://github.com/mlcdf/dyndns/actions/workflows/test.yml/badge.svg)](https://github.com/mlcdf/dyndns/actions/workflows/test.yml)
[![coverage](https://raw.githubusercontent.com/mlcdf/dyndns/coverage/badge.svg)](https://raw.githubusercontent.com/mlcdf/dyndns/coverage/badge.svg)

Update Gandi LiveDNS based on the current (dynamic) ip.

## Why
Expand All @@ -21,7 +24,7 @@ This program aims to solve that. It's intended to run on a always-on computer in
- From source (make sure `$GOPATH/bin` is in your `$PATH`):

```sh
go get go.mlcdf.fr/dyndns
go install go.mlcdf.fr/dyndns
```

## Setup
Expand Down Expand Up @@ -73,3 +76,8 @@ Run the tests
```sh
go test ./...
```

Force `go test` to run all the tests (and don't kill the docker-compose containers so the following runs will be faster).
```sh
./scrits/test.sh
```
14 changes: 14 additions & 0 deletions bincover_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//go:build testbincover
// +build testbincover

package main

import (
"testing"

"github.com/confluentinc/bincover"
)

func TestBincoverRunMain(t *testing.T) {
bincover.RunTest(func() { bincover.ExitCode = int(mainRun()) })
}
35 changes: 19 additions & 16 deletions internal/discord/discord.go → discord.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package discord
package main

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"io"
"os"

"go.mlcdf.fr/sally/build"
)

type Client struct {
type discordClient struct {
WebhookURL string
}

var _ io.Writer = (*discordClient)(nil)

// Webhook is the webhook object sent to discord
type Webhook struct {
Username string `json:"username"`
Expand Down Expand Up @@ -63,55 +64,57 @@ type Image struct {
}

func NewWebhook() *Webhook {
webhook := &Webhook{Username: build.String(), Embeds: []Embed{Embed{}}}

hostname, err := os.Hostname()
if err != nil {
hostname = "(unknown)"
}
webhook.Embeds[0].Footer.Text = hostname
return webhook

return &Webhook{
Username: build.String(),
Embeds: []Embed{{Footer: Footer{Text: hostname}}},
}
}

func (c *Client) PostInfo(webhook *Webhook) error {
func (c *discordClient) PostInfo(webhook *Webhook) error {
webhook.Embeds[0].Color = 2201331
return c.Post(webhook)
}

func (c *Client) PostError(webhook *Webhook) error {
func (c *discordClient) PostError(webhook *Webhook) error {
webhook.Embeds[0].Color = 15092300
return c.Post(webhook)
}

func (c *Client) PostSuccess(webhook *Webhook) error {
func (c *discordClient) PostSuccess(webhook *Webhook) error {
webhook.Embeds[0].Color = 5747840
return c.Post(webhook)
}

func (c *Client) Post(webhook *Webhook) error {
func (c *discordClient) Post(webhook *Webhook) error {
payload, err := json.Marshal(webhook)
if err != nil {
return fmt.Errorf("failed to marshal webhook payload: %s", err)
}

res, err := http.Post(c.WebhookURL, "application/json", bytes.NewReader(payload))
res, err := defaultHTTP.Post(c.WebhookURL, "application/json", bytes.NewReader(payload))
if err != nil {
return fmt.Errorf("failed to post to webhook: %s", err)
}
defer res.Body.Close()

body, err := ioutil.ReadAll(res.Body)
body, err := io.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("failed read body response : %s", err)
}

if res.StatusCode >= 400 || err != nil {
if res.StatusCode >= 400 {
return fmt.Errorf("failed to post to webhook: reason=%s status=%s", body, res.Status)
}

return nil
}

func (c *Client) Write(p []byte) (n int, err error) {
func (c *discordClient) Write(p []byte) (n int, err error) {
w := NewWebhook()
w.Embeds[0] = Embed{
Description: string(p),
Expand Down
16 changes: 16 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: "3"
services:
smocker:
image: thiht/smocker
ports:
- 8080:8080
- 8081:8081

db:
image: postgres
environment:
POSTGRES_DB: database
POSTGRES_USER: user
POSTGRES_PASSWORD: password
ports:
- 8082:5432
Loading

0 comments on commit aab1a89

Please sign in to comment.