-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
347 lines (301 loc) · 10.9 KB
/
config.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"reflect"
"runtime"
"strconv"
"strings"
"github.com/goccy/go-json"
)
// Config structure holding configuration settings
type Config struct {
RepoURLs []string `json:"repo_urls" env:"DBIN_REPO_URLS"`
MetadataURLs []string `json:"metadata_urls" env:"DBIN_METADATA_URLS"`
InstallDir string `json:"install_dir" env:"DBIN_INSTALL_DIR XDG_BIN_HOME"`
CacheDir string `json:"cache_dir" env:"DBIN_CACHEDIR"`
Limit int `json:"fsearch_limit"`
DisableTruncation bool `json:"disable_truncation" env:"DBIN_NOTRUNCATION"`
RetakeOwnership bool `json:"retake_ownership" env:"DBIN_REOWN"`
UseIntegrationHooks bool `json:"use_integration_hooks" env:"DBIN_USEHOOKS"`
Hooks Hooks `json:"integration_hooks,omitempty"`
CorrectionHooks CorrectionHooks `json:"correction_hooks,omitempty"`
}
// Hooks structure holding user-defined commands per extension
type Hooks struct {
Commands map[string]HookCommands `json:"commands"`
}
// HookCommands structure for integration and deintegration commands
type HookCommands struct {
IntegrationCommands []string `json:"integration_commands"`
DeintegrationCommands []string `json:"deintegration_commands"`
IntegrationErrorMsg string `json:"integration_error_msg"`
DeintegrationErrorMsg string `json:"deintegration_error_msg"`
UseRunFromCache bool `json:"use_run_from_cache"`
NoOp bool `json:"nop"`
}
// CorrectionHooks structure holding user-defined correction hooks
type CorrectionHooks struct {
Commands map[string]CorrectionCommand `json:"commands"`
}
// CorrectionCommand structure for correction commands
type CorrectionCommand struct {
Command string `json:"command"`
}
func executeHookCommand(config *Config, cmdTemplate, binaryPath, extension string, isIntegration bool, verbosityLevel Verbosity, metadata map[string]interface{}) error {
hookCommands, exists := config.Hooks.Commands[extension]
if !exists {
return fmt.Errorf("no commands found for extension: %s", extension)
}
// Check for the NoOp flag
if hookCommands.NoOp {
return nil
}
// Replace {{binary}} with the actual binary path in the command template
cmd := strings.ReplaceAll(cmdTemplate, "{{binary}}", binaryPath)
// Determine whether to use RunFromCache based on the config
useRunFromCache := hookCommands.UseRunFromCache
// Split command into command name and arguments
commandParts := strings.Fields(cmd)
if len(commandParts) == 0 {
//return fmt.Errorf("no command to execute for extension: %s", extension)
return nil
}
command := commandParts[0] // First part is the command
args := commandParts[1:] // Remaining parts are arguments
if useRunFromCache {
// Directly call RunFromCache with the full command string
return RunFromCache(config, command, args, true, verbosityLevel, metadata)
} else {
// Create a new command
cmdExec := exec.Command(command, args...)
// Set the command's output to the same as the current process' output
cmdExec.Stdout = os.Stdout
cmdExec.Stderr = os.Stderr
// Execute the command
if err := cmdExec.Run(); err != nil {
// Use the appropriate error message based on integration or deintegration
var errorMsg string
if isIntegration {
errorMsg = hookCommands.IntegrationErrorMsg
} else {
errorMsg = hookCommands.DeintegrationErrorMsg
}
// Format the error message with the provided error message template
return fmt.Errorf(errorMsg, binaryPath, err)
}
}
return nil
}
// LoadConfig loads the configuration from the JSON file and handles environment variables automatically.
func LoadConfig() (*Config, error) {
// Create a new config instance
cfg := &Config{}
// Get the user config directory
userConfigDir, err := os.UserConfigDir()
if err != nil {
return nil, fmt.Errorf("failed to get user config directory: %v", err)
}
configFilePath := filepath.Join(userConfigDir, "dbin.json")
// Check if the JSON file exists
if _, err := os.Stat(configFilePath); err == nil {
// Load the JSON file if it exists
if err := loadJSON(configFilePath, cfg); err != nil {
return nil, fmt.Errorf("failed to load JSON file: %v", err)
}
} else if !os.IsNotExist(err) {
// Return an error if there's another issue with the file
return nil, fmt.Errorf("failed to access JSON file: %v", err)
} else {
// Create a default config file if it does not exist
if err := CreateDefaultConfig(); err != nil {
return nil, fmt.Errorf("failed to create default config file: %v", err)
}
// Reload the configuration after creating the default file
if err := loadJSON(configFilePath, cfg); err != nil {
return nil, fmt.Errorf("failed to load JSON file: %v", err)
}
}
// Override configuration with environment variables
overrideWithEnv(cfg)
// Set default values if needed
setDefaultValues(cfg)
return cfg, nil
}
// loadJSON loads configuration from a JSON file.
func loadJSON(filePath string, cfg *Config) error {
file, err := os.Open(filePath)
if err != nil {
return err
}
defer file.Close()
decoder := json.NewDecoder(file)
return decoder.Decode(cfg)
}
// overrideWithEnv overrides configuration with environment variables.
func overrideWithEnv(cfg *Config) {
v := reflect.ValueOf(cfg).Elem()
t := v.Type()
// Helper function to set the field value based on the environment variable
setFieldFromEnv := func(field reflect.Value, envVar string) bool {
if value, exists := os.LookupEnv(envVar); exists {
switch field.Kind() {
case reflect.String:
field.SetString(value)
case reflect.Slice:
field.Set(reflect.ValueOf(parseStringSlice(value)))
case reflect.Bool:
if val, err := parseBool(value); err == nil {
field.SetBool(val)
}
case reflect.Int:
if val, err := strconv.Atoi(value); err == nil {
field.SetInt(int64(val))
}
}
return true
}
return false
}
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
envTags := strings.Split(t.Field(i).Tag.Get("env"), " ")
// Check the first environment variable
if len(envTags) > 0 {
firstEnvVar := envTags[0]
if setFieldFromEnv(field, firstEnvVar) {
continue // Skip further processing for this field
}
}
// Check subsequent environment variables if the first one is not set and the field is not already set
if field.IsZero() {
for _, envVar := range envTags[1:] {
if setFieldFromEnv(field, envVar) {
break // Stop processing further env tags for this field
}
}
}
}
}
// parseStringSlice splits a string by commas into a slice.
func parseStringSlice(s string) []string {
return strings.Split(s, ",")
}
// parseBool converts a string to a boolean value.
func parseBool(s string) (bool, error) {
return strconv.ParseBool(s)
}
// setDefaultValues sets default values for specific configuration entries only if they are not already set.
func setDefaultValues(config *Config) {
// Setting default InstallDir if not defined
if config.InstallDir == "" {
homeDir, err := os.UserHomeDir()
if err != nil {
fmt.Printf("failed to get user's Home directory: %v\n", err)
return
}
config.InstallDir = filepath.Join(homeDir, ".local/bin")
}
// Load cache dir from the user's cache directory
tempDir, err := os.UserCacheDir()
if err != nil {
fmt.Printf("failed to get user's Cache directory: %v\n", err)
return
}
if config.CacheDir == "" {
config.CacheDir = filepath.Join(tempDir, "dbin_cache")
}
// Determine architecture and set default repositories and metadata URLs
arch := runtime.GOARCH + "_" + runtime.GOOS
// Set up default repositories if none are provided
if len(config.RepoURLs) == 0 {
config.RepoURLs = []string{
"https://bin.pkgforge.dev/" + arch + "/",
"https://bin.pkgforge.dev/" + arch + "/Baseutils/",
"https://pkg.pkgforge.dev/" + arch + "/",
}
}
// Set up default metadata URLs if none are provided
if len(config.MetadataURLs) == 0 {
config.MetadataURLs = []string{
"https://github.com/xplshn/dbin-metadata/raw/refs/heads/master/misc/cmd/modMetadataAIO-ng/METADATA_AIO_" + arch + ".min.json",
}
}
if config.Limit == 0 {
config.Limit = 90
}
if !config.UseIntegrationHooks {
config.UseIntegrationHooks = true
}
if !config.RetakeOwnership {
config.RetakeOwnership = false
}
}
// CreateDefaultConfig creates a default configuration file.
func CreateDefaultConfig() error {
// Create a new config instance with default values
cfg := &Config{}
setDefaultValues(cfg)
// Set default hooks
cfg.Hooks = Hooks{
Commands: map[string]HookCommands{
".AppBundle": {
IntegrationCommands: []string{"pelfd --integrate {{binary}}"},
DeintegrationCommands: []string{"pelfd --deintegrate {{binary}}"},
IntegrationErrorMsg: "[%s] Could not integrate with the system via pelfd; Error: %v",
DeintegrationErrorMsg: "[%s] Could not deintegrate from the system via pelfd; Error: %v",
UseRunFromCache: true,
},
".AppImage": {
IntegrationCommands: []string{"pelfd --integrate {{binary}}"},
DeintegrationCommands: []string{"pelfd --deintegrate {{binary}}"},
IntegrationErrorMsg: "[%s] Could not integrate with the system via pelfd; Error: %v",
DeintegrationErrorMsg: "[%s] Could not deintegrate from the system via pelfd; Error: %v",
UseRunFromCache: true,
},
".NixAppImage": {
IntegrationCommands: []string{"pelfd --integrate {{binary}}"},
DeintegrationCommands: []string{"pelfd --deintegrate {{binary}}"},
IntegrationErrorMsg: "[%s] Could not integrate with the system via pelfd; Error: %v",
DeintegrationErrorMsg: "[%s] Could not deintegrate from the system via pelfd; Error: %v",
UseRunFromCache: true,
},
"": { // Normal static binaries don't need a handler, so we're just using a NoOp
IntegrationCommands: []string{"upx {{binary}}"},
DeintegrationCommands: []string{""},
IntegrationErrorMsg: "[%s] Could not be UPXed; Error: %v",
UseRunFromCache: true,
NoOp: true,
},
},
}
// Set default correction hooks
cfg.CorrectionHooks = CorrectionHooks{
Commands: map[string]CorrectionCommand{
".no_strip": {
Command: "{{trimSuffix}} {{binary}} .no_strip",
},
},
}
userConfigDir, err := os.UserConfigDir()
if err != nil {
return fmt.Errorf("failed to get user config directory: %v", err)
}
configFilePath := filepath.Join(userConfigDir, "dbin.json")
if err := os.MkdirAll(userConfigDir, 0755); err != nil {
return fmt.Errorf("failed to create config directory: %v", err)
}
// Marshal the config to JSON
configJSON, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal config to JSON: %v", err)
}
// Write the JSON to the config file
if err := os.WriteFile(configFilePath, configJSON, 0644); err != nil {
return fmt.Errorf("failed to write config file: %v", err)
}
fmt.Printf("Default config file created at: %s\n", configFilePath)
return nil
}