-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
150 lines (131 loc) · 4.28 KB
/
main.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
/*
* LURE Updater - Automated updater bot for LURE packages
* Copyright (C) 2023 Elara Musayelyan
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/caarlos0/env/v8"
"github.com/go-git/go-git/v5"
"github.com/pelletier/go-toml/v2"
"github.com/spf13/pflag"
"go.elara.ws/logger"
"go.elara.ws/logger/log"
"lure.sh/lure-updater/internal/builtins"
"lure.sh/lure-updater/internal/config"
"go.etcd.io/bbolt"
"go.starlark.net/starlark"
"golang.org/x/crypto/bcrypt"
"golang.org/x/term"
)
func init() {
log.Logger = logger.NewPretty(os.Stderr)
}
func main() {
configPath := pflag.StringP("config", "c", "/etc/lure-updater/config.toml", "Path to config file")
dbPath := pflag.StringP("database", "d", "/etc/lure-updater/db", "Path to database file")
pluginDir := pflag.StringP("plugin-dir", "p", "/etc/lure-updater/plugins", "Path to plugin directory")
serverAddr := pflag.StringP("address", "a", ":8080", "Webhook server address")
genHash := pflag.BoolP("gen-hash", "g", false, "Generate a password hash for webhooks")
useEnv := pflag.BoolP("use-env", "E", false, "Use environment variables for configuration")
debug := pflag.BoolP("debug", "D", false, "Enable debug logging")
pflag.Parse()
if *debug {
log.Logger.SetLevel(logger.LogLevelDebug)
}
if *genHash {
fmt.Print("Password: ")
pwd, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
log.Fatal("Error reading password").Err(err).Send()
}
hash, err := bcrypt.GenerateFromPassword(pwd, bcrypt.DefaultCost)
if err != nil {
log.Fatal("Error hashing password").Err(err).Send()
}
fmt.Printf("\n%s\n", hash)
return
}
db, err := bbolt.Open(*dbPath, 0o644, nil)
if err != nil {
log.Fatal("Error opening database").Err(err).Send()
}
cfg := &config.Config{}
if *useEnv {
err = env.Parse(cfg)
if err != nil {
log.Fatal("Error parsing environment variables").Err(err).Send()
}
} else {
fl, err := os.Open(*configPath)
if err != nil {
log.Fatal("Error opening config file").Err(err).Send()
}
err = toml.NewDecoder(fl).Decode(cfg)
if err != nil {
log.Fatal("Error decoding config file").Err(err).Send()
}
err = fl.Close()
if err != nil {
log.Fatal("Error closing config file").Err(err).Send()
}
}
if _, err := os.Stat(cfg.Git.RepoDir); os.IsNotExist(err) {
err = os.MkdirAll(cfg.Git.RepoDir, 0o755)
if err != nil {
log.Fatal("Error creating repository directory").Err(err).Send()
}
_, err := git.PlainClone(cfg.Git.RepoDir, false, &git.CloneOptions{
URL: cfg.Git.RepoURL,
Progress: os.Stderr,
})
if err != nil {
log.Fatal("Error cloning repository").Err(err).Send()
}
} else if err != nil {
log.Fatal("Cannot stat configured repo directory").Err(err).Send()
}
starFiles, err := filepath.Glob(filepath.Join(*pluginDir, "*.star"))
if err != nil {
log.Fatal("Error finding plugin files").Err(err).Send()
}
if len(starFiles) == 0 {
log.Fatal("No plugins found. At least one plugin is required.").Send()
}
mux := http.NewServeMux()
for _, starFile := range starFiles {
pluginName := filepath.Base(strings.TrimSuffix(starFile, ".star"))
thread := &starlark.Thread{Name: pluginName}
predeclared := starlark.StringDict{}
builtins.Register(predeclared, &builtins.Options{
Name: pluginName,
Config: cfg,
DB: db,
Mux: mux,
})
_, err = starlark.ExecFile(thread, starFile, nil, predeclared)
if err != nil {
log.Fatal("Error executing starlark file").Str("file", starFile).Err(err).Send()
}
log.Info("Initialized plugin").Str("name", pluginName).Send()
}
log.Info("Starting HTTP server").Str("addr", *serverAddr).Send()
http.ListenAndServe(*serverAddr, mux)
}