-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.go
140 lines (109 loc) · 4.05 KB
/
app.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
package main
import (
"context"
"fmt"
"github.com/mawilms/lembas/internal/entities"
"github.com/mawilms/lembas/internal/models"
"github.com/mawilms/lembas/internal/processes"
"github.com/mawilms/lembas/internal/settings"
"log/slog"
"os"
)
type App struct {
ctx context.Context
logger *slog.Logger
settings settings.Settings
datastore models.DatastoreInterface
process processes.Process
localPlugins []entities.LocalPluginEntity
remotePlugins []entities.RemotePluginEntity
}
func NewApp() *App {
loggerHandler := slog.NewTextHandler(os.Stdout, nil)
logger := slog.New(loggerHandler)
s, err := settings.New()
if err != nil {
logger.Error("Error while initializing settings", slog.String("err", err.Error()))
os.Exit(1)
}
datastore, err := models.NewDatastore(s.DataDirectory)
if err != nil {
logger.Error("Error while initializing datastore", slog.String("err", err.Error()))
os.Exit(1)
}
process := processes.Process{Logger: logger}
return &App{logger: logger, settings: s, datastore: datastore, process: process}
}
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
func (a *App) SearchLocal(input string) []entities.LocalPluginEntity {
plugins := a.process.SearchLocal(input, a.localPlugins)
return plugins
}
func (a *App) SearchRemote(input string) []entities.RemotePluginEntity {
plugins := a.process.SearchRemote(input, a.remotePlugins)
return plugins
}
func (a *App) SaveSettings(input map[string]string) {
newSettings := settings.Settings{
PluginDirectory: input["pluginPath"],
DataDirectory: input["dataDirectory"],
InfoUrl: input["infoUrl"],
}
err := newSettings.Store()
if err != nil {
a.settings = newSettings
}
}
func (a *App) GetSettings() settings.Settings {
return a.settings
}
func (a *App) InstallPlugin(url string) []entities.RemotePluginEntity {
a.logger.Info("trying to install plugin", slog.String("url", url))
plugins, err := a.process.InstallPlugin(a.datastore, url, a.settings.PluginDirectory, a.remotePlugins)
if err != nil {
a.logger.Error("failed to install plugin", slog.String("url", url), slog.String("error", err.Error()))
return make([]entities.RemotePluginEntity, 0)
}
a.logger.Info("successfully installed plugin", slog.String("url", url))
a.remotePlugins = plugins
return plugins
}
func (a *App) DeletePlugin(name, author string) []entities.LocalPluginEntity {
a.logger.Info("trying to delete plugin", slog.String("name", name), slog.String("author", author))
plugins, err := a.process.DeletePlugin(a.datastore, name, author, a.settings.PluginDirectory)
if err != nil {
a.logger.Error("failed to delete plugin", slog.String("name", name), slog.String("author", author), slog.String("error", err.Error()))
return make([]entities.LocalPluginEntity, 0)
}
a.logger.Info("successfully deleted plugin", slog.String("name", name), slog.String("author", author))
a.localPlugins = plugins
return plugins
}
func (a *App) UpdatePlugins(plugins any) {
fmt.Println(plugins)
// TODO: Run internal.DownloadPlugin(url, a.settings.PluginDirectory) in a loop
}
func (a *App) GetInstalledPlugins() []entities.LocalPluginEntity {
a.logger.Info("loading installed plugins")
plugins, err := a.process.GetInstalledPlugins(a.datastore)
if err != nil {
a.logger.Error("failed to get installed plugins", slog.String("error", err.Error()))
return make([]entities.LocalPluginEntity, 0)
}
a.localPlugins = plugins
a.logger.Info("local plugins successfully loaded", slog.Int("amount plugins", len(a.localPlugins)))
return plugins
}
func (a *App) GetRemotePlugins() []entities.RemotePluginEntity {
a.logger.Info("loading remote plugins")
plugins, err := a.process.GetRemotePlugins(a.settings.InfoUrl, a.localPlugins)
if err != nil {
a.logger.Error("failed to get fetch remote plugins", slog.String("feed url", a.settings.InfoUrl), slog.String("error", err.Error()))
return make([]entities.RemotePluginEntity, 0)
}
a.remotePlugins = plugins
a.logger.Info("remote plugins successfully loaded", slog.Int("amount plugins", len(a.remotePlugins)))
return plugins
}