Skip to content

Commit 7cc8951

Browse files
authored
Merge pull request #9 from typomedia/send_email
Send email
2 parents 4163448 + fb406e2 commit 7cc8951

File tree

30 files changed

+995
-83
lines changed

30 files changed

+995
-83
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.idea/
22
.vscode/
33
dist/
4-
*.boltdb
4+
*.boltdb
5+
tmp/

Makefile

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
build:
2-
go mod tidy
1+
build: tidy
32
go build -ldflags "-s -w" -o dist/ .
43

5-
run:
4+
tidy:
65
go mod tidy
7-
go run main.go
86

9-
compile:
10-
go mod tidy
7+
run: tidy
8+
go install github.com/air-verse/air@latest
9+
air
10+
11+
compile: tidy
1112
GOOS=linux GOARCH=amd64 go build -ldflags "-s -w" -o dist/patchouli-linux-amd64 .
1213
GOOS=darwin GOARCH=amd64 go build -ldflags "-s -w" -o dist/patchouli-macos-amd64 .
1314
GOOS=windows GOARCH=amd64 go build -ldflags "-s -w" -o dist/patchouli-windows-amd64.exe .

app/app.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package app
2+
3+
var app *Application
4+
5+
type Application struct {
6+
Name string
7+
Version string
8+
Author string
9+
Description string
10+
MailTemplate string
11+
}
12+
13+
func new() *Application {
14+
app = &Application{}
15+
app.Name = "Patchouli"
16+
app.Version = "0.2.0"
17+
app.Author = "Philipp Speck <[email protected]>"
18+
app.Description = "Patch Management Planner"
19+
20+
return app
21+
}
22+
func GetApp() *Application {
23+
if app == nil {
24+
app = new()
25+
}
26+
return app
27+
}

app/handler/config/edit.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/gofiber/fiber/v2"
55
"github.com/typomedia/patchouli/app/store/boltdb"
66
"github.com/typomedia/patchouli/app/structs"
7+
"golang.org/x/crypto/bcrypt"
78
)
89

910
func Edit(c *fiber.Ctx) error {
@@ -16,7 +17,13 @@ func Edit(c *fiber.Ctx) error {
1617

1718
var config structs.Config
1819
db.Get("main", &config, "config")
19-
20+
if config.Smtp.Password != "" {
21+
passwordHash, err := bcrypt.GenerateFromPassword([]byte(config.Smtp.Password), bcrypt.DefaultCost)
22+
if err != nil {
23+
return err
24+
}
25+
config.Smtp.Password = string(passwordHash)
26+
}
2027
defer db.Close()
2128
return c.Render("app/views/config/edit", fiber.Map{
2229
"Config": config,
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package filter
2+
3+
import (
4+
"encoding/json"
5+
"sort"
6+
"time"
7+
8+
"github.com/gofiber/fiber/v2"
9+
"github.com/gofiber/fiber/v2/log"
10+
"github.com/typomedia/patchouli/app/helper"
11+
"github.com/typomedia/patchouli/app/store/boltdb"
12+
"github.com/typomedia/patchouli/app/structs"
13+
)
14+
15+
func Operator(c *fiber.Ctx) error {
16+
id := c.Params("id")
17+
db := boltdb.New()
18+
19+
err := db.SetBucket("machine")
20+
if err != nil {
21+
return err
22+
}
23+
24+
machines, _ := db.GetAllByOperatorId(id, "machine")
25+
26+
Machines := structs.Machines{}
27+
for _, v := range machines {
28+
machine := structs.Machine{}
29+
err = json.Unmarshal(v, &machine)
30+
if err != nil {
31+
return err
32+
}
33+
34+
lastUpdate, _ := db.GetLastByName(machine.Id, "history")
35+
36+
update := structs.Update{}
37+
err = json.Unmarshal(lastUpdate, &update)
38+
if err != nil {
39+
log.Error(err)
40+
}
41+
42+
machine.Update = update
43+
44+
Machines = append(Machines, machine)
45+
46+
}
47+
48+
// sort machines by oldest update first
49+
sort.Sort(structs.ByDate(Machines))
50+
51+
err = db.SetBucket("config")
52+
if err != nil {
53+
return err
54+
}
55+
56+
var config structs.Config
57+
db.Get("main", &config, "config")
58+
59+
defer db.Close()
60+
61+
interval := config.General.Interval
62+
for i := range Machines {
63+
currentDate := time.Now()
64+
65+
if Machines[i].Update.Date == "" {
66+
Machines[i].Update.Date = "0000-00-00"
67+
Machines[i].Status = "danger"
68+
continue
69+
}
70+
71+
Machines[i].Update.Date = helper.UnixToDateString(Machines[i].Update.Date)
72+
73+
date, err := time.Parse("2006-01-02", Machines[i].Update.Date)
74+
if err != nil {
75+
log.Error(err)
76+
}
77+
78+
Machines[i].Days = int(currentDate.Sub(date).Hours() / 24)
79+
if Machines[i].Days > interval {
80+
Machines[i].Status = "danger"
81+
} else if Machines[i].Days > interval/3 {
82+
Machines[i].Status = "warning"
83+
} else {
84+
Machines[i].Status = "success"
85+
}
86+
Machines[i].Days = interval - int(currentDate.Sub(date).Hours()/24)
87+
88+
}
89+
return c.Render("app/views/dashboard/list", fiber.Map{
90+
"Machines": Machines,
91+
})
92+
}

app/handler/dashboard/list.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package dashboard
22

33
import (
44
"encoding/json"
5+
"sort"
6+
"time"
7+
58
"github.com/gofiber/fiber/v2"
69
"github.com/gofiber/fiber/v2/log"
710
"github.com/typomedia/patchouli/app/helper"
811
"github.com/typomedia/patchouli/app/store/boltdb"
912
"github.com/typomedia/patchouli/app/structs"
10-
"sort"
11-
"time"
1213
)
1314

1415
func List(c *fiber.Ctx) error {
@@ -31,15 +32,18 @@ func List(c *fiber.Ctx) error {
3132

3233
lastUpdate, _ := db.GetLastByName(machine.Id, "history")
3334

34-
update := structs.Update{}
35-
err = json.Unmarshal(lastUpdate, &update)
36-
if err != nil {
37-
log.Error(err)
35+
if lastUpdate != nil {
36+
update := structs.Update{}
37+
err = json.Unmarshal(lastUpdate, &update)
38+
if err != nil {
39+
log.Error(err)
40+
}
41+
machine.Update = update
3842
}
3943

40-
machine.Update = update
41-
42-
Machines = append(Machines, machine)
44+
if !machine.Inactive {
45+
Machines = append(Machines, machine)
46+
}
4347

4448
}
4549

@@ -56,7 +60,7 @@ func List(c *fiber.Ctx) error {
5660

5761
defer db.Close()
5862

59-
interval := config.Interval
63+
interval := config.General.Interval
6064
for i := range Machines {
6165
currentDate := time.Now()
6266

app/handler/machine/activate.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package machine
2+
3+
import (
4+
"github.com/gofiber/fiber/v2"
5+
"github.com/typomedia/patchouli/app/store/boltdb"
6+
"github.com/typomedia/patchouli/app/structs"
7+
)
8+
9+
func Activate(c *fiber.Ctx) error {
10+
machineId := c.Params("id")
11+
12+
db := boltdb.New()
13+
14+
err := db.SetBucket("machine")
15+
if err != nil {
16+
return err
17+
}
18+
defer db.Close()
19+
20+
var machine structs.Machine
21+
err = db.Get(machineId, &machine, "machine")
22+
if err != nil {
23+
return err
24+
}
25+
26+
machine.Inactive = false
27+
28+
err = db.Set(machineId, machine, "machine")
29+
30+
if err != nil {
31+
return err
32+
}
33+
34+
return c.Redirect("/machine")
35+
}

app/handler/machine/deactivate.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package machine
2+
3+
import (
4+
"github.com/gofiber/fiber/v2"
5+
"github.com/typomedia/patchouli/app/store/boltdb"
6+
"github.com/typomedia/patchouli/app/structs"
7+
)
8+
9+
func Deactivate(c *fiber.Ctx) error {
10+
machineId := c.Params("id")
11+
12+
db := boltdb.New()
13+
14+
err := db.SetBucket("machine")
15+
if err != nil {
16+
return err
17+
}
18+
defer db.Close()
19+
20+
var machine structs.Machine
21+
err = db.Get(machineId, &machine, "machine")
22+
if err != nil {
23+
return err
24+
}
25+
26+
machine.Inactive = true
27+
err = db.Set(machineId, machine, "machine")
28+
29+
if err != nil {
30+
return err
31+
}
32+
33+
return c.Redirect("/machine")
34+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package filter
2+
3+
import (
4+
"encoding/json"
5+
6+
"github.com/gofiber/fiber/v2"
7+
"github.com/typomedia/patchouli/app/store/boltdb"
8+
"github.com/typomedia/patchouli/app/structs"
9+
)
10+
11+
func Operator(c *fiber.Ctx) error {
12+
id := c.Params("id")
13+
db := boltdb.New()
14+
15+
// set bucket
16+
err := db.SetBucket("machine")
17+
if err != nil {
18+
return err
19+
}
20+
21+
machines, _ := db.GetAllByOperatorId(id, "machine")
22+
23+
Machines := structs.Machines{}
24+
25+
for _, v := range machines {
26+
machine := structs.Machine{}
27+
err = json.Unmarshal(v, &machine)
28+
if err != nil {
29+
return err
30+
}
31+
Machines = append(Machines, machine)
32+
33+
}
34+
35+
defer db.Close()
36+
37+
return c.Render("app/views/machine/list", fiber.Map{
38+
"Machines": Machines,
39+
})
40+
}

app/handler/machine/list.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package machine
22

33
import (
44
"encoding/json"
5+
56
"github.com/gofiber/fiber/v2"
67
"github.com/typomedia/patchouli/app/store/boltdb"
78
"github.com/typomedia/patchouli/app/structs"
@@ -19,17 +20,24 @@ func List(c *fiber.Ctx) error {
1920
machines, _ := db.GetAll("machine")
2021

2122
Machines := structs.Machines{}
23+
inactiveMachines := structs.Machines{}
2224

2325
for _, v := range machines {
2426
machine := structs.Machine{}
2527
err = json.Unmarshal(v, &machine)
2628
if err != nil {
2729
return err
2830
}
29-
Machines = append(Machines, machine)
31+
if machine.Inactive {
32+
inactiveMachines = append(inactiveMachines, machine)
33+
} else {
34+
Machines = append(Machines, machine)
3035

36+
}
3137
}
3238

39+
Machines = append(Machines, inactiveMachines...)
40+
3341
defer db.Close()
3442

3543
return c.Render("app/views/machine/list", fiber.Map{

0 commit comments

Comments
 (0)