This repository was archived by the owner on Oct 12, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
187 lines (165 loc) · 4.46 KB
/
Copy pathmain.go
File metadata and controls
187 lines (165 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright 2026 Georg Hagn (tiny-frameworks)
// SPDX-License-Identifier: Apache-2.0
/*
A simple `main.go` snippet serves as the entry point. Default values are used here:
- The `env` structure is located in the working directory alongside `main.go`.
- The `default` seller is used (found in `env/sellers/default`).
- The `native` provider is used (specified in `env/system/systm.yaml` via `zugferd_provider=native` and `pdf_provider=native`).
You can find a detailed code example showing how to integrate nexfact as a library in `orchestrator.orchestrator_test`.
*/
package main
import (
stdErrors "errors"
"os"
"os/exec"
"runtime"
"codeberg.org/tiny-frameworks/nexfact/orchestrator"
"codeberg.org/tiny-frameworks/nexfact/orchestrator/config"
"codeberg.org/tiny-frameworks/nexutils/errors"
"codeberg.org/tiny-frameworks/nexutils/logger"
)
func main() {
// Executes the logic and ensures that `defer` calls are guaranteed to run!
os.Exit(run())
}
func run() int {
defer config.StopCaches()
// Required only for the local monorepo example:
os.Setenv(orchestrator.EnvRootKey, "../env")
// In production, the orchestrator searches env-directory with:
// 1: orchestrator.NewDefault():
// 1.1: environment-variable: NEXFACT_ENVROOT, if empty:
// 1.2: default directory position (env-dir in working-directory)
// 2: orchestrator.New(env-path)
orch, err := orchestrator.NewDefault()
if err := setupLogger(); err != nil {
return 1
}
if err != nil {
errors.LogError(logger.Logger, err)
os.Exit(errors.GetExitCode(err))
}
// 2. Execute job (expects JSON payload for the toolchain)
jsonPayload := jsonJob()
result, err := orch.RunJsonJob(jsonPayload)
if err != nil {
errors.LogError(logger.Logger, err)
os.Exit(errors.GetExitCode(err))
}
logger.Logger.Info("Job succeeded", "JobID:", result.JobID, "OutputPath", result.OutputPath)
if err := openPDF(result.OutputPath); err != nil {
errors.LogError(logger.Logger, err)
os.Exit(errors.GetExitCode(err))
}
return 0 // SUCCESS Exit-Code
}
func setupLogger() error {
cfg := &logger.LoggerConfig{
Filename: "example_main.log",
UseLocking: false,
Level: logger.LevelInfo,
}
return logger.SetupLogging(cfg)
}
func openPDF(filePath string) error {
var cmd *exec.Cmd
switch runtime.GOOS {
case "windows":
cmd = exec.Command("cmd", "/c", "start", filePath)
case "darwin":
cmd = exec.Command("open", filePath)
case "linux":
cmd = exec.Command("xdg-open", filePath)
default:
return stdErrors.New("unsupported operating system: " + runtime.GOOS)
}
return cmd.Start()
}
func jsonJob() []byte {
return []byte(`{
"invoice": {
"currency": "EUR",
"invoice_id": "RE-0814-min",
"issue_date": "20260105",
"service_date": "20260104",
"buyer": {
"id": "471115",
"name1": "Franz Fröhlich AG",
"name2": "Einkauf Herr Ansprech",
"name3": "Carnica",
"street": "Musterstrasse 64",
"zip": "01221",
"city": "Musterdorf",
"country": "Deutschland"
},
"info": {
"info_date": "20260102"
},
"items": [
{
"pos": 1,
"id": "A1",
"description": "Go Entwicklungsservice",
"quantity": 10.5,
"unit": "Stunde",
"net_price": 95.00,
"line_total": 997.5,
"tax_rate": 19
},
{
"pos": 2,
"id": "B5",
"description": "Dokumentations-Pauschale",
"quantity": 1,
"unit": "Pauschale",
"net_price": 45.50,
"line_total": 45.50,
"tax_rate": 19
},
{
"pos": 3,
"id": "4711",
"description": "Go-Grundkurs für Fortgeschrittene",
"quantity": 1,
"unit": "Stück",
"net_price": 150.00,
"line_total": 150.00,
"tax_rate": 19
},
{
"pos": 4,
"id": "0815",
"description": "LibreOffice Basic Beruhigungstee",
"quantity": 5,
"unit": "Liter",
"net_price": 3.50,
"line_total": 17.50,
"tax_rate": 7
}
],
"vats": [
{
"pos": 1,
"id": "S",
"description": "USt",
"vat_base": 1193.00,
"vat_price": 226.67,
"tax_rate": 19
},
{
"pos": 2,
"id": "S",
"description": "USt",
"vat_base": 17.50,
"vat_price": 1.23,
"tax_rate": 7
}
],
"totals": {
"line_total_amount": 1210.50,
"tax_total_amount": 227.91,
"grand_total_amount": 1438.41
}
}
}`)
}