From 958979c62b92715a4b665e21b433ae23f8e7b53f Mon Sep 17 00:00:00 2001 From: schreifuchs Date: Sun, 24 Aug 2025 14:23:51 +0200 Subject: [PATCH] feat: config --- .gitignore | 1 + config.example.json | 60 ++++++++++++++++++++++++++++ config.go | 36 +++++++++++++++++ go.mod | 3 +- go.sum | 4 -- index.html | 20 +++++----- mailer/send.go | 4 +- main.go | 96 +++++++++++++-------------------------------- types.go | 21 ++++++++++ 9 files changed, 159 insertions(+), 86 deletions(-) create mode 100644 .gitignore create mode 100644 config.example.json create mode 100644 config.go create mode 100644 types.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d344ba6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config.json diff --git a/config.example.json b/config.example.json new file mode 100644 index 0000000..42d411d --- /dev/null +++ b/config.example.json @@ -0,0 +1,60 @@ +{ + "gitea_url": "https://git.schreifuchs.ch", + "gitea_token": "", + "repos": [ + { + "owner": "lou-taylor", + "repo": "lou-taylor-web" + }, + { + "owner": "lou-taylor", + "repo": "lou-taylor-api" + }, + { + "owner": "lou-taylor", + "repo": "accounting" + } + ], + "min_duration": "15m", + "hourly": 16, + "from_entity": { + "name": "schreifuchs.ch", + "iban": "", + "address": { + "street": "", + "number": "", + "zip_code": "", + "place": "", + "country": "" + }, + "contact": "" + }, + "to_entity": { + "name": "", + "address": { + "street": "", + "number": "", + "zip_code": "", + "place": "", + "country": "" + }, + "contact": "Loana Groux" + }, + "pdf_generator_url": "http://localhost:3030", + "mailer": { + "smtp": { + "host": "mail.your-server.de", + "port": "465", + "user": "", + "password": "" + }, + "from": "" + }, + "mail": { + "to": "", + "subject": "", + "body": "" + }, + "mail_bcc": [""] +} + diff --git a/config.go b/config.go new file mode 100644 index 0000000..dd0dabc --- /dev/null +++ b/config.go @@ -0,0 +1,36 @@ +package main + +import ( + "encoding/json" + "os" + + "git.schreifuchs.ch/lou-taylor/accounting/mailer" + "git.schreifuchs.ch/lou-taylor/accounting/model" +) + +type Config struct { + GiteaURL string `json:"gitea_url"` + GiteaToken string `json:"gitea_token"` + Repos []Repo `json:"repos"` + MinDuration Duration `json:"min_duration"` + Hourly float64 `json:"hourly"` + FromEntity model.Entity `json:"from_entity"` + ToEntity model.Entity `json:"to_entity"` + PdfGeneratorURL string `json:"pdf_generator_url"` + Mailer mailer.Config `json:"mailer"` + Mail mailer.Mail `json:"mail"` + MailBcc []string `json:"mail_bcc"` +} + +func LoadConfig(path string) (Config, error) { + var cfg Config + file, err := os.Open(path) + if err != nil { + return cfg, err + } + defer file.Close() + decoder := json.NewDecoder(file) + err = decoder.Decode(&cfg) + return cfg, err +} + diff --git a/go.mod b/go.mod index ed621c4..5755f58 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( code.gitea.io/sdk/gitea v0.21.0 github.com/gomarkdown/markdown v0.0.0-20250810172220-2e2c11897d1a github.com/jedib0t/go-pretty/v6 v6.6.8 + github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e github.com/starwalkn/gotenberg-go-client/v8 v8.11.0 ) @@ -15,11 +16,9 @@ require ( github.com/davidmz/go-pageant v1.0.2 // indirect github.com/go-fed/httpsig v1.1.0 // indirect github.com/hashicorp/go-version v1.7.0 // indirect - github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible // indirect github.com/mattn/go-runewidth v0.0.16 // indirect github.com/rivo/uniseg v0.4.7 // indirect golang.org/x/crypto v0.41.0 // indirect golang.org/x/sys v0.35.0 // indirect golang.org/x/text v0.28.0 // indirect - gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect ) diff --git a/go.sum b/go.sum index fb3b102..d254de0 100644 --- a/go.sum +++ b/go.sum @@ -49,9 +49,5 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.28.0 h1:rhazDwis8INMIwQ4tpjLDzUhx6RlXqZNPEM0huQojng= golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk= -gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk= -gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE= -gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/index.html b/index.html index a492e7c..3d3a366 100644 --- a/index.html +++ b/index.html @@ -216,7 +216,7 @@ article {

schreifuchs.ch

Kilchbergerweg 1
- 3052 Zollikofen
+ Zollikofen
Niklas Breitenstein

@@ -224,7 +224,7 @@ article {

- Rechnung: 20 25082 41057 10271 08692 82635
+ Rechnung: 20 25082 41117 00284 67114 43342
Datum: 24.08.2025

@@ -234,7 +234,7 @@ article {

Lou Taylor
Alpenstrasse 22
- 4950 Huttwil
+ Huttwil
Loana Groux

@@ -387,19 +387,19 @@ article { CH06 0079 0042 5877 0443 7
schreifuchs.ch
Kilchbergerweg 1
- 3052 Zollikofen + Zollikofen

Referenz

-

20 25082 41057 10271 08692 82635

+

20 25082 41117 00284 67114 43342

Zahlbar durch

Lou Taylor
Alpenstrasse 22
- 4950 Huttwil + Huttwil

@@ -481,7 +481,7 @@ article {
- + @@ -587,19 +587,19 @@ article { CH06 0079 0042 5877 0443 7
schreifuchs.ch
Kilchbergerweg 1
- 3052 Zollikofen + Zollikofen

Referenz

-

20 25082 41057 10271 08692 82635

+

20 25082 41117 00284 67114 43342

Zahlbar durch

Lou Taylor
Alpenstrasse 22
- 4950 Huttwil + Huttwil

diff --git a/mailer/send.go b/mailer/send.go index f359b03..246e20b 100644 --- a/mailer/send.go +++ b/mailer/send.go @@ -7,10 +7,10 @@ import ( "github.com/jordan-wright/email" ) -func (s Service) Send(m Mail) (err error) { +func (s Service) Send(m Mail, bcc ...string) (err error) { e := email.NewEmail() e.To = []string{m.TO} - e.Bcc = []string{"niklas@sunway.ch"} + e.Bcc = bcc e.From = s.from e.Subject = m.Subject e.Text = []byte(m.Body) diff --git a/main.go b/main.go index faac9b3..6745cb9 100644 --- a/main.go +++ b/main.go @@ -1,40 +1,39 @@ package main import ( - "os" "time" "code.gitea.io/sdk/gitea" "git.schreifuchs.ch/lou-taylor/accounting/issue" "git.schreifuchs.ch/lou-taylor/accounting/mailer" - "git.schreifuchs.ch/lou-taylor/accounting/model" "git.schreifuchs.ch/lou-taylor/accounting/pdf" "git.schreifuchs.ch/lou-taylor/accounting/report" ) type Repo struct { - owner string - repo string + Owner string `json:"owner"` + Repo string `json:"repo"` } func main() { + cfg, err := LoadConfig("config.json") + if err != nil { + panic(err) + } + client, err := gitea.NewClient( - "https://git.schreifuchs.ch", - gitea.SetToken("6a8ea8f9de039b0950c634bfea40c6f97f94b06b"), + cfg.GiteaURL, + gitea.SetToken(cfg.GiteaToken), ) if err != nil { panic(err) } var is []*gitea.Issue - for _, repo := range []Repo{ - {"lou-taylor", "lou-taylor-web"}, - {"lou-taylor", "lou-taylor-api"}, - {"lou-taylor", "accounting"}, - } { + for _, repo := range cfg.Repos { iss, _, err := client.ListRepoIssues( - repo.owner, - repo.repo, + repo.Owner, + repo.Repo, gitea.ListIssueOption{ ListOptions: gitea.ListOptions{Page: 0, PageSize: 99999}, Since: time.Now().AddDate(0, -1, 0), @@ -55,46 +54,16 @@ func main() { return i.Closed != nil && i.Closed.After(time.Now().AddDate(0, -1, 0)) }, ) - issues := issue.FromGiteas(is, time.Minute*15) + issues := issue.FromGiteas(is, time.Duration(cfg.MinDuration)) r := report.New( issues, - model.Entity{ - Name: "schreifuchs.ch", - IBAN: "CH06 0079 0042 5877 0443 7", - Address: model.Address{ - Street: "Kilchbergerweg", - Number: "1", - ZIPCode: "3052", - Place: "Zollikofen", - Country: "Schweiz", - }, - Contact: "Niklas Breitenstein", - }, - model.Entity{ - Name: "Lou Taylor", - Address: model.Address{ - Street: "Alpenstrasse", - Number: "22", - ZIPCode: "4950", - Place: "Huttwil", - Country: "Schweiz", - }, - Contact: "Loana Groux", - }, - 16, + cfg.FromEntity, + cfg.ToEntity, + cfg.Hourly, ) html := r.ToHTML() - file, err := os.Create("index.html") - if err != nil { - panic(err) - } - defer file.Close() - - file.Write([]byte(html)) - - // fmt.Print(html) - pdfs, err := pdf.New("http://localhost:3030") + pdfs, err := pdf.New(cfg.PdfGeneratorURL) if err != nil { panic(err) } @@ -104,30 +73,21 @@ func main() { panic(err) } - mlr, err := mailer.New(mailer.Config{ - SMTP: mailer.SMTPConfig{ - Host: "mail.your-server.de", - Port: "465", - User: "test@schreifuchs.ch", - Password: "xV27D1nj33dNz8B4", - }, - From: "test@schreifuchs.ch", - }) + mlr, err := mailer.New(cfg.Mailer) if err != nil { panic(err) } - err = mlr.Send(mailer.Mail{ - TO: "kontakt@schreifuchs.ch", - Subject: "test", - Body: "Hallo", - Attachments: []mailer.Attachment{ - { - Name: "invoice.pdf", - MimeType: "pdf", - Content: document, - }, + + mail := cfg.Mail + mail.Attachments = []mailer.Attachment{ + { + Name: "invoice.pdf", + MimeType: "pdf", + Content: document, }, - }) + } + + err = mlr.Send(mail) if err != nil { panic(err) } diff --git a/types.go b/types.go new file mode 100644 index 0000000..8f351af --- /dev/null +++ b/types.go @@ -0,0 +1,21 @@ +package main + +import ( + "encoding/json" + "time" +) + +type Duration time.Duration + +func (d *Duration) UnmarshalJSON(b []byte) error { + var s string + if err := json.Unmarshal(b, &s); err != nil { + return err + } + tmp, err := time.ParseDuration(s) + if err != nil { + return err + } + *d = Duration(tmp) + return nil +} \ No newline at end of file