-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaarsstjerner.go
181 lines (139 loc) · 3.64 KB
/
aarsstjerner.go
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
package main
import (
"fmt"
"os"
"slices"
"sort"
"time"
"bitbucket.org/long174/go-odoo"
"github.com/charmbracelet/glamour"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
"github.com/pkg/browser"
"github.com/spejder/aarsstjerner/internal/ms"
"github.com/urfave/cli/v2"
)
type otherInfo struct {
Aarstjerne int `yaml:"årsstjerne"`
}
func getHTML(ctx *cli.Context) (string, error) {
listInMarkdown, err := getMarkdown(ctx)
if err != nil {
return "", err
}
// create markdown parser with extensions
p := parser.New()
doc := p.Parse([]byte(listInMarkdown))
// create HTML renderer with extensions
htmlFlags := html.CompletePage | html.Smartypants
opts := html.RendererOptions{Flags: htmlFlags}
renderer := html.NewRenderer(opts)
output := markdown.Render(doc, renderer)
return string(output), nil
}
func runBrowser(ctx *cli.Context) error {
output, err := getHTML(ctx)
if err != nil {
return fmt.Errorf("building html: %w", err)
}
tmpFile, err := os.CreateTemp("", "aarsstjerner.*.html")
if err != nil {
return fmt.Errorf("could not create temporary file: %w", err)
}
defer tmpFile.Close()
defer os.Remove(tmpFile.Name())
_, err = tmpFile.WriteString(output)
if err != nil {
return fmt.Errorf("writing temporary file: %w", err)
}
err = tmpFile.Close()
if err != nil {
return fmt.Errorf("closing temporary file: %w", err)
}
err = browser.OpenFile(tmpFile.Name())
if err != nil {
return fmt.Errorf("opening browser: %w", err)
}
time.Sleep(1 * time.Second)
return nil
}
func list(ctx *cli.Context) error {
md, err := getMarkdown(ctx)
if err != nil {
return err
}
fmt.Fprintln(os.Stdout, md)
return nil
}
func term(ctx *cli.Context) error {
md, err := getMarkdown(ctx)
if err != nil {
return err
}
result, err := glamour.Render(md, "auto")
if err != nil {
return fmt.Errorf("rendering output as markdown: %w", err)
}
fmt.Fprintln(os.Stdout, result)
return nil
}
//nolint:funlen,cyclop
func getMarkdown(ctx *cli.Context) (string, error) {
username, password, err := credentials(ctx)
if err != nil {
return "", fmt.Errorf("getting credentials: %w", err)
}
config := &odoo.ClientConfig{
Admin: username,
Password: password,
Database: ctx.String("ms-database"),
URL: ctx.String("ms-url"),
}
oc, err := odoo.NewClient(config)
if err != nil {
return "", fmt.Errorf("creating Odoo client: %w", err)
}
client := &ms.Client{Client: *oc}
fmt.Fprintln(os.Stderr, "Henter medlemmer...")
profiles, err := profiles(client)
if err != nil {
return "", err
}
fmt.Fprintln(os.Stderr, "Henter medlemskaber...")
res := make(map[int][]string)
ids := make([]int64, 0, len(*profiles))
for _, profile := range *profiles {
ids = append(ids, profile.MembershipIds.Get()...)
}
memberships, err := memberships(client, ids)
if err != nil {
return "", err
}
membershipsMap := make(map[int64]ms.MemberMembership, len(*memberships))
for _, membership := range *memberships {
membershipsMap[membership.Id.Get()] = membership
}
for _, profile := range *profiles {
years, name := calculate(ctx, profile, membershipsMap)
if years == 0 {
continue
}
res[years] = append(res[years], name)
}
keys := make([]int, 0, len(res))
for k := range res {
keys = append(keys, k)
}
sort.Ints(keys)
slices.Reverse(keys)
output := ""
for _, years := range keys {
output += fmt.Sprintf("\n# %d-årsstjerner (%d stk)\n", years, len(res[years]))
for _, name := range res[years] {
output += fmt.Sprintf("- %s\n", name)
}
}
output += fmt.Sprintf("\n---\n_%s_\n", time.Now().Format("2006-01-02 15:04:05"))
return output, nil
}