-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathpkg.go
More file actions
95 lines (83 loc) · 2.68 KB
/
Copy pathpkg.go
File metadata and controls
95 lines (83 loc) · 2.68 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
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"slices"
"github.com/PlakarKorp/kloset/connectors/exporter"
"github.com/PlakarKorp/kloset/connectors/importer"
"github.com/PlakarKorp/kloset/connectors/storage"
"github.com/PlakarKorp/pkg"
"github.com/PlakarKorp/plakar/appcontext"
"github.com/PlakarKorp/plakar/plugins"
"github.com/PlakarKorp/plakar/utils"
)
func setupPkgManager(ctx *appcontext.AppContext, dataDir, cacheDir string) error {
plugdir := filepath.Join(dataDir, "plugins", pkg.PLUGIN_API_VERSION)
cachedir := filepath.Join(cacheDir, "plugins", pkg.PLUGIN_API_VERSION)
backend, err := pkg.NewFlatBackend(ctx.GetInner(), plugdir, cachedir, &pkg.FlatBackendOptions{
PreLoadHook: pkgpreloadhook,
LoadHook: pkgloadhook,
UnloadHook: pkgunloadhook,
})
if err != nil {
return fmt.Errorf("failed to init the package manager: %w", err)
}
token, _ := ctx.GetCookies().GetAuthToken()
manager, err := pkg.New(backend, &pkg.Options{
InstallURL: "https://plakar.io/dist/plugins/kloset/community/",
ApiURL: "https://api.plakar.io/",
BinaryNeedsAuth: true,
UserAgent: "plakar/" + utils.VERSION,
RequestHook: pkg.WithBearer(func() (string, error) { return token, nil }),
})
if err != nil {
return fmt.Errorf("failed to init the package manager: %w", err)
}
if err := backend.LoadAll(); err != nil {
return fmt.Errorf("failed to load packages: %w", err)
}
ctx.SetPkgManager(manager)
return nil
}
func pkgpreloadhook(m *pkg.Manifest) error {
for _, conn := range m.Connectors {
for _, proto := range conn.Protocols {
switch conn.Type {
case "exporter":
if slices.Contains(exporter.Backends(), proto) {
return fmt.Errorf("protocol %s already provided "+
"by another installed package", proto)
}
case "importer":
if slices.Contains(importer.Backends(), proto) {
return fmt.Errorf("protocol %s already provided "+
"by another installed package", proto)
}
case "storage":
if slices.Contains(storage.Backends(), proto) {
return fmt.Errorf("protocol %s already provided "+
"by another installed package", proto)
}
default:
fmt.Fprintf(os.Stderr,
"skipping unknown connector type: %s\n",
conn.Type)
}
}
}
return nil
}
func pkgloadhook(m *pkg.Manifest, p *pkg.Package, pkgdir string) {
if err := plugins.Load(m, pkgdir); err != nil {
fmt.Fprintf(os.Stderr, "%s: failed to load %s@%s: %s\n",
flag.CommandLine.Name(), m.Name, p.Version, err)
}
}
func pkgunloadhook(m *pkg.Manifest, p *pkg.Package) {
if err := plugins.Unload(m); err != nil {
fmt.Fprintf(os.Stderr, "%s: failed to unload %s@%s: %s\n",
flag.CommandLine.Name(), m.Name, p.Version, err)
}
}