-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.go
More file actions
62 lines (54 loc) · 1.8 KB
/
scripts.go
File metadata and controls
62 lines (54 loc) · 1.8 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
package main
import (
"context"
"os"
"path"
"text/template"
"time"
)
func removeOldScripts(ctx context.Context, config Config) {
files, err := os.ReadDir(config.FlatpakDir)
if err != nil {
log.ErrorContext(ctx, "Error reading output directory", "err", err)
os.Exit(1)
}
for _, file := range files {
f, err := os.Open(path.Join(config.FlatpakDir, file.Name()))
if err != nil {
log.ErrorContext(ctx, "Error opening file", "err", err, "file", file.Name())
continue
}
content := make([]byte, 30)
i, err := f.ReadAt(content, 14)
if err != nil || i != 30 || string(content) != "**Generated by flatpak-alias**" {
log.WarnContext(ctx, "Not a managed file", "file", file.Name())
continue
}
err = os.Remove(path.Join(config.FlatpakDir, file.Name()))
if err != nil {
log.ErrorContext(ctx, "Error deleting file", "err", err, "file", file.Name())
}
}
}
func generateScripts(ctx context.Context, apps []Flatpak, scriptTemplate *template.Template, config Config) {
now := time.Now()
for _, app := range apps {
log.InfoContext(ctx, "Found application", "name", app.Application.Name, "command", app.Application.Command)
app.Timestamp = now
if _, err := os.Stat(path.Join(config.FlatpakDir, app.Application.Command)); err == nil {
log.WarnContext(ctx, "File already exists", "command", app.Application.Command)
continue
}
file, err := os.Create(path.Join(config.FlatpakDir, app.Application.Command))
if err != nil {
log.ErrorContext(ctx, "Error creating file", "err", err, "command", app.Application.Command)
continue
}
err = scriptTemplate.Execute(file, app)
if err != nil {
log.ErrorContext(ctx, "Error writing template", "err", err, "command", app.Application.Command)
continue
}
os.Chmod(path.Join(config.FlatpakDir, app.Application.Command), 0755)
}
}